math.odin 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. package math
  2. import "intrinsics"
  3. _ :: intrinsics;
  4. Float_Class :: enum {
  5. Normal, // an ordinary nonzero floating point value
  6. Subnormal, // a subnormal floating point value
  7. Zero, // zero
  8. Neg_Zero, // the negative zero
  9. NaN, // Not-A-Number (NaN)
  10. Inf, // positive infinity
  11. Neg_Inf, // negative infinity
  12. };
  13. TAU :: 6.28318530717958647692528676655900576;
  14. PI :: 3.14159265358979323846264338327950288;
  15. E :: 2.71828182845904523536;
  16. τ :: TAU;
  17. π :: PI;
  18. e :: E;
  19. SQRT_TWO :: 1.41421356237309504880168872420969808;
  20. SQRT_THREE :: 1.73205080756887729352744634150587236;
  21. SQRT_FIVE :: 2.23606797749978969640917366873127623;
  22. LN2 :: 0.693147180559945309417232121458176568;
  23. LN10 :: 2.30258509299404568401799145468436421;
  24. MAX_F64_PRECISION :: 16; // Maximum number of meaningful digits after the decimal point for 'f64'
  25. MAX_F32_PRECISION :: 8; // Maximum number of meaningful digits after the decimal point for 'f32'
  26. MAX_F16_PRECISION :: 4; // Maximum number of meaningful digits after the decimal point for 'f16'
  27. RAD_PER_DEG :: TAU/360.0;
  28. DEG_PER_RAD :: 360.0/TAU;
  29. @(default_calling_convention="none")
  30. foreign _ {
  31. @(link_name="llvm.sqrt.f16")
  32. sqrt_f16 :: proc(x: f16) -> f16 ---;
  33. @(link_name="llvm.sqrt.f32")
  34. sqrt_f32 :: proc(x: f32) -> f32 ---;
  35. @(link_name="llvm.sqrt.f64")
  36. sqrt_f64 :: proc(x: f64) -> f64 ---;
  37. @(link_name="llvm.sin.f16")
  38. sin_f16 :: proc(θ: f16) -> f16 ---;
  39. @(link_name="llvm.sin.f32")
  40. sin_f32 :: proc(θ: f32) -> f32 ---;
  41. @(link_name="llvm.sin.f64")
  42. sin_f64 :: proc(θ: f64) -> f64 ---;
  43. @(link_name="llvm.cos.f16")
  44. cos_f16 :: proc(θ: f16) -> f16 ---;
  45. @(link_name="llvm.cos.f32")
  46. cos_f32 :: proc(θ: f32) -> f32 ---;
  47. @(link_name="llvm.cos.f64")
  48. cos_f64 :: proc(θ: f64) -> f64 ---;
  49. @(link_name="llvm.pow.f16")
  50. pow_f16 :: proc(x, power: f16) -> f16 ---;
  51. @(link_name="llvm.pow.f32")
  52. pow_f32 :: proc(x, power: f32) -> f32 ---;
  53. @(link_name="llvm.pow.f64")
  54. pow_f64 :: proc(x, power: f64) -> f64 ---;
  55. @(link_name="llvm.fmuladd.f16")
  56. fmuladd_f16 :: proc(a, b, c: f16) -> f16 ---;
  57. @(link_name="llvm.fmuladd.f32")
  58. fmuladd_f32 :: proc(a, b, c: f32) -> f32 ---;
  59. @(link_name="llvm.fmuladd.f64")
  60. fmuladd_f64 :: proc(a, b, c: f64) -> f64 ---;
  61. @(link_name="llvm.log.f16")
  62. ln_f16 :: proc(x: f16) -> f16 ---;
  63. @(link_name="llvm.log.f32")
  64. ln_f32 :: proc(x: f32) -> f32 ---;
  65. @(link_name="llvm.log.f64")
  66. ln_f64 :: proc(x: f64) -> f64 ---;
  67. @(link_name="llvm.exp.f16")
  68. exp_f16 :: proc(x: f16) -> f16 ---;
  69. @(link_name="llvm.exp.f32")
  70. exp_f32 :: proc(x: f32) -> f32 ---;
  71. @(link_name="llvm.exp.f64")
  72. exp_f64 :: proc(x: f64) -> f64 ---;
  73. @(link_name="llvm.ldexp.f16")
  74. ldexp_f16 :: proc(val: f16, exp: i32) -> f16 ---;
  75. @(link_name="llvm.ldexp.f32")
  76. ldexp_f32 :: proc(val: f32, exp: i32) -> f32 ---;
  77. @(link_name="llvm.ldexp.f64")
  78. ldexp_f64 :: proc(val: f64, exp: i32) -> f64 ---;
  79. }
  80. sqrt :: proc{sqrt_f16, sqrt_f32, sqrt_f64};
  81. sin :: proc{sin_f16, sin_f32, sin_f64};
  82. cos :: proc{cos_f16, cos_f32, cos_f64};
  83. pow :: proc{pow_f16, pow_f32, pow_f64};
  84. fmuladd :: proc{fmuladd_f16, fmuladd_f32, fmuladd_f64};
  85. ln :: proc{ln_f16, ln_f32, ln_f64};
  86. exp :: proc{exp_f16, exp_f32, exp_f64};
  87. ldexp :: proc{ldexp_f16, ldexp_f32, ldexp_f64};
  88. log_f16 :: proc(x, base: f16) -> f16 { return ln(x) / ln(base); }
  89. log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base); }
  90. log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base); }
  91. log :: proc{log_f16, log_f32, log_f64};
  92. log2_f16 :: proc(x: f16) -> f16 { return ln(x)/LN2; }
  93. log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2; }
  94. log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2; }
  95. log2 :: proc{log2_f16, log2_f32, log2_f64};
  96. log10_f16 :: proc(x: f16) -> f16 { return ln(x)/LN10; }
  97. log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10; }
  98. log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10; }
  99. log10 :: proc{log10_f16, log10_f32, log10_f64};
  100. tan_f16 :: proc(θ: f16) -> f16 { return sin(θ)/cos(θ); }
  101. tan_f32 :: proc(θ: f32) -> f32 { return sin(θ)/cos(θ); }
  102. tan_f64 :: proc(θ: f64) -> f64 { return sin(θ)/cos(θ); }
  103. tan :: proc{tan_f16, tan_f32, tan_f64};
  104. lerp :: proc(a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t; }
  105. saturate :: proc(a: $T) -> (x: T) { return clamp(a, 0, 1); };
  106. unlerp_f16 :: proc(a, b, x: f16) -> (t: f16) { return (x-a)/(b-a); }
  107. unlerp_f32 :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
  108. unlerp_f64 :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
  109. unlerp :: proc{unlerp_f16, unlerp_f32, unlerp_f64};
  110. wrap :: proc(x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  111. tmp := mod(x, y);
  112. return y + tmp if tmp < 0 else tmp;
  113. }
  114. angle_diff :: proc(a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  115. dist := wrap(b - a, TAU);
  116. return wrap(dist*2, TAU) - dist;
  117. }
  118. angle_lerp :: proc(a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  119. return a + angle_diff(a, b) * t;
  120. }
  121. step :: proc(edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  122. return 0 if x < edge else 1;
  123. }
  124. smoothstep :: proc(edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  125. t := clamp((x - edge0) / (edge1 - edge0), 0, 1);
  126. return t * t * (3 - 2*t);
  127. }
  128. bias :: proc(t, b: $T) -> T where intrinsics.type_is_numeric(T) {
  129. return t / (((1/b) - 2) * (1 - t) + 1);
  130. }
  131. gain :: proc(t, g: $T) -> T where intrinsics.type_is_numeric(T) {
  132. if t < 0.5 {
  133. return bias(t*2, g)*0.5;
  134. }
  135. return bias(t*2 - 1, 1 - g)*0.5 + 0.5;
  136. }
  137. sign_f16 :: proc(x: f16) -> f16 { return f16(int(0 < x) - int(x < 0)); }
  138. sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
  139. sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
  140. sign :: proc{sign_f16, sign_f32, sign_f64};
  141. sign_bit_f16 :: proc(x: f16) -> bool {
  142. return (transmute(u16)x) & (1<<15) != 0;
  143. }
  144. sign_bit_f32 :: proc(x: f32) -> bool {
  145. return (transmute(u32)x) & (1<<31) != 0;
  146. }
  147. sign_bit_f64 :: proc(x: f64) -> bool {
  148. return (transmute(u64)x) & (1<<63) != 0;
  149. }
  150. sign_bit :: proc{sign_bit_f16, sign_bit_f32, sign_bit_f64};
  151. copy_sign_f16 :: proc(x, y: f16) -> f16 {
  152. ix := transmute(u16)x;
  153. iy := transmute(u16)y;
  154. ix &= 0x7fff;
  155. ix |= iy & 0x8000;
  156. return transmute(f16)ix;
  157. }
  158. copy_sign_f32 :: proc(x, y: f32) -> f32 {
  159. ix := transmute(u32)x;
  160. iy := transmute(u32)y;
  161. ix &= 0x7fff_ffff;
  162. ix |= iy & 0x8000_0000;
  163. return transmute(f32)ix;
  164. }
  165. copy_sign_f64 :: proc(x, y: f64) -> f64 {
  166. ix := transmute(u64)x;
  167. iy := transmute(u64)y;
  168. ix &= 0x7fff_ffff_ffff_ffff;
  169. ix |= iy & 0x8000_0000_0000_0000;
  170. return transmute(f64)ix;
  171. }
  172. copy_sign :: proc{copy_sign_f16, copy_sign_f32, copy_sign_f64};
  173. to_radians_f16 :: proc(degrees: f16) -> f16 { return degrees * RAD_PER_DEG; }
  174. to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG; }
  175. to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG; }
  176. to_degrees_f16 :: proc(radians: f16) -> f16 { return radians * DEG_PER_RAD; }
  177. to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD; }
  178. to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD; }
  179. to_radians :: proc{to_radians_f16, to_radians_f32, to_radians_f64};
  180. to_degrees :: proc{to_degrees_f16, to_degrees_f32, to_degrees_f64};
  181. trunc_f16 :: proc(x: f16) -> f16 {
  182. trunc_internal :: proc(f: f16) -> f16 {
  183. mask :: 0x1f;
  184. shift :: 16 - 6;
  185. bias :: 0xf;
  186. if f < 1 {
  187. switch {
  188. case f < 0: return -trunc_internal(-f);
  189. case f == 0: return f;
  190. case: return 0;
  191. }
  192. }
  193. x := transmute(u16)f;
  194. e := (x >> shift) & mask - bias;
  195. if e < shift {
  196. x &= ~(1 << (shift-e)) - 1;
  197. }
  198. return transmute(f16)x;
  199. }
  200. switch classify(x) {
  201. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  202. return x;
  203. case .Normal, .Subnormal: // carry on
  204. }
  205. return trunc_internal(x);
  206. }
  207. trunc_f32 :: proc(x: f32) -> f32 {
  208. trunc_internal :: proc(f: f32) -> f32 {
  209. mask :: 0xff;
  210. shift :: 32 - 9;
  211. bias :: 0x7f;
  212. if f < 1 {
  213. switch {
  214. case f < 0: return -trunc_internal(-f);
  215. case f == 0: return f;
  216. case: return 0;
  217. }
  218. }
  219. x := transmute(u32)f;
  220. e := (x >> shift) & mask - bias;
  221. if e < shift {
  222. x &= ~(1 << (shift-e)) - 1;
  223. }
  224. return transmute(f32)x;
  225. }
  226. switch classify(x) {
  227. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  228. return x;
  229. case .Normal, .Subnormal: // carry on
  230. }
  231. return trunc_internal(x);
  232. }
  233. trunc_f64 :: proc(x: f64) -> f64 {
  234. trunc_internal :: proc(f: f64) -> f64 {
  235. mask :: 0x7ff;
  236. shift :: 64 - 12;
  237. bias :: 0x3ff;
  238. if f < 1 {
  239. switch {
  240. case f < 0: return -trunc_internal(-f);
  241. case f == 0: return f;
  242. case: return 0;
  243. }
  244. }
  245. x := transmute(u64)f;
  246. e := (x >> shift) & mask - bias;
  247. if e < shift {
  248. x &= ~(1 << (shift-e)) - 1;
  249. }
  250. return transmute(f64)x;
  251. }
  252. switch classify(x) {
  253. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  254. return x;
  255. case .Normal, .Subnormal: // carry on
  256. }
  257. return trunc_internal(x);
  258. }
  259. trunc :: proc{trunc_f16, trunc_f32, trunc_f64};
  260. round_f16 :: proc(x: f16) -> f16 {
  261. return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
  262. }
  263. round_f32 :: proc(x: f32) -> f32 {
  264. return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
  265. }
  266. round_f64 :: proc(x: f64) -> f64 {
  267. return ceil(x - 0.5) if x < 0 else floor(x + 0.5);
  268. }
  269. round :: proc{round_f16, round_f32, round_f64};
  270. ceil_f16 :: proc(x: f16) -> f16 { return -floor(-x); }
  271. ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x); }
  272. ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x); }
  273. ceil :: proc{ceil_f16, ceil_f32, ceil_f64};
  274. floor_f16 :: proc(x: f16) -> f16 {
  275. if x == 0 || is_nan(x) || is_inf(x) {
  276. return x;
  277. }
  278. if x < 0 {
  279. d, fract := modf(-x);
  280. if fract != 0.0 {
  281. d = d + 1;
  282. }
  283. return -d;
  284. }
  285. d, _ := modf(x);
  286. return d;
  287. }
  288. floor_f32 :: proc(x: f32) -> f32 {
  289. if x == 0 || is_nan(x) || is_inf(x) {
  290. return x;
  291. }
  292. if x < 0 {
  293. d, fract := modf(-x);
  294. if fract != 0.0 {
  295. d = d + 1;
  296. }
  297. return -d;
  298. }
  299. d, _ := modf(x);
  300. return d;
  301. }
  302. floor_f64 :: proc(x: f64) -> f64 {
  303. if x == 0 || is_nan(x) || is_inf(x) {
  304. return x;
  305. }
  306. if x < 0 {
  307. d, fract := modf(-x);
  308. if fract != 0.0 {
  309. d = d + 1;
  310. }
  311. return -d;
  312. }
  313. d, _ := modf(x);
  314. return d;
  315. }
  316. floor :: proc{floor_f16, floor_f32, floor_f64};
  317. floor_div :: proc(x, y: $T) -> T
  318. where intrinsics.type_is_integer(T) {
  319. a := x / y;
  320. r := x % y;
  321. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  322. a -= 1;
  323. }
  324. return a;
  325. }
  326. floor_mod :: proc(x, y: $T) -> T
  327. where intrinsics.type_is_integer(T) {
  328. r := x % y;
  329. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  330. r += y;
  331. }
  332. return r;
  333. }
  334. modf_f16 :: proc(x: f16) -> (int: f16, frac: f16) {
  335. shift :: 16 - 5 - 1;
  336. mask :: 0x1f;
  337. bias :: 15;
  338. if x < 1 {
  339. switch {
  340. case x < 0:
  341. int, frac = modf(-x);
  342. return -int, -frac;
  343. case x == 0:
  344. return x, x;
  345. }
  346. return 0, x;
  347. }
  348. i := transmute(u16)x;
  349. e := uint(i>>shift)&mask - bias;
  350. if e < shift {
  351. i &~= 1<<(shift-e) - 1;
  352. }
  353. int = transmute(f16)i;
  354. frac = x - int;
  355. return;
  356. }
  357. modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
  358. shift :: 32 - 8 - 1;
  359. mask :: 0xff;
  360. bias :: 127;
  361. if x < 1 {
  362. switch {
  363. case x < 0:
  364. int, frac = modf(-x);
  365. return -int, -frac;
  366. case x == 0:
  367. return x, x;
  368. }
  369. return 0, x;
  370. }
  371. i := transmute(u32)x;
  372. e := uint(i>>shift)&mask - bias;
  373. if e < shift {
  374. i &~= 1<<(shift-e) - 1;
  375. }
  376. int = transmute(f32)i;
  377. frac = x - int;
  378. return;
  379. }
  380. modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
  381. shift :: 64 - 11 - 1;
  382. mask :: 0x7ff;
  383. bias :: 1023;
  384. if x < 1 {
  385. switch {
  386. case x < 0:
  387. int, frac = modf(-x);
  388. return -int, -frac;
  389. case x == 0:
  390. return x, x;
  391. }
  392. return 0, x;
  393. }
  394. i := transmute(u64)x;
  395. e := uint(i>>shift)&mask - bias;
  396. if e < shift {
  397. i &~= 1<<(shift-e) - 1;
  398. }
  399. int = transmute(f64)i;
  400. frac = x - int;
  401. return;
  402. }
  403. modf :: proc{modf_f16, modf_f32, modf_f64};
  404. split_decimal :: modf;
  405. mod_f16 :: proc(x, y: f16) -> (n: f16) {
  406. z := abs(y);
  407. n = remainder(abs(x), z);
  408. if sign(n) < 0 {
  409. n += z;
  410. }
  411. return copy_sign(n, x);
  412. }
  413. mod_f32 :: proc(x, y: f32) -> (n: f32) {
  414. z := abs(y);
  415. n = remainder(abs(x), z);
  416. if sign(n) < 0 {
  417. n += z;
  418. }
  419. return copy_sign(n, x);
  420. }
  421. mod_f64 :: proc(x, y: f64) -> (n: f64) {
  422. z := abs(y);
  423. n = remainder(abs(x), z);
  424. if sign(n) < 0 {
  425. n += z;
  426. }
  427. return copy_sign(n, x);
  428. }
  429. mod :: proc{mod_f16, mod_f32, mod_f64};
  430. remainder_f16 :: proc(x, y: f16) -> f16 { return x - round(x/y) * y; }
  431. remainder_f32 :: proc(x, y: f32) -> f32 { return x - round(x/y) * y; }
  432. remainder_f64 :: proc(x, y: f64) -> f64 { return x - round(x/y) * y; }
  433. remainder :: proc{remainder_f16, remainder_f32, remainder_f64};
  434. gcd :: proc(x, y: $T) -> T
  435. where intrinsics.type_is_ordered_numeric(T) {
  436. x, y := x, y;
  437. for y != 0 {
  438. x %= y;
  439. x, y = y, x;
  440. }
  441. return abs(x);
  442. }
  443. lcm :: proc(x, y: $T) -> T
  444. where intrinsics.type_is_ordered_numeric(T) {
  445. return x / gcd(x, y) * y;
  446. }
  447. frexp_f16 :: proc(x: f16) -> (significand: f16, exponent: int) {
  448. f, e := frexp_f64(f64(x));
  449. return f16(f), e;
  450. }
  451. frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
  452. f, e := frexp_f64(f64(x));
  453. return f32(f), e;
  454. }
  455. frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
  456. switch {
  457. case x == 0:
  458. return 0, 0;
  459. case x < 0:
  460. significand, exponent = frexp(-x);
  461. return -significand, exponent;
  462. }
  463. ex := trunc(log2(x));
  464. exponent = int(ex);
  465. significand = x / pow(2.0, ex);
  466. if abs(significand) >= 1 {
  467. exponent += 1;
  468. significand /= 2;
  469. }
  470. if exponent == 1024 && significand == 0 {
  471. significand = 0.99999999999999988898;
  472. }
  473. return;
  474. }
  475. frexp :: proc{frexp_f16, frexp_f32, frexp_f64};
  476. binomial :: proc(n, k: int) -> int {
  477. switch {
  478. case k <= 0: return 1;
  479. case 2*k > n: return binomial(n, n-k);
  480. }
  481. b := n;
  482. for i in 2..<k {
  483. b = (b * (n+1-i))/i;
  484. }
  485. return b;
  486. }
  487. factorial :: proc(n: int) -> int {
  488. when size_of(int) == size_of(i64) {
  489. @static table := [21]int{
  490. 1,
  491. 1,
  492. 2,
  493. 6,
  494. 24,
  495. 120,
  496. 720,
  497. 5_040,
  498. 40_320,
  499. 362_880,
  500. 3_628_800,
  501. 39_916_800,
  502. 479_001_600,
  503. 6_227_020_800,
  504. 87_178_291_200,
  505. 1_307_674_368_000,
  506. 20_922_789_888_000,
  507. 355_687_428_096_000,
  508. 6_402_373_705_728_000,
  509. 121_645_100_408_832_000,
  510. 2_432_902_008_176_640_000,
  511. };
  512. } else {
  513. @static table := [13]int{
  514. 1,
  515. 1,
  516. 2,
  517. 6,
  518. 24,
  519. 120,
  520. 720,
  521. 5_040,
  522. 40_320,
  523. 362_880,
  524. 3_628_800,
  525. 39_916_800,
  526. 479_001_600,
  527. };
  528. }
  529. assert(n >= 0, "parameter must not be negative");
  530. assert(n < len(table), "parameter is too large to lookup in the table");
  531. return table[n];
  532. }
  533. classify_f16 :: proc(x: f16) -> Float_Class {
  534. switch {
  535. case x == 0:
  536. i := transmute(i16)x;
  537. if i < 0 {
  538. return .Neg_Zero;
  539. }
  540. return .Zero;
  541. case x*0.5 == x:
  542. if x < 0 {
  543. return .Neg_Inf;
  544. }
  545. return .Inf;
  546. case !(x == x):
  547. return .NaN;
  548. }
  549. u := transmute(u16)x;
  550. exp := int(u>>10) & (1<<5 - 1);
  551. if exp == 0 {
  552. return .Subnormal;
  553. }
  554. return .Normal;
  555. }
  556. classify_f32 :: proc(x: f32) -> Float_Class {
  557. switch {
  558. case x == 0:
  559. i := transmute(i32)x;
  560. if i < 0 {
  561. return .Neg_Zero;
  562. }
  563. return .Zero;
  564. case x*0.5 == x:
  565. if x < 0 {
  566. return .Neg_Inf;
  567. }
  568. return .Inf;
  569. case !(x == x):
  570. return .NaN;
  571. }
  572. u := transmute(u32)x;
  573. exp := int(u>>23) & (1<<8 - 1);
  574. if exp == 0 {
  575. return .Subnormal;
  576. }
  577. return .Normal;
  578. }
  579. classify_f64 :: proc(x: f64) -> Float_Class {
  580. switch {
  581. case x == 0:
  582. i := transmute(i64)x;
  583. if i < 0 {
  584. return .Neg_Zero;
  585. }
  586. return .Zero;
  587. case x*0.5 == x:
  588. if x < 0 {
  589. return .Neg_Inf;
  590. }
  591. return .Inf;
  592. case !(x == x):
  593. return .NaN;
  594. }
  595. u := transmute(u64)x;
  596. exp := int(u>>52) & (1<<11 - 1);
  597. if exp == 0 {
  598. return .Subnormal;
  599. }
  600. return .Normal;
  601. }
  602. classify :: proc{classify_f16, classify_f32, classify_f64};
  603. is_nan_f16 :: proc(x: f16) -> bool { return classify(x) == .NaN; }
  604. is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN; }
  605. is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN; }
  606. is_nan :: proc{is_nan_f16, is_nan_f32, is_nan_f64};
  607. // is_inf reports whether f is an infinity, according to sign.
  608. // If sign > 0, is_inf reports whether f is positive infinity.
  609. // If sign < 0, is_inf reports whether f is negative infinity.
  610. // If sign == 0, is_inf reports whether f is either infinity.
  611. is_inf_f16 :: proc(x: f16, sign: int = 0) -> bool {
  612. class := classify(abs(x));
  613. switch {
  614. case sign > 0:
  615. return class == .Inf;
  616. case sign < 0:
  617. return class == .Neg_Inf;
  618. }
  619. return class == .Inf || class == .Neg_Inf;
  620. }
  621. is_inf_f32 :: proc(x: f32, sign: int = 0) -> bool {
  622. class := classify(abs(x));
  623. switch {
  624. case sign > 0:
  625. return class == .Inf;
  626. case sign < 0:
  627. return class == .Neg_Inf;
  628. }
  629. return class == .Inf || class == .Neg_Inf;
  630. }
  631. is_inf_f64 :: proc(x: f64, sign: int = 0) -> bool {
  632. class := classify(abs(x));
  633. switch {
  634. case sign > 0:
  635. return class == .Inf;
  636. case sign < 0:
  637. return class == .Neg_Inf;
  638. }
  639. return class == .Inf || class == .Neg_Inf;
  640. }
  641. is_inf :: proc{is_inf_f16, is_inf_f32, is_inf_f64};
  642. inf_f16 :: proc(sign: int) -> f16 {
  643. return f16(inf_f16(sign));
  644. }
  645. inf_f32 :: proc(sign: int) -> f32 {
  646. return f32(inf_f64(sign));
  647. }
  648. inf_f64 :: proc(sign: int) -> f64 {
  649. v: u64;
  650. if sign >= 0 {
  651. v = 0x7ff00000_00000000;
  652. } else {
  653. v = 0xfff00000_00000000;
  654. }
  655. return transmute(f64)v;
  656. }
  657. nan_f16 :: proc() -> f16 {
  658. return f16(nan_f64());
  659. }
  660. nan_f32 :: proc() -> f32 {
  661. return f32(nan_f64());
  662. }
  663. nan_f64 :: proc() -> f64 {
  664. v: u64 = 0x7ff80000_00000001;
  665. return transmute(f64)v;
  666. }
  667. is_power_of_two :: proc(x: int) -> bool {
  668. return x > 0 && (x & (x-1)) == 0;
  669. }
  670. next_power_of_two :: proc(x: int) -> int {
  671. k := x -1;
  672. when size_of(int) == 8 {
  673. k = k | (k >> 32);
  674. }
  675. k = k | (k >> 16);
  676. k = k | (k >> 8);
  677. k = k | (k >> 4);
  678. k = k | (k >> 2);
  679. k = k | (k >> 1);
  680. k += 1 + int(x <= 0);
  681. return k;
  682. }
  683. sum :: proc(x: $T/[]$E) -> (res: E)
  684. where intrinsics.type_is_numeric(E) {
  685. for i in x {
  686. res += i;
  687. }
  688. return;
  689. }
  690. prod :: proc(x: $T/[]$E) -> (res: E)
  691. where intrinsics.type_is_numeric(E) {
  692. for i in x {
  693. res *= i;
  694. }
  695. return;
  696. }
  697. cumsum_inplace :: proc(x: $T/[]$E) -> T
  698. where intrinsics.type_is_numeric(E) {
  699. for i in 1..<len(x) {
  700. x[i] = x[i-1] + x[i];
  701. }
  702. }
  703. cumsum :: proc(dst, src: $T/[]$E) -> T
  704. where intrinsics.type_is_numeric(E) {
  705. N := min(len(dst), len(src));
  706. if N > 0 {
  707. dst[0] = src[0];
  708. for i in 1..<N {
  709. dst[i] = dst[i-1] + src[i];
  710. }
  711. }
  712. return dst[:N];
  713. }
  714. atan2_f16 :: proc(y, x: f16) -> f16 {
  715. // TODO(bill): Better atan2_f16
  716. return f16(atan2_f64(f64(y), f64(x)));
  717. }
  718. atan2_f32 :: proc(y, x: f32) -> f32 {
  719. // TODO(bill): Better atan2_f32
  720. return f32(atan2_f64(f64(y), f64(x)));
  721. }
  722. atan2_f64 :: proc(y, x: f64) -> f64 {
  723. // TODO(bill): Faster atan2_f64 if possible
  724. // The original C code:
  725. // Stephen L. Moshier
  726. // [email protected]
  727. NAN :: 0h7fff_ffff_ffff_ffff;
  728. INF :: 0h7FF0_0000_0000_0000;
  729. PI :: 0h4009_21fb_5444_2d18;
  730. atan :: proc(x: f64) -> f64 {
  731. if x == 0 {
  732. return x;
  733. }
  734. if x > 0 {
  735. return s_atan(x);
  736. }
  737. return -s_atan(-x);
  738. }
  739. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  740. s_atan :: proc(x: f64) -> f64 {
  741. MORE_BITS :: 6.123233995736765886130e-17; // pi/2 = PIO2 + MORE_BITS
  742. TAN3PI08 :: 2.41421356237309504880; // tan(3*pi/8)
  743. if x <= 0.66 {
  744. return x_atan(x);
  745. }
  746. if x > TAN3PI08 {
  747. return PI/2 - x_atan(1/x) + MORE_BITS;
  748. }
  749. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS;
  750. }
  751. // x_atan evaluates a series valid in the range [0, 0.66].
  752. x_atan :: proc(x: f64) -> f64 {
  753. P0 :: -8.750608600031904122785e-01;
  754. P1 :: -1.615753718733365076637e+01;
  755. P2 :: -7.500855792314704667340e+01;
  756. P3 :: -1.228866684490136173410e+02;
  757. P4 :: -6.485021904942025371773e+01;
  758. Q0 :: +2.485846490142306297962e+01;
  759. Q1 :: +1.650270098316988542046e+02;
  760. Q2 :: +4.328810604912902668951e+02;
  761. Q3 :: +4.853903996359136964868e+02;
  762. Q4 :: +1.945506571482613964425e+02;
  763. z := x * x;
  764. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4);
  765. z = x*z + x;
  766. return z;
  767. }
  768. switch {
  769. case is_nan(y) || is_nan(x):
  770. return NAN;
  771. case y == 0:
  772. if x >= 0 && !sign_bit(x) {
  773. return copy_sign(0.0, y);
  774. }
  775. return copy_sign(PI, y);
  776. case x == 0:
  777. return copy_sign(PI*0.5, y);
  778. case is_inf(x, 0):
  779. if is_inf(x, 1) {
  780. if is_inf(y, 0) {
  781. return copy_sign(PI*0.25, y);
  782. }
  783. return copy_sign(0, y);
  784. }
  785. if is_inf(y, 0) {
  786. return copy_sign(PI*0.75, y);
  787. }
  788. return copy_sign(PI, y);
  789. case is_inf(y, 0):
  790. return copy_sign(PI*0.5, y);
  791. }
  792. q := atan(y / x);
  793. if x < 0 {
  794. if q <= 0 {
  795. return q + PI;
  796. }
  797. return q - PI;
  798. }
  799. return q;
  800. }
  801. atan2 :: proc{atan2_f16, atan2_f32, atan2_f64};
  802. atan_f16 :: proc(x: f16) -> f16 {
  803. return atan2_f16(x, 1);
  804. }
  805. atan_f32 :: proc(x: f32) -> f32 {
  806. return atan2_f32(x, 1);
  807. }
  808. atan_f64 :: proc(x: f64) -> f64 {
  809. return atan2_f64(x, 1);
  810. }
  811. atan :: proc{atan_f16, atan_f32, atan_f64};
  812. asin_f16 :: proc(x: f16) -> f16 {
  813. return atan2_f16(x, 1 + sqrt_f16(1 - x*x));
  814. }
  815. asin_f32 :: proc(x: f32) -> f32 {
  816. return atan2_f32(x, 1 + sqrt_f32(1 - x*x));
  817. }
  818. asin_f64 :: proc(x: f64) -> f64 {
  819. return atan2_f64(x, 1 + sqrt_f64(1 - x*x));
  820. }
  821. asin :: proc{asin_f16, asin_f32, asin_f64};
  822. acos_f16 :: proc(x: f16) -> f16 {
  823. return 2 * atan2_f16(sqrt_f16(1 - x), sqrt_f16(1 + x));
  824. }
  825. acos_f32 :: proc(x: f32) -> f32 {
  826. return 2 * atan2_f32(sqrt_f32(1 - x), sqrt_f32(1 + x));
  827. }
  828. acos_f64 :: proc(x: f64) -> f64 {
  829. return 2 * atan2_f64(sqrt_f64(1 - x), sqrt_f64(1 + x));
  830. }
  831. acos :: proc{acos_f16, acos_f32, acos_f64};
  832. sinh_f16 :: proc(x: f16) -> f16 {
  833. return (exp(x) - exp(-x))*0.5;
  834. }
  835. sinh_f32 :: proc(x: f32) -> f32 {
  836. return (exp(x) - exp(-x))*0.5;
  837. }
  838. sinh_f64 :: proc(x: f64) -> f64 {
  839. return (exp(x) - exp(-x))*0.5;
  840. }
  841. sinh :: proc{sinh_f16, sinh_f32, sinh_f64};
  842. cosh_f16 :: proc(x: f16) -> f16 {
  843. return (exp(x) + exp(-x))*0.5;
  844. }
  845. cosh_f32 :: proc(x: f32) -> f32 {
  846. return (exp(x) + exp(-x))*0.5;
  847. }
  848. cosh_f64 :: proc(x: f64) -> f64 {
  849. return (exp(x) + exp(-x))*0.5;
  850. }
  851. cosh :: proc{cosh_f16, cosh_f32, cosh_f64};
  852. tanh_f16 :: proc(x: f16) -> f16 {
  853. t := exp(2*x);
  854. return (t - 1) / (t + 1);
  855. }
  856. tanh_f32 :: proc(x: f32) -> f32 {
  857. t := exp(2*x);
  858. return (t - 1) / (t + 1);
  859. }
  860. tanh_f64 :: proc(x: f64) -> f64 {
  861. t := exp(2*x);
  862. return (t - 1) / (t + 1);
  863. }
  864. tanh :: proc{tanh_f16, tanh_f32, tanh_f64};
  865. F16_DIG :: 3;
  866. F16_EPSILON :: 0.00097656;
  867. F16_GUARD :: 0;
  868. F16_MANT_DIG :: 11;
  869. F16_MAX :: 65504.0;
  870. F16_MAX_10_EXP :: 4;
  871. F16_MAX_EXP :: 15;
  872. F16_MIN :: 6.10351562e-5;
  873. F16_MIN_10_EXP :: -4;
  874. F16_MIN_EXP :: -14;
  875. F16_NORMALIZE :: 0;
  876. F16_RADIX :: 2;
  877. F16_ROUNDS :: 1;
  878. F32_DIG :: 6;
  879. F32_EPSILON :: 1.192092896e-07;
  880. F32_GUARD :: 0;
  881. F32_MANT_DIG :: 24;
  882. F32_MAX :: 3.402823466e+38;
  883. F32_MAX_10_EXP :: 38;
  884. F32_MAX_EXP :: 128;
  885. F32_MIN :: 1.175494351e-38;
  886. F32_MIN_10_EXP :: -37;
  887. F32_MIN_EXP :: -125;
  888. F32_NORMALIZE :: 0;
  889. F32_RADIX :: 2;
  890. F32_ROUNDS :: 1;
  891. F64_DIG :: 15; // # of decimal digits of precision
  892. F64_EPSILON :: 2.2204460492503131e-016; // smallest such that 1.0+F64_EPSILON != 1.0
  893. F64_MANT_DIG :: 53; // # of bits in mantissa
  894. F64_MAX :: 1.7976931348623158e+308; // max value
  895. F64_MAX_10_EXP :: 308; // max decimal exponent
  896. F64_MAX_EXP :: 1024; // max binary exponent
  897. F64_MIN :: 2.2250738585072014e-308; // min positive value
  898. F64_MIN_10_EXP :: -307; // min decimal exponent
  899. F64_MIN_EXP :: -1021; // min binary exponent
  900. F64_RADIX :: 2; // exponent radix
  901. F64_ROUNDS :: 1; // addition rounding: near