Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataRange.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using Unity.Mathematics;
  7. namespace Burst.Compiler.IL.Tests.Helpers
  8. {
  9. [Flags]
  10. internal enum DataRange
  11. {
  12. // Standard Test (Zero, Minus100To100, Inf, Nan)
  13. Standard = Zero | Minus100To100 | Inf | NaN,
  14. // Standard Test (Zero, ZeroExclusiveTo100, Inf, Nan)
  15. StandardPositive = Zero | ZeroExclusiveTo100 | Inf | NaN,
  16. // Standard Between -1 and 1 (Zero, MinusOneInclusiveToOneInclusive, Inf, Nan)
  17. Standard11 = Zero | MinusOneInclusiveToOneInclusive | Inf | NaN,
  18. Zero = 1 << 1,
  19. ZeroExclusiveToOneInclusive = 1 << 2,
  20. MinusOneInclusiveToOneInclusive = 1 << 3,
  21. Minus100To100 = 1 << 4,
  22. ZeroExclusiveTo100 = 1 << 5,
  23. Inf = 1 << 6,
  24. NaN = 1 << 7,
  25. }
  26. internal static class DataRangeExtensions
  27. {
  28. private const int VectorsCount = 6;
  29. public static IEnumerable<object> ExpandRange(this DataRange dataRange, Type type, int seed)
  30. {
  31. if (type == typeof(bool))
  32. {
  33. yield return true;
  34. yield return false;
  35. }
  36. else if (type == typeof(int))
  37. {
  38. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  39. {
  40. yield return (int)(double)value;
  41. }
  42. }
  43. else if (type == typeof(uint))
  44. {
  45. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  46. {
  47. var d = (double) value;
  48. if (d >= 0.0)
  49. {
  50. yield return (uint) d;
  51. }
  52. }
  53. }
  54. else if (type == typeof(long))
  55. {
  56. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  57. {
  58. yield return (long)(double)value;
  59. }
  60. }
  61. else if (type == typeof(ulong))
  62. {
  63. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  64. {
  65. var d = (double)value;
  66. if (d >= 0.0)
  67. {
  68. yield return (ulong)d;
  69. }
  70. }
  71. }
  72. else if (type == typeof(short))
  73. {
  74. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  75. {
  76. yield return (short)(double)value;
  77. }
  78. }
  79. else if (type == typeof(ushort))
  80. {
  81. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf|DataRange.NaN), typeof(double), seed))
  82. {
  83. var d = (double)value;
  84. if (d >= 0.0)
  85. {
  86. yield return (ushort)d;
  87. }
  88. }
  89. }
  90. else if (type == typeof(sbyte))
  91. {
  92. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  93. {
  94. var d = (double)value;
  95. yield return (sbyte) d;
  96. }
  97. }
  98. else if (type == typeof(ushort))
  99. {
  100. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  101. {
  102. var d = (double)value;
  103. if (d >= 0.0)
  104. {
  105. yield return (byte)d;
  106. }
  107. }
  108. }
  109. else if (type == typeof(float))
  110. {
  111. foreach (var value in ExpandRange(dataRange, typeof(double), seed))
  112. {
  113. var d = (double) value;
  114. if (double.IsNaN(d))
  115. {
  116. yield return float.NaN;
  117. }
  118. else if (double.IsPositiveInfinity(d))
  119. {
  120. yield return float.PositiveInfinity;
  121. }
  122. else if (double.IsNegativeInfinity(d))
  123. {
  124. yield return float.NegativeInfinity;
  125. }
  126. else
  127. {
  128. yield return (float) (double) value;
  129. }
  130. }
  131. }
  132. else if (type == typeof(double))
  133. {
  134. if ((dataRange & (DataRange.Minus100To100)) != 0)
  135. {
  136. yield return -100.0;
  137. yield return -77.9;
  138. yield return -50.0;
  139. yield return -36.5;
  140. yield return -9.1;
  141. if ((dataRange & (DataRange.Zero)) != 0)
  142. {
  143. yield return 0.0;
  144. }
  145. yield return 5.1;
  146. yield return 43.5;
  147. yield return 50.0;
  148. yield return 76.8;
  149. yield return 100.0;
  150. if ((dataRange & (DataRange.NaN)) != 0)
  151. {
  152. yield return double.NaN;
  153. }
  154. if ((dataRange & (DataRange.Inf)) != 0)
  155. {
  156. yield return double.PositiveInfinity;
  157. yield return double.NegativeInfinity;
  158. }
  159. }
  160. else if ((dataRange & DataRange.ZeroExclusiveTo100) != 0)
  161. {
  162. foreach (var value in ExpandRange(dataRange | DataRange.Minus100To100, typeof(double), seed))
  163. {
  164. var d = (double)value;
  165. if (double.IsNaN(d) || double.IsInfinity(d))
  166. {
  167. yield return d;
  168. }
  169. else if (d != 0.0)
  170. {
  171. d = d * 0.5 + 50.1;
  172. if (d > 100.0)
  173. {
  174. d = 100.0;
  175. }
  176. yield return d;
  177. }
  178. }
  179. if ((dataRange & (DataRange.Zero)) != 0)
  180. {
  181. yield return 0.0;
  182. }
  183. }
  184. else if ((dataRange & (DataRange.MinusOneInclusiveToOneInclusive)) != 0)
  185. {
  186. foreach (var value in ExpandRange(dataRange | DataRange.Minus100To100, typeof(double), seed))
  187. {
  188. var d = (double) value;
  189. // Return nan/inf as-is
  190. if (double.IsNaN(d) || double.IsInfinity(d) || d == 0.0)
  191. {
  192. yield return d;
  193. }
  194. else
  195. {
  196. yield return d / 100.0;
  197. }
  198. }
  199. }
  200. else if ((dataRange & (DataRange.ZeroExclusiveToOneInclusive)) != 0)
  201. {
  202. foreach (var value in ExpandRange(dataRange | DataRange.ZeroExclusiveTo100, typeof(double), seed))
  203. {
  204. var d = (double) value;
  205. if (double.IsNaN(d) || double.IsInfinity(d) || d == 0.0)
  206. {
  207. yield return d;
  208. }
  209. else
  210. {
  211. yield return d / 100.0;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. throw new NotSupportedException($"Invalid datarange `{dataRange}`: missing either Minus100To100 | MinusOneInclusiveToOneInclusive | ZeroExclusiveToOneInclusive`");
  218. }
  219. }
  220. else if (type.Namespace == "Unity.Mathematics")
  221. {
  222. if (type.IsByRef)
  223. {
  224. type = type.GetElementType();
  225. }
  226. if (type.Name.StartsWith("bool"))
  227. {
  228. var size = (uint)(type.Name["bool".Length] - '0');
  229. var bools = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(bool), seed).OfType<bool>().ToArray();
  230. var indices = Enumerable.Range(0, bools.Length).ToList();
  231. var originalIndices = new List<int>(indices);
  232. var random = new System.Random(seed);
  233. switch (size)
  234. {
  235. case 2:
  236. for (int i = 0; i < VectorsCount; i++)
  237. {
  238. var x = bools[NextIndex(random, indices, originalIndices)];
  239. var y = bools[NextIndex(random, indices, originalIndices)];
  240. yield return new bool2(x, y);
  241. }
  242. break;
  243. case 3:
  244. for (int i = 0; i < VectorsCount; i++)
  245. {
  246. var x = bools[NextIndex(random, indices, originalIndices)];
  247. var y = bools[NextIndex(random, indices, originalIndices)];
  248. var z = bools[NextIndex(random, indices, originalIndices)];
  249. yield return new bool3(x, y, z);
  250. }
  251. break;
  252. case 4:
  253. for (int i = 0; i < VectorsCount; i++)
  254. {
  255. var x = bools[NextIndex(random, indices, originalIndices)];
  256. var y = bools[NextIndex(random, indices, originalIndices)];
  257. var z = bools[NextIndex(random, indices, originalIndices)];
  258. var w = bools[NextIndex(random, indices, originalIndices)];
  259. yield return new bool4(x, y, z, w);
  260. }
  261. break;
  262. default:
  263. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  264. }
  265. }
  266. else if (type.Name.StartsWith("int"))
  267. {
  268. var size = (uint) (type.Name["int".Length] - '0');
  269. var ints = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(int), seed).OfType<int>().ToArray();
  270. var indices = Enumerable.Range(0, ints.Length).ToList();
  271. var originalIndices = new List<int>(indices);
  272. var random = new System.Random(seed);
  273. switch (size)
  274. {
  275. case 2:
  276. for (int i = 0; i < VectorsCount; i++)
  277. {
  278. var x = ints[NextIndex(random, indices, originalIndices)];
  279. var y = ints[NextIndex(random, indices, originalIndices)];
  280. yield return new int2(x, y);
  281. }
  282. break;
  283. case 3:
  284. for (int i = 0; i < VectorsCount; i++)
  285. {
  286. var x = ints[NextIndex(random, indices, originalIndices)];
  287. var y = ints[NextIndex(random, indices, originalIndices)];
  288. var z = ints[NextIndex(random, indices, originalIndices)];
  289. yield return new int3(x, y, z);
  290. }
  291. break;
  292. case 4:
  293. for (int i = 0; i < VectorsCount; i++)
  294. {
  295. var x = ints[NextIndex(random, indices, originalIndices)];
  296. var y = ints[NextIndex(random, indices, originalIndices)];
  297. var z = ints[NextIndex(random, indices, originalIndices)];
  298. var w = ints[NextIndex(random, indices, originalIndices)];
  299. yield return new int4(x, y, z, w);
  300. }
  301. break;
  302. default:
  303. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  304. }
  305. }
  306. else if (type.Name.StartsWith("uint"))
  307. {
  308. var size = (uint)(type.Name["uint".Length] - '0');
  309. var uints = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(uint), seed).OfType<uint>().ToArray();
  310. var indices = Enumerable.Range(0, uints.Length).ToList();
  311. var originalIndices = new List<int>(indices);
  312. var random = new System.Random(seed);
  313. switch (size)
  314. {
  315. case 2:
  316. for (int i = 0; i < VectorsCount; i++)
  317. {
  318. var x = uints[NextIndex(random, indices, originalIndices)];
  319. var y = uints[NextIndex(random, indices, originalIndices)];
  320. yield return new uint2(x, y);
  321. }
  322. break;
  323. case 3:
  324. for (int i = 0; i < VectorsCount; i++)
  325. {
  326. var x = uints[NextIndex(random, indices, originalIndices)];
  327. var y = uints[NextIndex(random, indices, originalIndices)];
  328. var z = uints[NextIndex(random, indices, originalIndices)];
  329. yield return new uint3(x, y, z);
  330. }
  331. break;
  332. case 4:
  333. for (int i = 0; i < VectorsCount; i++)
  334. {
  335. var x = uints[NextIndex(random, indices, originalIndices)];
  336. var y = uints[NextIndex(random, indices, originalIndices)];
  337. var z = uints[NextIndex(random, indices, originalIndices)];
  338. var w = uints[NextIndex(random, indices, originalIndices)];
  339. yield return new uint4(x, y, z, w);
  340. }
  341. break;
  342. default:
  343. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  344. }
  345. }
  346. else if (type.Name.StartsWith("half"))
  347. {
  348. var size = (uint)(type.Name["half".Length] - '0');
  349. var floats = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(float), seed).OfType<float>().ToList();
  350. var originalIndices = Enumerable.Range(0, floats.Count).ToList();
  351. var indices = new List<int>(originalIndices);
  352. // We only put NaN and Inf in the first set of values
  353. if ((dataRange & DataRange.NaN) != 0)
  354. {
  355. indices.Add(floats.Count);
  356. floats.Add(float.NaN);
  357. }
  358. if ((dataRange & DataRange.Inf) != 0)
  359. {
  360. indices.Add(floats.Count);
  361. floats.Add(float.PositiveInfinity);
  362. }
  363. var random = new System.Random(seed);
  364. switch (size)
  365. {
  366. case 2:
  367. for (int i = 0; i < VectorsCount; i++)
  368. {
  369. var x = floats[NextIndex(random, indices, originalIndices)];
  370. var y = floats[NextIndex(random, indices, originalIndices)];
  371. yield return new half2(new float2(x, y));
  372. }
  373. break;
  374. case 3:
  375. for (int i = 0; i < VectorsCount; i++)
  376. {
  377. var x = floats[NextIndex(random, indices, originalIndices)];
  378. var y = floats[NextIndex(random, indices, originalIndices)];
  379. var z = floats[NextIndex(random, indices, originalIndices)];
  380. yield return new half3(new float3(x, y, z));
  381. }
  382. break;
  383. case 4:
  384. for (int i = 0; i < VectorsCount; i++)
  385. {
  386. var x = floats[NextIndex(random, indices, originalIndices)];
  387. var y = floats[NextIndex(random, indices, originalIndices)];
  388. var z = floats[NextIndex(random, indices, originalIndices)];
  389. var w = floats[NextIndex(random, indices, originalIndices)];
  390. yield return new half4(new float4(x, y, z, w));
  391. }
  392. break;
  393. default:
  394. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  395. }
  396. }
  397. else if (type.Name.StartsWith("float"))
  398. {
  399. var size = (uint) (type.Name["float".Length] - '0');
  400. var floats = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(float), seed).OfType<float>().ToList();
  401. var originalIndices = Enumerable.Range(0, floats.Count).ToList();
  402. var indices = new List<int>(originalIndices);
  403. // We only put NaN and Inf in the first set of values
  404. if ((dataRange & DataRange.NaN) != 0)
  405. {
  406. indices.Add(floats.Count);
  407. floats.Add(float.NaN);
  408. }
  409. if ((dataRange & DataRange.Inf) != 0)
  410. {
  411. indices.Add(floats.Count);
  412. floats.Add(float.PositiveInfinity);
  413. }
  414. var random = new System.Random(seed);
  415. switch (size)
  416. {
  417. case 2:
  418. for (int i = 0; i < VectorsCount; i++)
  419. {
  420. var x = floats[NextIndex(random, indices, originalIndices)];
  421. var y = floats[NextIndex(random, indices, originalIndices)];
  422. yield return new float2(x, y);
  423. }
  424. break;
  425. case 3:
  426. for (int i = 0; i < VectorsCount; i++)
  427. {
  428. var x = floats[NextIndex(random, indices, originalIndices)];
  429. var y = floats[NextIndex(random, indices, originalIndices)];
  430. var z = floats[NextIndex(random, indices, originalIndices)];
  431. yield return new float3(x, y, z);
  432. }
  433. break;
  434. case 4:
  435. for (int i = 0; i < VectorsCount; i++)
  436. {
  437. var x = floats[NextIndex(random, indices, originalIndices)];
  438. var y = floats[NextIndex(random, indices, originalIndices)];
  439. var z = floats[NextIndex(random, indices, originalIndices)];
  440. var w = floats[NextIndex(random, indices, originalIndices)];
  441. yield return new float4(x, y, z, w);
  442. }
  443. break;
  444. default:
  445. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  446. }
  447. }
  448. else if (type.Name.StartsWith("double"))
  449. {
  450. var size = (uint)(type.Name["double".Length] - '0');
  451. var doubles = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(double), seed).OfType<double>().ToList();
  452. var originalIndices = Enumerable.Range(0, doubles.Count).ToList();
  453. var indices = new List<int>(originalIndices);
  454. // We only put NaN and Inf in the first set of values
  455. if ((dataRange & DataRange.NaN) != 0)
  456. {
  457. indices.Add(doubles.Count);
  458. doubles.Add(double.NaN);
  459. }
  460. if ((dataRange & DataRange.Inf) != 0)
  461. {
  462. indices.Add(doubles.Count);
  463. doubles.Add(double.PositiveInfinity);
  464. }
  465. var random = new System.Random(seed);
  466. switch (size)
  467. {
  468. case 2:
  469. for (int i = 0; i < VectorsCount; i++)
  470. {
  471. var x = doubles[NextIndex(random, indices, originalIndices)];
  472. var y = doubles[NextIndex(random, indices, originalIndices)];
  473. yield return new double2(x, y);
  474. }
  475. break;
  476. case 3:
  477. for (int i = 0; i < VectorsCount; i++)
  478. {
  479. var x = doubles[NextIndex(random, indices, originalIndices)];
  480. var y = doubles[NextIndex(random, indices, originalIndices)];
  481. var z = doubles[NextIndex(random, indices, originalIndices)];
  482. yield return new double3(x, y, z);
  483. }
  484. break;
  485. case 4:
  486. for (int i = 0; i < VectorsCount; i++)
  487. {
  488. var x = doubles[NextIndex(random, indices, originalIndices)];
  489. var y = doubles[NextIndex(random, indices, originalIndices)];
  490. var z = doubles[NextIndex(random, indices, originalIndices)];
  491. var w = doubles[NextIndex(random, indices, originalIndices)];
  492. yield return new double4(x, y, z, w);
  493. }
  494. break;
  495. default:
  496. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  497. }
  498. }
  499. else
  500. {
  501. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  502. }
  503. }
  504. else
  505. {
  506. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  507. }
  508. }
  509. private static int NextIndex(System.Random random, List<int> indices, List<int> originalIndices)
  510. {
  511. var id = random.Next(0, indices.Count - 1);
  512. var index = indices[id];
  513. indices.RemoveAt(id);
  514. if (indices.Count == 0)
  515. {
  516. indices.AddRange(originalIndices);
  517. }
  518. return index;
  519. }
  520. }
  521. }