math.odin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package math
  2. import "intrinsics"
  3. Float_Class :: enum {
  4. Normal, // an ordinary nonzero floating point value
  5. Subnormal, // a subnormal floating point value
  6. Zero, // zero
  7. Neg_Zero, // the negative zero
  8. NaN, // Not-A-Number (NaN)
  9. Inf, // positive infinity
  10. Neg_Inf // negative infinity
  11. };
  12. TAU :: 6.28318530717958647692528676655900576;
  13. PI :: 3.14159265358979323846264338327950288;
  14. E :: 2.71828182845904523536;
  15. τ :: TAU;
  16. π :: PI;
  17. e :: E;
  18. SQRT_TWO :: 1.41421356237309504880168872420969808;
  19. SQRT_THREE :: 1.73205080756887729352744634150587236;
  20. SQRT_FIVE :: 2.23606797749978969640917366873127623;
  21. LN2 :: 0.693147180559945309417232121458176568;
  22. LN10 :: 2.30258509299404568401799145468436421;
  23. MAX_F64_PRECISION :: 16; // Maximum number of meaningful digits after the decimal point for 'f64'
  24. MAX_F32_PRECISION :: 8; // Maximum number of meaningful digits after the decimal point for 'f32'
  25. RAD_PER_DEG :: TAU/360.0;
  26. DEG_PER_RAD :: 360.0/TAU;
  27. @(default_calling_convention="none")
  28. foreign _ {
  29. @(link_name="llvm.sqrt.f32")
  30. sqrt_f32 :: proc(x: f32) -> f32 ---;
  31. @(link_name="llvm.sqrt.f64")
  32. sqrt_f64 :: proc(x: f64) -> f64 ---;
  33. @(link_name="llvm.sin.f32")
  34. sin_f32 :: proc(θ: f32) -> f32 ---;
  35. @(link_name="llvm.sin.f64")
  36. sin_f64 :: proc(θ: f64) -> f64 ---;
  37. @(link_name="llvm.cos.f32")
  38. cos_f32 :: proc(θ: f32) -> f32 ---;
  39. @(link_name="llvm.cos.f64")
  40. cos_f64 :: proc(θ: f64) -> f64 ---;
  41. @(link_name="llvm.pow.f32")
  42. pow_f32 :: proc(x, power: f32) -> f32 ---;
  43. @(link_name="llvm.pow.f64")
  44. pow_f64 :: proc(x, power: f64) -> f64 ---;
  45. @(link_name="llvm.fmuladd.f32")
  46. fmuladd_f32 :: proc(a, b, c: f32) -> f32 ---;
  47. @(link_name="llvm.fmuladd.f64")
  48. fmuladd_f64 :: proc(a, b, c: f64) -> f64 ---;
  49. @(link_name="llvm.log.f32")
  50. ln_f32 :: proc(x: f32) -> f32 ---;
  51. @(link_name="llvm.log.f64")
  52. ln_f64 :: proc(x: f64) -> f64 ---;
  53. @(link_name="llvm.exp.f32")
  54. exp_f32 :: proc(x: f32) -> f32 ---;
  55. @(link_name="llvm.exp.f64")
  56. exp_f64 :: proc(x: f64) -> f64 ---;
  57. }
  58. sqrt :: proc{sqrt_f32, sqrt_f64};
  59. sin :: proc{sin_f32, sin_f64};
  60. cos :: proc{cos_f32, cos_f64};
  61. pow :: proc{pow_f32, pow_f64};
  62. fmuladd :: proc{fmuladd_f32, fmuladd_f64};
  63. ln :: proc{ln_f32, ln_f64};
  64. exp :: proc{exp_f32, exp_f64};
  65. log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base); }
  66. log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base); }
  67. log :: proc{log_f32, log_f64};
  68. log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2; }
  69. log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2; }
  70. log2 :: proc{log2_f32, log2_f64};
  71. log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10; }
  72. log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10; }
  73. log10 :: proc{log10_f32, log10_f64};
  74. tan_f32 :: proc "c" (θ: f32) -> f32 { return sin(θ)/cos(θ); }
  75. tan_f64 :: proc "c" (θ: f64) -> f64 { return sin(θ)/cos(θ); }
  76. tan :: proc{tan_f32, tan_f64};
  77. lerp :: proc(a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t; }
  78. unlerp_f32 :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
  79. unlerp_f64 :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
  80. unlerp :: proc{unlerp_f32, unlerp_f64};
  81. sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
  82. sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
  83. sign :: proc{sign_f32, sign_f64};
  84. copy_sign_f32 :: proc(x, y: f32) -> f32 {
  85. ix := transmute(u32)x;
  86. iy := transmute(u32)y;
  87. ix &= 0x7fff_ffff;
  88. ix |= iy & 0x8000_0000;
  89. return transmute(f32)ix;
  90. }
  91. copy_sign_f64 :: proc(x, y: f64) -> f64 {
  92. ix := transmute(u64)x;
  93. iy := transmute(u64)y;
  94. ix &= 0x7fff_ffff_ffff_ffff;
  95. ix |= iy & 0x8000_0000_0000_0000;
  96. return transmute(f64)ix;
  97. }
  98. copy_sign :: proc{copy_sign_f32, copy_sign_f64};
  99. to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG; }
  100. to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG; }
  101. to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD; }
  102. to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD; }
  103. to_radians :: proc{to_radians_f32, to_radians_f64};
  104. to_degrees :: proc{to_degrees_f32, to_degrees_f64};
  105. trunc_f32 :: proc(x: f32) -> f32 {
  106. trunc_internal :: proc(f: f32) -> f32 {
  107. mask :: 0xff;
  108. shift :: 32 - 9;
  109. bias :: 0x7f;
  110. if f < 1 {
  111. switch {
  112. case f < 0: return -trunc_internal(-f);
  113. case f == 0: return f;
  114. case: return 0;
  115. }
  116. }
  117. x := transmute(u32)f;
  118. e := (x >> shift) & mask - bias;
  119. if e < shift {
  120. x &= ~(1 << (shift-e)) - 1;
  121. }
  122. return transmute(f32)x;
  123. }
  124. switch classify(x) {
  125. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  126. return x;
  127. }
  128. return trunc_internal(x);
  129. }
  130. trunc_f64 :: proc(x: f64) -> f64 {
  131. trunc_internal :: proc(f: f64) -> f64 {
  132. mask :: 0x7ff;
  133. shift :: 64 - 12;
  134. bias :: 0x3ff;
  135. if f < 1 {
  136. switch {
  137. case f < 0: return -trunc_internal(-f);
  138. case f == 0: return f;
  139. case: return 0;
  140. }
  141. }
  142. x := transmute(u64)f;
  143. e := (x >> shift) & mask - bias;
  144. if e < shift {
  145. x &= ~(1 << (shift-e)) - 1;
  146. }
  147. return transmute(f64)x;
  148. }
  149. switch classify(x) {
  150. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  151. return x;
  152. }
  153. return trunc_internal(x);
  154. }
  155. trunc :: proc{trunc_f32, trunc_f64};
  156. round_f32 :: proc(x: f32) -> f32 {
  157. return x < 0 ? ceil(x - 0.5) : floor(x + 0.5);
  158. }
  159. round_f64 :: proc(x: f64) -> f64 {
  160. return x < 0 ? ceil(x - 0.5) : floor(x + 0.5);
  161. }
  162. round :: proc{round_f32, round_f64};
  163. ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x); }
  164. ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x); }
  165. ceil :: proc{ceil_f32, ceil_f64};
  166. floor_f32 :: proc(x: f32) -> f32 {
  167. if x == 0 || is_nan(x) || is_inf(x) {
  168. return x;
  169. }
  170. if x < 0 {
  171. d, fract := modf(-x);
  172. if fract != 0.0 {
  173. d = d + 1;
  174. }
  175. return -d;
  176. }
  177. d, _ := modf(x);
  178. return d;
  179. }
  180. floor_f64 :: proc(x: f64) -> f64 {
  181. if x == 0 || is_nan(x) || is_inf(x) {
  182. return x;
  183. }
  184. if x < 0 {
  185. d, fract := modf(-x);
  186. if fract != 0.0 {
  187. d = d + 1;
  188. }
  189. return -d;
  190. }
  191. d, _ := modf(x);
  192. return d;
  193. }
  194. floor :: proc{floor_f32, floor_f64};
  195. floor_div :: proc(x, y: $T) -> T
  196. where intrinsics.type_is_integer(T) {
  197. a := x / y;
  198. r := x % y;
  199. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  200. a -= 1;
  201. }
  202. return a;
  203. }
  204. floor_mod :: proc(x, y: $T) -> T
  205. where intrinsics.type_is_integer(T) {
  206. r := x % y;
  207. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  208. r += y;
  209. }
  210. return r;
  211. }
  212. modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
  213. shift :: 32 - 8 - 1;
  214. mask :: 0xff;
  215. bias :: 127;
  216. if x < 1 {
  217. switch {
  218. case x < 0:
  219. int, frac = modf(-x);
  220. return -int, -frac;
  221. case x == 0:
  222. return x, x;
  223. }
  224. return 0, x;
  225. }
  226. i := transmute(u32)x;
  227. e := uint(i>>shift)&mask - bias;
  228. if e < shift {
  229. i &~= 1<<(shift-e) - 1;
  230. }
  231. int = transmute(f32)i;
  232. frac = x - int;
  233. return;
  234. }
  235. modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
  236. shift :: 64 - 11 - 1;
  237. mask :: 0x7ff;
  238. bias :: 1023;
  239. if x < 1 {
  240. switch {
  241. case x < 0:
  242. int, frac = modf(-x);
  243. return -int, -frac;
  244. case x == 0:
  245. return x, x;
  246. }
  247. return 0, x;
  248. }
  249. i := transmute(u64)x;
  250. e := uint(i>>shift)&mask - bias;
  251. if e < shift {
  252. i &~= 1<<(shift-e) - 1;
  253. }
  254. int = transmute(f64)i;
  255. frac = x - int;
  256. return;
  257. }
  258. modf :: proc{modf_f32, modf_f64};
  259. split_decimal :: modf;
  260. mod_f32 :: proc(x, y: f32) -> (n: f32) {
  261. z := abs(y);
  262. n = remainder(abs(x), z);
  263. if sign(n) < 0 {
  264. n += z;
  265. }
  266. return copy_sign(n, x);
  267. }
  268. mod_f64 :: proc(x, y: f64) -> (n: f64) {
  269. z := abs(y);
  270. n = remainder(abs(x), z);
  271. if sign(n) < 0 {
  272. n += z;
  273. }
  274. return copy_sign(n, x);
  275. }
  276. mod :: proc{mod_f32, mod_f64};
  277. remainder_f32 :: proc(x, y: f32) -> f32 { return x - round(x/y) * y; }
  278. remainder_f64 :: proc(x, y: f64) -> f64 { return x - round(x/y) * y; }
  279. remainder :: proc{remainder_f32, remainder_f64};
  280. gcd :: proc(x, y: $T) -> T
  281. where intrinsics.type_is_ordered_numeric(T) {
  282. x, y := x, y;
  283. for y != 0 {
  284. x %= y;
  285. x, y = y, x;
  286. }
  287. return abs(x);
  288. }
  289. lcm :: proc(x, y: $T) -> T
  290. where intrinsics.type_is_ordered_numeric(T) {
  291. return x / gcd(x, y) * y;
  292. }
  293. frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
  294. switch {
  295. case x == 0:
  296. return 0, 0;
  297. case x < 0:
  298. significand, exponent = frexp(-x);
  299. return -significand, exponent;
  300. }
  301. ex := trunc(log2(x));
  302. exponent = int(ex);
  303. significand = x / pow(2.0, ex);
  304. if abs(significand) >= 1 {
  305. exponent += 1;
  306. significand /= 2;
  307. }
  308. if exponent == 1024 && significand == 0 {
  309. significand = 0.99999999999999988898;
  310. }
  311. return;
  312. }
  313. frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
  314. switch {
  315. case x == 0:
  316. return 0, 0;
  317. case x < 0:
  318. significand, exponent = frexp(-x);
  319. return -significand, exponent;
  320. }
  321. ex := trunc(log2(x));
  322. exponent = int(ex);
  323. significand = x / pow(2.0, ex);
  324. if abs(significand) >= 1 {
  325. exponent += 1;
  326. significand /= 2;
  327. }
  328. if exponent == 1024 && significand == 0 {
  329. significand = 0.99999999999999988898;
  330. }
  331. return;
  332. }
  333. frexp :: proc{frexp_f32, frexp_f64};
  334. binomial :: proc(n, k: int) -> int {
  335. switch {
  336. case k <= 0: return 1;
  337. case 2*k > n: return binomial(n, n-k);
  338. }
  339. b := n;
  340. for i in 2..<k {
  341. b = (b * (n+1-i))/i;
  342. }
  343. return b;
  344. }
  345. factorial :: proc(n: int) -> int {
  346. when size_of(int) == size_of(i64) {
  347. @static table := [21]int{
  348. 1,
  349. 1,
  350. 2,
  351. 6,
  352. 24,
  353. 120,
  354. 720,
  355. 5_040,
  356. 40_320,
  357. 362_880,
  358. 3_628_800,
  359. 39_916_800,
  360. 479_001_600,
  361. 6_227_020_800,
  362. 87_178_291_200,
  363. 1_307_674_368_000,
  364. 20_922_789_888_000,
  365. 355_687_428_096_000,
  366. 6_402_373_705_728_000,
  367. 121_645_100_408_832_000,
  368. 2_432_902_008_176_640_000,
  369. };
  370. } else {
  371. @static table := [13]int{
  372. 1,
  373. 1,
  374. 2,
  375. 6,
  376. 24,
  377. 120,
  378. 720,
  379. 5_040,
  380. 40_320,
  381. 362_880,
  382. 3_628_800,
  383. 39_916_800,
  384. 479_001_600,
  385. };
  386. }
  387. assert(n >= 0, "parameter must not be negative");
  388. assert(n < len(table), "parameter is too large to lookup in the table");
  389. return 0;
  390. }
  391. classify_f32 :: proc(x: f32) -> Float_Class {
  392. switch {
  393. case x == 0:
  394. i := transmute(i32)x;
  395. if i < 0 {
  396. return .Neg_Zero;
  397. }
  398. return .Zero;
  399. case x*0.5 == x:
  400. if x < 0 {
  401. return .Neg_Inf;
  402. }
  403. return .Inf;
  404. case x != x:
  405. return .NaN;
  406. }
  407. u := transmute(u32)x;
  408. exp := int(u>>23) & (1<<8 - 1);
  409. if exp == 0 {
  410. return .Subnormal;
  411. }
  412. return .Normal;
  413. }
  414. classify_f64 :: proc(x: f64) -> Float_Class {
  415. switch {
  416. case x == 0:
  417. i := transmute(i64)x;
  418. if i < 0 {
  419. return .Neg_Zero;
  420. }
  421. return .Zero;
  422. case x*0.5 == x:
  423. if x < 0 {
  424. return .Neg_Inf;
  425. }
  426. return .Inf;
  427. case x != x:
  428. return .NaN;
  429. }
  430. u := transmute(u64)x;
  431. exp := int(u>>52) & (1<<11 - 1);
  432. if exp == 0 {
  433. return .Subnormal;
  434. }
  435. return .Normal;
  436. }
  437. classify :: proc{classify_f32, classify_f64};
  438. is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN; }
  439. is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN; }
  440. is_nan :: proc{is_nan_f32, is_nan_f64};
  441. is_inf_f32 :: proc(x: f32) -> bool { return classify(abs(x)) == .Inf; }
  442. is_inf_f64 :: proc(x: f64) -> bool { return classify(abs(x)) == .Inf; }
  443. is_inf :: proc{is_inf_f32, is_inf_f64};
  444. is_power_of_two :: proc(x: int) -> bool {
  445. return x > 0 && (x & (x-1)) == 0;
  446. }
  447. next_power_of_two :: proc(x: int) -> int {
  448. k := x -1;
  449. when size_of(int) == 8 {
  450. k = k | (k >> 32);
  451. }
  452. k = k | (k >> 16);
  453. k = k | (k >> 8);
  454. k = k | (k >> 4);
  455. k = k | (k >> 2);
  456. k = k | (k >> 1);
  457. k += 1 + int(x <= 0);
  458. return k;
  459. }
  460. sum :: proc(x: $T/[]$E) -> (res: E)
  461. where intrinsics.BuiltinProc_type_is_numeric(E) {
  462. for i in x {
  463. res += i;
  464. }
  465. return;
  466. }
  467. prod :: proc(x: $T/[]$E) -> (res: E)
  468. where intrinsics.BuiltinProc_type_is_numeric(E) {
  469. for i in x {
  470. res *= i;
  471. }
  472. return;
  473. }
  474. cumsum_inplace :: proc(x: $T/[]$E) -> T
  475. where intrinsics.BuiltinProc_type_is_numeric(E) {
  476. for i in 1..<len(x) {
  477. x[i] = x[i-1] + x[i];
  478. }
  479. }
  480. cumsum :: proc(dst, src: $T/[]$E) -> T
  481. where intrinsics.BuiltinProc_type_is_numeric(E) {
  482. N := min(len(dst), len(src));
  483. if N > 0 {
  484. dst[0] = src[0];
  485. for i in 1..<N {
  486. dst[i] = dst[i-1] + src[i];
  487. }
  488. }
  489. return dst[:N];
  490. }
  491. F32_DIG :: 6;
  492. F32_EPSILON :: 1.192092896e-07;
  493. F32_GUARD :: 0;
  494. F32_MANT_DIG :: 24;
  495. F32_MAX :: 3.402823466e+38;
  496. F32_MAX_10_EXP :: 38;
  497. F32_MAX_EXP :: 128;
  498. F32_MIN :: 1.175494351e-38;
  499. F32_MIN_10_EXP :: -37;
  500. F32_MIN_EXP :: -125;
  501. F32_NORMALIZE :: 0;
  502. F32_RADIX :: 2;
  503. F32_ROUNDS :: 1;
  504. F64_DIG :: 15; // # of decimal digits of precision
  505. F64_EPSILON :: 2.2204460492503131e-016; // smallest such that 1.0+F64_EPSILON != 1.0
  506. F64_MANT_DIG :: 53; // # of bits in mantissa
  507. F64_MAX :: 1.7976931348623158e+308; // max value
  508. F64_MAX_10_EXP :: 308; // max decimal exponent
  509. F64_MAX_EXP :: 1024; // max binary exponent
  510. F64_MIN :: 2.2250738585072014e-308; // min positive value
  511. F64_MIN_10_EXP :: -307; // min decimal exponent
  512. F64_MIN_EXP :: -1021; // min binary exponent
  513. F64_RADIX :: 2; // exponent radix
  514. F64_ROUNDS :: 1; // addition rounding: near