No Description
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.

081-UnityMath.cs 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using Burst.Compiler.IL.Tests.Helpers;
  2. using System.Linq.Expressions;
  3. using Unity.Mathematics;
  4. namespace Burst.Compiler.IL.Tests
  5. {
  6. /// <summary>
  7. /// Tests a few single float functions for <see cref="Unity.Mathematics.math"/> functions.
  8. /// </summary>
  9. internal class TestUnityMath
  10. {
  11. [TestCompiler(DataRange.Standard)]
  12. public static float TestCos(float value)
  13. {
  14. return math.cos(value);
  15. }
  16. [TestCompiler(DataRange.Standard)]
  17. public static float TestSin(float value)
  18. {
  19. return math.sin(value);
  20. }
  21. [TestCompiler(DataRange.Standard)]
  22. public static float TestTan(float value)
  23. {
  24. return (float) math.tan(value);
  25. }
  26. [TestCompiler(-1000000f)]
  27. [TestCompiler(-1.2f)]
  28. public static float TestTan2(float value)
  29. {
  30. return (float)math.tan(value);
  31. }
  32. [TestCompiler(DataRange.Standard11)]
  33. public static float TestAcos(float value)
  34. {
  35. return math.acos(value);
  36. }
  37. [TestCompiler(DataRange.Standard11)]
  38. public static float TestAsin(float value)
  39. {
  40. return math.asin(value);
  41. }
  42. [TestCompiler(DataRange.Standard11)]
  43. public static float TestAtan(float value)
  44. {
  45. return (float)math.atan(value);
  46. }
  47. [TestCompiler(DataRange.Standard)]
  48. public static float TestCosh(float value)
  49. {
  50. return math.cosh(value);
  51. }
  52. [TestCompiler(DataRange.Standard)]
  53. public static float TestSinh(float value)
  54. {
  55. return math.sinh(value);
  56. }
  57. [TestCompiler(DataRange.Standard)]
  58. public static float TestTanh(float value)
  59. {
  60. return (float)math.tanh(value);
  61. }
  62. [TestCompiler(DataRange.StandardPositive)]
  63. public static float TestSqrt(float value)
  64. {
  65. return math.sqrt(value);
  66. }
  67. [TestCompiler(DataRange.StandardPositive & ~DataRange.Zero)]
  68. public static float TestLog(float value)
  69. {
  70. return math.log(value);
  71. }
  72. [TestCompiler(DataRange.StandardPositive & ~DataRange.Zero)]
  73. public static float TestLog10(float value)
  74. {
  75. return math.log10(value);
  76. }
  77. [TestCompiler(DataRange.StandardPositive)]
  78. public static float TestExp(float value)
  79. {
  80. return math.exp(value);
  81. }
  82. [TestCompiler(DataRange.Standard & ~(DataRange.Zero|DataRange.NaN), DataRange.Standard)]
  83. [TestCompiler(DataRange.Standard & ~DataRange.Zero, DataRange.Standard & ~DataRange.Zero)]
  84. public static float TestPow(float value, float power)
  85. {
  86. return math.pow(value, power);
  87. }
  88. [TestCompiler(DataRange.Standard)]
  89. public static float TestAbsFloat(float value)
  90. {
  91. return math.abs(value);
  92. }
  93. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  94. public static int TestMaxInt(int left, int right)
  95. {
  96. return math.max(left, right);
  97. }
  98. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  99. public static int TestMinInt(int left, int right)
  100. {
  101. return math.min(left, right);
  102. }
  103. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  104. public static float TestMaxfloat(float left, float right)
  105. {
  106. return math.max(left, right);
  107. }
  108. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  109. public static float TestMinfloat(float left, float right)
  110. {
  111. return math.min(left, right);
  112. }
  113. [TestCompiler(DataRange.Standard & ~DataRange.NaN)]
  114. public static float TestSignFloat(float value)
  115. {
  116. return math.sign(value);
  117. }
  118. [TestCompiler(-123.45)]
  119. [TestCompiler(-1E-20)]
  120. [TestCompiler(0.0)]
  121. [TestCompiler(1E-10)]
  122. [TestCompiler(123.45)]
  123. [TestCompiler(double.NegativeInfinity)]
  124. [TestCompiler(double.NaN)]
  125. [TestCompiler(double.PositiveInfinity)]
  126. public static double TestSignDouble(double value)
  127. {
  128. return math.sign(value);
  129. }
  130. [TestCompiler(DataRange.Standard)]
  131. public static float TestCeilingfloat(float value)
  132. {
  133. return math.ceil(value);
  134. }
  135. [TestCompiler(DataRange.Standard)]
  136. public static float TestFloorfloat(float value)
  137. {
  138. return math.floor(value);
  139. }
  140. [TestCompiler(DataRange.Standard)]
  141. public static float TestRoundfloat(float value)
  142. {
  143. return math.round(value);
  144. }
  145. [TestCompiler(DataRange.Standard)]
  146. public static float TestTruncatefloat(float value)
  147. {
  148. return math.trunc(value);
  149. }
  150. private readonly static float3 a = new float3(1, 2, 3);
  151. [TestCompiler]
  152. public static bool TestStaticLoad()
  153. {
  154. var cmp = a == new float3(1, 2, 3);
  155. return cmp.x && cmp.y && cmp.z;
  156. }
  157. [TestCompiler(42L)]
  158. public static long TestLongCountbits(long value)
  159. {
  160. return math.countbits(value) - 1;
  161. }
  162. [TestCompiler(42L)]
  163. public static long TestLongLzcnt(long value)
  164. {
  165. return math.lzcnt(value) - 1;
  166. }
  167. [TestCompiler(42L)]
  168. public static long TestLongTzcnt(long value)
  169. {
  170. return math.tzcnt(value) - 1;
  171. }
  172. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  173. public static unsafe void TestUshortAddInt2(int2* o, ushort i)
  174. {
  175. *o = i + new int2(0, 0);
  176. }
  177. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  178. public static unsafe void TestInt2AddUshort(int2* o, ushort i)
  179. {
  180. *o = new int2(0, 0) + i;
  181. }
  182. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  183. public static unsafe void TestUshortSubInt2(int2* o, ushort i)
  184. {
  185. *o = i - new int2(0, 0);
  186. }
  187. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  188. public static unsafe void TestInt2SubUshort(int2* o, ushort i)
  189. {
  190. *o = new int2(0, 0) - i;
  191. }
  192. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  193. public static unsafe void TestUshortMulInt2(int2* o, ushort i)
  194. {
  195. *o = i * new int2(0, 0);
  196. }
  197. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  198. public static unsafe void TestInt2MulUshort(int2* o, ushort i)
  199. {
  200. *o = new int2(0, 0) * i;
  201. }
  202. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  203. public static unsafe void TestUshortDivInt2(int2* o, ushort i)
  204. {
  205. *o = i / new int2(1, 1);
  206. }
  207. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  208. public static unsafe void TestInt2DivUshort(int2* o, ushort i)
  209. {
  210. *o = new int2(0, 0) / i;
  211. }
  212. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  213. public static unsafe void TestUshortModInt2(int2* o, ushort i)
  214. {
  215. *o = i % new int2(1, 1);
  216. }
  217. [TestCompiler(typeof(ReturnBox), (ushort)42)]
  218. public static unsafe void TestInt2ModUshort(int2* o, ushort i)
  219. {
  220. *o = new int2(0, 0) % i;
  221. }
  222. [TestCompiler((ushort)42)]
  223. public static unsafe int TestUshortEqInt2(ushort i)
  224. {
  225. return math.all(i == new int2(0, 0)) ? 1 : 0;
  226. }
  227. [TestCompiler((ushort)42)]
  228. public static unsafe int TestInt2EqUshort(ushort i)
  229. {
  230. return math.all(new int2(0, 0) == i) ? 1 : 0;
  231. }
  232. [TestCompiler((ushort)42)]
  233. public static unsafe int TestUshortNeInt2(ushort i)
  234. {
  235. return math.all(i != new int2(0, 0)) ? 1 : 0;
  236. }
  237. [TestCompiler((ushort)42)]
  238. public static unsafe int TestInt2NeUshort(ushort i)
  239. {
  240. return math.all(new int2(0, 0) != i) ? 1 : 0;
  241. }
  242. [TestCompiler((ushort)42)]
  243. public static unsafe int TestUshortGeInt2(ushort i)
  244. {
  245. return math.all(i >= new int2(0, 0)) ? 1 : 0;
  246. }
  247. [TestCompiler((ushort)42)]
  248. public static unsafe int TestInt2GeUshort(ushort i)
  249. {
  250. return math.all(new int2(0, 0) >= i) ? 1 : 0;
  251. }
  252. [TestCompiler((ushort)42)]
  253. public static unsafe int TestUshortGtInt2(ushort i)
  254. {
  255. return math.all(i > new int2(0, 0)) ? 1 : 0;
  256. }
  257. [TestCompiler((ushort)42)]
  258. public static unsafe int TestInt2GtUshort(ushort i)
  259. {
  260. return math.all(new int2(0, 0) > i) ? 1 : 0;
  261. }
  262. [TestCompiler((ushort)42)]
  263. public static unsafe int TestUshortLtInt2(ushort i)
  264. {
  265. return math.all(i < new int2(0, 0)) ? 1 : 0;
  266. }
  267. [TestCompiler((ushort)42)]
  268. public static unsafe int TestInt2LtUshort(ushort i)
  269. {
  270. return math.all(new int2(0, 0) < i) ? 1 : 0;
  271. }
  272. [TestCompiler((ushort)42)]
  273. public static unsafe int TestUshortLeInt2(ushort i)
  274. {
  275. return math.all(i <= new int2(0, 0)) ? 1 : 0;
  276. }
  277. [TestCompiler((ushort)42)]
  278. public static unsafe int TestInt2LeUshort(ushort i)
  279. {
  280. return math.all(new int2(0, 0) <= i) ? 1 : 0;
  281. }
  282. [TestCompiler(42.0f)]
  283. public static float TestSqrtAndAcosIsDefinedBehaviour(float a)
  284. {
  285. // This sqrt call will get folded away, but we need it here because it exhibits the bug.
  286. if (math.sqrt(4) == 2)
  287. {
  288. return math.acos(a);
  289. }
  290. else
  291. {
  292. return 42;
  293. }
  294. }
  295. }
  296. }