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.

080-TestSystemMath.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using System;
  2. using Burst.Compiler.IL.Tests.Helpers;
  3. namespace Burst.Compiler.IL.Tests
  4. {
  5. /// <summary>
  6. /// Tests of the <see cref="System.Math"/> functions.
  7. /// </summary>
  8. internal partial class TestSystemMath
  9. {
  10. [TestCompiler(DataRange.Standard)]
  11. public static double TestCos(float value)
  12. {
  13. return Math.Cos(value);
  14. }
  15. [TestCompiler(DataRange.Standard)]
  16. public static double TestSin(float value)
  17. {
  18. return Math.Sin(value);
  19. }
  20. [TestCompiler(DataRange.Standard)]
  21. public static float TestTan(float value)
  22. {
  23. return (float) Math.Tan(value);
  24. }
  25. [TestCompiler(DataRange.Standard11)]
  26. public static double TestAcos(float value)
  27. {
  28. return Math.Acos(value);
  29. }
  30. [TestCompiler(DataRange.Standard11)]
  31. public static double TestAsin(float value)
  32. {
  33. return Math.Asin(value);
  34. }
  35. [TestCompiler(DataRange.Standard11)]
  36. public static float TestAtan(float value)
  37. {
  38. return (float)Math.Atan(value);
  39. }
  40. [TestCompiler(DataRange.ZeroExclusiveToOneInclusive, DataRange.ZeroExclusiveToOneInclusive)]
  41. public static float TestAtan2(float y, float x)
  42. {
  43. return (float)Math.Atan2(y, x);
  44. }
  45. [TestCompiler(DataRange.Standard)]
  46. public static double TestCosh(float value)
  47. {
  48. return Math.Cosh(value);
  49. }
  50. [TestCompiler(DataRange.Standard)]
  51. public static double TestSinh(float value)
  52. {
  53. return Math.Sinh(value);
  54. }
  55. [TestCompiler(DataRange.Standard)]
  56. public static float TestTanh(float value)
  57. {
  58. return (float)Math.Tanh(value);
  59. }
  60. [TestCompiler(DataRange.StandardPositive)]
  61. public static double TestSqrt(float value)
  62. {
  63. return Math.Sqrt(value);
  64. }
  65. [TestCompiler(DataRange.StandardPositive & ~DataRange.Zero)]
  66. public static double TestLog(float value)
  67. {
  68. return Math.Log(value);
  69. }
  70. [TestCompiler(DataRange.StandardPositive & ~DataRange.Zero)]
  71. public static double TestLog10(float value)
  72. {
  73. return Math.Log10(value);
  74. }
  75. [TestCompiler(DataRange.StandardPositive)]
  76. public static double TestExp(float value)
  77. {
  78. return Math.Exp(value);
  79. }
  80. [TestCompiler(DataRange.Standard & ~(DataRange.Zero|DataRange.NaN), DataRange.Standard)]
  81. [TestCompiler(DataRange.Standard & ~DataRange.Zero, DataRange.Standard & ~DataRange.Zero)]
  82. public static double TestPow(float value, float power)
  83. {
  84. return Math.Pow(value, power);
  85. }
  86. [TestCompiler(DataRange.Standard)]
  87. public static sbyte TestAbsSByte(sbyte value)
  88. {
  89. return Math.Abs(value);
  90. }
  91. [TestCompiler(DataRange.Standard)]
  92. public static short TestAbsShort(short value)
  93. {
  94. return Math.Abs(value);
  95. }
  96. [TestCompiler(DataRange.Standard)]
  97. public static int TestAbsInt(int value)
  98. {
  99. return Math.Abs(value);
  100. }
  101. [TestCompiler(DataRange.Standard)]
  102. public static long TestAbsLong(long value)
  103. {
  104. return Math.Abs(value);
  105. }
  106. [TestCompiler(DataRange.Standard)]
  107. public static float TestAbsFloat(float value)
  108. {
  109. return Math.Abs(value);
  110. }
  111. [TestCompiler(DataRange.Standard)]
  112. public static double TestAbsDouble(double value)
  113. {
  114. return Math.Abs(value);
  115. }
  116. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  117. public static int TestMaxInt(int left, int right)
  118. {
  119. return Math.Max(left, right);
  120. }
  121. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  122. public static int TestMinInt(int left, int right)
  123. {
  124. return Math.Min(left, right);
  125. }
  126. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  127. public static double TestMaxDouble(double left, double right)
  128. {
  129. return Math.Max(left, right);
  130. }
  131. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  132. public static double TestMinDouble(double left, double right)
  133. {
  134. return Math.Min(left, right);
  135. }
  136. [TestCompiler(DataRange.Standard)]
  137. public static int TestSignInt(int value)
  138. {
  139. return Math.Sign(value);
  140. }
  141. [TestCompiler(DataRange.Standard & ~DataRange.NaN)]
  142. public static int TestSignFloat(float value)
  143. {
  144. return Math.Sign(value);
  145. }
  146. [TestCompiler(float.NaN, ExpectedException = typeof(ArithmeticException))]
  147. [MonoOnly(".NET CLR does not support burst.abort correctly")]
  148. public static int TestSignException(float value)
  149. {
  150. return Math.Sign(value);
  151. }
  152. [TestCompiler(DataRange.Standard & ~DataRange.NaN)]
  153. public static int TestSignDouble(double value)
  154. {
  155. return Math.Sign(value);
  156. }
  157. [TestCompiler(DataRange.Standard)]
  158. public static double TestCeilingDouble(double value)
  159. {
  160. return Math.Ceiling(value);
  161. }
  162. [TestCompiler(DataRange.Standard)]
  163. public static double TestFloorDouble(double value)
  164. {
  165. return Math.Floor(value);
  166. }
  167. [TestCompiler(DataRange.Standard)]
  168. public static double TestRoundDouble(double value)
  169. {
  170. return Math.Round(value);
  171. }
  172. [TestCompiler(DataRange.Standard, DataRange.ZeroInclusiveToFifteenInclusive, SkipForILInterpreter = true)] // https://jira.unity3d.com/browse/BUR-2376
  173. public static double TestRoundDoubleDigits(double value, int digits)
  174. {
  175. return Math.Round(value, digits);
  176. }
  177. [TestCompiler(DataRange.Standard, MidpointRounding.ToEven, SkipForILInterpreter = true)] // https://jira.unity3d.com/browse/BUR-2376
  178. [TestCompiler(DataRange.Standard, MidpointRounding.AwayFromZero, SkipForILInterpreter = true)] // https://jira.unity3d.com/browse/BUR-2376
  179. public static double TestRoundDoubleMidpoint(double value, MidpointRounding mode)
  180. {
  181. return Math.Round(value, mode);
  182. }
  183. [TestCompiler(DataRange.Standard, DataRange.ZeroInclusiveToFifteenInclusive, MidpointRounding.ToEven, SkipForILInterpreter = true)] // https://jira.unity3d.com/browse/BUR-2376
  184. [TestCompiler(DataRange.Standard, DataRange.ZeroInclusiveToFifteenInclusive, MidpointRounding.AwayFromZero, SkipForILInterpreter = true)] // https://jira.unity3d.com/browse/BUR-2376
  185. public static double TestRoundDoubleDigitsMidpoint(double value, int digits, MidpointRounding mode)
  186. {
  187. return Math.Round(value, digits, mode);
  188. }
  189. [TestCompiler(DataRange.Standard)]
  190. public static double TestTruncateDouble(double value)
  191. {
  192. return Math.Truncate(value);
  193. }
  194. [TestCompiler(DataRange.Standard, DataRange.Standard & ~DataRange.Zero)]
  195. public static int TestDivRemInt(int a, int b)
  196. {
  197. int remResult;
  198. var divResult = Math.DivRem(a, b, out remResult);
  199. return divResult + remResult * 7;
  200. }
  201. [TestCompiler(DataRange.Standard, DataRange.StandardPositiveExclusiveZero)]
  202. public static long TestDivRemLong(long a, long b)
  203. {
  204. var divResult = Math.DivRem(a, b, out var remResult);
  205. return divResult + remResult * 7;
  206. }
  207. #if NET6_0_OR_GREATER
  208. [TestCompiler(0xdead, 0xbeef)]
  209. [TestCompiler(0x0, 0xbeef)]
  210. public static nint TestDivRemIntPtr(nint a, nint b)
  211. {
  212. var quotrem = Math.DivRem(a, b);
  213. return quotrem.Quotient + quotrem.Remainder * 7;
  214. }
  215. [TestCompiler(0xdeadu, 0xbeefu)]
  216. [TestCompiler(0x0u, 0xbeefu)]
  217. public static nuint TestDivRemUIntPtr(nuint a, nuint b)
  218. {
  219. var quotRem = Math.DivRem(a, b);
  220. return quotRem.Quotient + quotRem.Remainder * 7;
  221. }
  222. [TestCompiler(DataRange.StandardPositive, DataRange.StandardPositiveExclusiveZero)]
  223. public static ulong TestDivRemUlong(ulong a, ulong b)
  224. {
  225. var quotRem = Math.DivRem(a, b);
  226. return quotRem.Quotient + quotRem.Remainder * 7;
  227. }
  228. [TestCompiler(DataRange.StandardPositive, DataRange.StandardPositiveExclusiveZero)]
  229. public static uint TestDivRemUint(uint a, uint b)
  230. {
  231. var quotRem = Math.DivRem(a, b);
  232. return quotRem.Quotient + quotRem.Remainder * 7;
  233. }
  234. [TestCompiler(DataRange.Standard11, DataRange.StandardPositiveExclusiveZero)]
  235. public static sbyte TestDivRemSByte(sbyte a, sbyte b)
  236. {
  237. var quotRem = Math.DivRem(a, b);
  238. return (sbyte)(quotRem.Quotient + quotRem.Remainder * 7);
  239. }
  240. [TestCompiler(DataRange.StandardPositive, DataRange.StandardPositiveExclusiveZero)]
  241. public static byte TestDivRemByte(byte a, byte b)
  242. {
  243. var quotRem = Math.DivRem(a, b);
  244. return (byte)(quotRem.Quotient + quotRem.Remainder * 7);
  245. }
  246. [TestCompiler(DataRange.Standard & ~DataRange.HighIntRange, DataRange.StandardPositiveExclusiveZero)]
  247. public static short TestDivRemInt16(short a, short b)
  248. {
  249. var quotRem = Math.DivRem(a, b);
  250. return (short)(quotRem.Quotient + quotRem.Remainder * 7);
  251. }
  252. [TestCompiler(DataRange.StandardPositive, DataRange.StandardPositiveExclusiveZero)]
  253. public static ushort TestDivRemUint16(ushort a, ushort b)
  254. {
  255. var quotRem = Math.DivRem(a, b);
  256. return (ushort)(quotRem.Quotient + quotRem.Remainder * 7);
  257. }
  258. // TODO: Commented out due to missing intrinsic implementation.
  259. // TODO: Can be reenabled when the intrinsic PR lands.
  260. //[TestCompiler(DataRange.Standard11)]
  261. public static double TestBitDecrement(double a)
  262. {
  263. return Math.BitDecrement(a);
  264. }
  265. // TODO: Commented out due to missing intrinsic implementation.
  266. // TODO: Can be reenabled when the intrinsic PR lands.
  267. //[TestCompiler(DataRange.Standard11)]
  268. public static double TestBitIncrement(double a)
  269. {
  270. return Math.BitIncrement(a);
  271. }
  272. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  273. public static double TestCopySign(double a, double b)
  274. {
  275. return Math.CopySign(a, b);
  276. }
  277. [TestCompiler(DataRange.Standard)]
  278. public static int TestILogB(double a)
  279. {
  280. return Math.ILogB(a);
  281. }
  282. [TestCompiler(DataRange.Standard)]
  283. public static double TestLog2(double a)
  284. {
  285. return Math.Log2(a);
  286. }
  287. // TODO: Commented out due to missing intrinsic implementation.
  288. // TODO: Can be reenabled when the intrinsic PR lands.
  289. //[TestCompiler(DataRange.Standard, DataRange.Standard)]
  290. public static double TestMaxMagnitude(double a, double b)
  291. {
  292. return Math.MaxMagnitude(a, b);
  293. }
  294. // TODO: Commented out due to missing intrinsic implementation.
  295. // TODO: Can be reenabled when the intrinsic PR lands.
  296. //[TestCompiler(DataRange.Standard, DataRange.Standard)]
  297. public static double TestMinMagnitude(double a, double b)
  298. {
  299. return Math.MinMagnitude(a, b);
  300. }
  301. // TODO: Commented out due to missing intrinsic implementation.
  302. // TODO: Can be reenabled when the intrinsic PR lands.
  303. //[TestCompiler(DataRange.Standard)]
  304. public static double TestReciprocalEstimate(double a)
  305. {
  306. return Math.ReciprocalEstimate(a);
  307. }
  308. // TODO: Commented out due to missing intrinsic implementation.
  309. // TODO: Can be reenabled when the intrinsic PR lands.
  310. //[TestCompiler(DataRange.Standard)]
  311. public static double TestReciprocalSqrtEstimate(double a)
  312. {
  313. return Math.ReciprocalSqrtEstimate(a);
  314. }
  315. // TODO: Commented out due to missing intrinsic implementation.
  316. // TODO: Can be reenabled when the intrinsic PR lands.
  317. //[TestCompiler(DataRange.Standard, DataRange.Standard)]
  318. //[TestCompiler(int.MaxValue, DataRange.Standard)]
  319. public static ulong TestBigMulUlong(ulong a, ulong b)
  320. {
  321. var high = Math.BigMul(a, b, out ulong low);
  322. return high + low;
  323. }
  324. // TODO: Commented out due to missing intrinsic implementation.
  325. // TODO: Can be reenabled when the intrinsic PR lands.
  326. //[TestCompiler(DataRange.Standard, DataRange.Standard)]
  327. //[TestCompiler(int.MaxValue, DataRange.Standard)]
  328. public static long TestBigMulLong(long a, long b)
  329. {
  330. var high = Math.BigMul(a, b, out long low);
  331. return high + low;
  332. }
  333. #endif
  334. [TestCompiler(DataRange.Standard, DataRange.Standard)]
  335. [TestCompiler(int.MaxValue, DataRange.Standard)]
  336. public static long TestBigMulInt(int a, int b)
  337. {
  338. return Math.BigMul(a, b);
  339. }
  340. [TestCompiler(DataRange.Standard & ~DataRange.Zero, DataRange.Standard & ~DataRange.Zero)]
  341. public static double TestLogWithBaseDouble(double a, double newBase)
  342. {
  343. return Math.Log(a, newBase);
  344. }
  345. #if NET5_0_OR_GREATER || NETSTANDARD2_1
  346. [TestCompiler(DataRange.Standard, (byte)1, (byte)50)]
  347. public static byte TestClampByte(byte a, byte min, byte max)
  348. {
  349. return Math.Clamp(a, min, max);
  350. }
  351. [TestCompiler(DataRange.Standard, 1.0, 50.0)]
  352. public static double TestClampDouble(double a, double min, double max)
  353. {
  354. return Math.Clamp(a, min, max);
  355. }
  356. [TestCompiler(DataRange.Standard, (short)1, (short)50)]
  357. public static short TestClampShort(short a, short min, short max)
  358. {
  359. return Math.Clamp(a, min, max);
  360. }
  361. [TestCompiler(DataRange.Standard, 1, 50)]
  362. public static int TestClampInt(int a, int min, int max)
  363. {
  364. return Math.Clamp(a, min, max);
  365. }
  366. [TestCompiler(DataRange.Standard, 1L, 50L)]
  367. public static long TestClampLong(long a, long min, long max)
  368. {
  369. return Math.Clamp(a, min, max);
  370. }
  371. [TestCompiler(DataRange.Standard, (sbyte)1, (sbyte)50)]
  372. public static sbyte TestClampSByte(sbyte a, sbyte min, sbyte max)
  373. {
  374. return Math.Clamp(a, min, max);
  375. }
  376. [TestCompiler(DataRange.Standard, 1.0f, 50.0f)]
  377. public static float TestClampFloat(float a, float min, float max)
  378. {
  379. return Math.Clamp(a, min, max);
  380. }
  381. [TestCompiler(DataRange.Standard, (ushort)1, (ushort)50)]
  382. public static ushort TestClampUShort(ushort a, ushort min, ushort max)
  383. {
  384. return Math.Clamp(a, min, max);
  385. }
  386. [TestCompiler(DataRange.Standard, (uint)1, (uint)50)]
  387. public static uint TestClampUInt(uint a, uint min, uint max)
  388. {
  389. return Math.Clamp(a, min, max);
  390. }
  391. [TestCompiler(DataRange.Standard, (ulong)1, (ulong)50)]
  392. public static ulong TestClampULong(ulong a, ulong min, ulong max)
  393. {
  394. return Math.Clamp(a, min, max);
  395. }
  396. // TODO: Commented out due to missing intrinsic implementation.
  397. // TODO: Can be reenabled when the intrinsic PR lands.
  398. //[TestCompiler(DataRange.Standard, DataRange.Standard)]
  399. public static double TestIEEERemainder(double a, double newBase)
  400. {
  401. return Math.IEEERemainder(a, newBase);
  402. }
  403. [TestCompiler(DataRange.Standard11)]
  404. public static double TestAcosh(double a)
  405. {
  406. return Math.Acosh(a);
  407. }
  408. [TestCompiler(DataRange.Standard11)]
  409. public static double TestAsinh(double a)
  410. {
  411. return Math.Asinh(a);
  412. }
  413. [TestCompiler(DataRange.Standard11)]
  414. public static double TestAtanh(double a)
  415. {
  416. return Math.Atanh(a);
  417. }
  418. [TestCompiler(DataRange.Standard11)]
  419. public static double TestCbrt(double a)
  420. {
  421. return Math.Cbrt(a);
  422. }
  423. #endif
  424. [TestCompiler(DataRange.Standard)]
  425. public static bool TestIsNanDouble(double a)
  426. {
  427. return double.IsNaN(a);
  428. }
  429. [TestCompiler(DataRange.Standard)]
  430. public static bool TestIsNanFloat(float a)
  431. {
  432. return float.IsNaN(a);
  433. }
  434. [TestCompiler(DataRange.Standard, IgnoreOnNetCore = true)] // Disabled due to BitConverter doubletoint which pulls in System.Runtime.Intrinsics, which pulls in a typeof
  435. public static bool TestIsInfinityDouble(double a)
  436. {
  437. return double.IsInfinity(a);
  438. }
  439. [TestCompiler(DataRange.Standard, IgnoreOnNetCore = true)] // Disabled due to BitConverter doubletoint which pulls in System.Runtime.Intrinsics, which pulls in a typeof
  440. public static bool TestIsInfinityFloat(float a)
  441. {
  442. return float.IsInfinity(a);
  443. }
  444. }
  445. }