math.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. @(link_name="llvm.ldexp.f32")
  58. ldexp_f32 :: proc(val: f32, exp: i32) -> f32 ---;
  59. @(link_name="llvm.ldexp.f64")
  60. ldexp_f64 :: proc(val: f64, exp: i32) -> f64 ---;
  61. }
  62. sqrt :: proc{sqrt_f32, sqrt_f64};
  63. sin :: proc{sin_f32, sin_f64};
  64. cos :: proc{cos_f32, cos_f64};
  65. pow :: proc{pow_f32, pow_f64};
  66. fmuladd :: proc{fmuladd_f32, fmuladd_f64};
  67. ln :: proc{ln_f32, ln_f64};
  68. exp :: proc{exp_f32, exp_f64};
  69. ldexp :: proc{ldexp_f32, ldexp_f64};
  70. log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base); }
  71. log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base); }
  72. log :: proc{log_f32, log_f64};
  73. log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2; }
  74. log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2; }
  75. log2 :: proc{log2_f32, log2_f64};
  76. log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10; }
  77. log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10; }
  78. log10 :: proc{log10_f32, log10_f64};
  79. tan_f32 :: proc "c" (θ: f32) -> f32 { return sin(θ)/cos(θ); }
  80. tan_f64 :: proc "c" (θ: f64) -> f64 { return sin(θ)/cos(θ); }
  81. tan :: proc{tan_f32, tan_f64};
  82. lerp :: proc(a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t; }
  83. unlerp_f32 :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
  84. unlerp_f64 :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
  85. unlerp :: proc{unlerp_f32, unlerp_f64};
  86. wrap :: proc(x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  87. tmp := mod(x, y);
  88. return tmp < 0 ? wrap + tmp : tmp;
  89. }
  90. angle_diff :: proc(a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  91. dist := wrap(b - a, TAU);
  92. return wrap(dist*2, TAU) - dist;
  93. }
  94. angle_lerp :: proc(a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  95. return a + angle_diff(a, b) * t;
  96. }
  97. step :: proc(edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  98. return x < edge ? 0 : 1;
  99. }
  100. smoothstep :: proc(edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  101. t := clamp((x - edge0) / (edge1 - edge0), 0, 1);
  102. return t * t * (3 - 2*t);
  103. }
  104. bias :: proc(t, b: $T) -> T where intrinsics.type_is_numeric(T) {
  105. return t / (((1/b) - 2) * (1 - t) + 1);
  106. }
  107. gain :: proc(t, g: $T) -> T where intrinsics.type_is_numeric(T) {
  108. if t < 0.5 {
  109. return bias(t*2, g)*0.5;
  110. }
  111. return bias(t*2 - 1, 1 - g)*0.5 + 0.5;
  112. }
  113. sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
  114. sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
  115. sign :: proc{sign_f32, sign_f64};
  116. sign_bit_f32 :: proc(x: f32) -> bool {
  117. return (transmute(u32)x) & (1<<31) != 0;
  118. }
  119. sign_bit_f64 :: proc(x: f64) -> bool {
  120. return (transmute(u64)x) & (1<<63) != 0;
  121. }
  122. sign_bit :: proc{sign_bit_f32, sign_bit_f64};
  123. copy_sign_f32 :: proc(x, y: f32) -> f32 {
  124. ix := transmute(u32)x;
  125. iy := transmute(u32)y;
  126. ix &= 0x7fff_ffff;
  127. ix |= iy & 0x8000_0000;
  128. return transmute(f32)ix;
  129. }
  130. copy_sign_f64 :: proc(x, y: f64) -> f64 {
  131. ix := transmute(u64)x;
  132. iy := transmute(u64)y;
  133. ix &= 0x7fff_ffff_ffff_ffff;
  134. ix |= iy & 0x8000_0000_0000_0000;
  135. return transmute(f64)ix;
  136. }
  137. copy_sign :: proc{copy_sign_f32, copy_sign_f64};
  138. to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG; }
  139. to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG; }
  140. to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD; }
  141. to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD; }
  142. to_radians :: proc{to_radians_f32, to_radians_f64};
  143. to_degrees :: proc{to_degrees_f32, to_degrees_f64};
  144. trunc_f32 :: proc(x: f32) -> f32 {
  145. trunc_internal :: proc(f: f32) -> f32 {
  146. mask :: 0xff;
  147. shift :: 32 - 9;
  148. bias :: 0x7f;
  149. if f < 1 {
  150. switch {
  151. case f < 0: return -trunc_internal(-f);
  152. case f == 0: return f;
  153. case: return 0;
  154. }
  155. }
  156. x := transmute(u32)f;
  157. e := (x >> shift) & mask - bias;
  158. if e < shift {
  159. x &= ~(1 << (shift-e)) - 1;
  160. }
  161. return transmute(f32)x;
  162. }
  163. switch classify(x) {
  164. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  165. return x;
  166. case .Normal, .Subnormal: // carry on
  167. }
  168. return trunc_internal(x);
  169. }
  170. trunc_f64 :: proc(x: f64) -> f64 {
  171. trunc_internal :: proc(f: f64) -> f64 {
  172. mask :: 0x7ff;
  173. shift :: 64 - 12;
  174. bias :: 0x3ff;
  175. if f < 1 {
  176. switch {
  177. case f < 0: return -trunc_internal(-f);
  178. case f == 0: return f;
  179. case: return 0;
  180. }
  181. }
  182. x := transmute(u64)f;
  183. e := (x >> shift) & mask - bias;
  184. if e < shift {
  185. x &= ~(1 << (shift-e)) - 1;
  186. }
  187. return transmute(f64)x;
  188. }
  189. switch classify(x) {
  190. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  191. return x;
  192. case .Normal, .Subnormal: // carry on
  193. }
  194. return trunc_internal(x);
  195. }
  196. trunc :: proc{trunc_f32, trunc_f64};
  197. round_f32 :: proc(x: f32) -> f32 {
  198. return x < 0 ? ceil(x - 0.5) : floor(x + 0.5);
  199. }
  200. round_f64 :: proc(x: f64) -> f64 {
  201. return x < 0 ? ceil(x - 0.5) : floor(x + 0.5);
  202. }
  203. round :: proc{round_f32, round_f64};
  204. ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x); }
  205. ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x); }
  206. ceil :: proc{ceil_f32, ceil_f64};
  207. floor_f32 :: proc(x: f32) -> f32 {
  208. if x == 0 || is_nan(x) || is_inf(x) {
  209. return x;
  210. }
  211. if x < 0 {
  212. d, fract := modf(-x);
  213. if fract != 0.0 {
  214. d = d + 1;
  215. }
  216. return -d;
  217. }
  218. d, _ := modf(x);
  219. return d;
  220. }
  221. floor_f64 :: proc(x: f64) -> f64 {
  222. if x == 0 || is_nan(x) || is_inf(x) {
  223. return x;
  224. }
  225. if x < 0 {
  226. d, fract := modf(-x);
  227. if fract != 0.0 {
  228. d = d + 1;
  229. }
  230. return -d;
  231. }
  232. d, _ := modf(x);
  233. return d;
  234. }
  235. floor :: proc{floor_f32, floor_f64};
  236. floor_div :: proc(x, y: $T) -> T
  237. where intrinsics.type_is_integer(T) {
  238. a := x / y;
  239. r := x % y;
  240. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  241. a -= 1;
  242. }
  243. return a;
  244. }
  245. floor_mod :: proc(x, y: $T) -> T
  246. where intrinsics.type_is_integer(T) {
  247. r := x % y;
  248. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  249. r += y;
  250. }
  251. return r;
  252. }
  253. modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
  254. shift :: 32 - 8 - 1;
  255. mask :: 0xff;
  256. bias :: 127;
  257. if x < 1 {
  258. switch {
  259. case x < 0:
  260. int, frac = modf(-x);
  261. return -int, -frac;
  262. case x == 0:
  263. return x, x;
  264. }
  265. return 0, x;
  266. }
  267. i := transmute(u32)x;
  268. e := uint(i>>shift)&mask - bias;
  269. if e < shift {
  270. i &~= 1<<(shift-e) - 1;
  271. }
  272. int = transmute(f32)i;
  273. frac = x - int;
  274. return;
  275. }
  276. modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
  277. shift :: 64 - 11 - 1;
  278. mask :: 0x7ff;
  279. bias :: 1023;
  280. if x < 1 {
  281. switch {
  282. case x < 0:
  283. int, frac = modf(-x);
  284. return -int, -frac;
  285. case x == 0:
  286. return x, x;
  287. }
  288. return 0, x;
  289. }
  290. i := transmute(u64)x;
  291. e := uint(i>>shift)&mask - bias;
  292. if e < shift {
  293. i &~= 1<<(shift-e) - 1;
  294. }
  295. int = transmute(f64)i;
  296. frac = x - int;
  297. return;
  298. }
  299. modf :: proc{modf_f32, modf_f64};
  300. split_decimal :: modf;
  301. mod_f32 :: proc(x, y: f32) -> (n: f32) {
  302. z := abs(y);
  303. n = remainder(abs(x), z);
  304. if sign(n) < 0 {
  305. n += z;
  306. }
  307. return copy_sign(n, x);
  308. }
  309. mod_f64 :: proc(x, y: f64) -> (n: f64) {
  310. z := abs(y);
  311. n = remainder(abs(x), z);
  312. if sign(n) < 0 {
  313. n += z;
  314. }
  315. return copy_sign(n, x);
  316. }
  317. mod :: proc{mod_f32, mod_f64};
  318. remainder_f32 :: proc(x, y: f32) -> f32 { return x - round(x/y) * y; }
  319. remainder_f64 :: proc(x, y: f64) -> f64 { return x - round(x/y) * y; }
  320. remainder :: proc{remainder_f32, remainder_f64};
  321. gcd :: proc(x, y: $T) -> T
  322. where intrinsics.type_is_ordered_numeric(T) {
  323. x, y := x, y;
  324. for y != 0 {
  325. x %= y;
  326. x, y = y, x;
  327. }
  328. return abs(x);
  329. }
  330. lcm :: proc(x, y: $T) -> T
  331. where intrinsics.type_is_ordered_numeric(T) {
  332. return x / gcd(x, y) * y;
  333. }
  334. frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
  335. switch {
  336. case x == 0:
  337. return 0, 0;
  338. case x < 0:
  339. significand, exponent = frexp(-x);
  340. return -significand, exponent;
  341. }
  342. ex := trunc(log2(x));
  343. exponent = int(ex);
  344. significand = x / pow(2.0, ex);
  345. if abs(significand) >= 1 {
  346. exponent += 1;
  347. significand /= 2;
  348. }
  349. if exponent == 1024 && significand == 0 {
  350. significand = 0.99999999999999988898;
  351. }
  352. return;
  353. }
  354. frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
  355. switch {
  356. case x == 0:
  357. return 0, 0;
  358. case x < 0:
  359. significand, exponent = frexp(-x);
  360. return -significand, exponent;
  361. }
  362. ex := trunc(log2(x));
  363. exponent = int(ex);
  364. significand = x / pow(2.0, ex);
  365. if abs(significand) >= 1 {
  366. exponent += 1;
  367. significand /= 2;
  368. }
  369. if exponent == 1024 && significand == 0 {
  370. significand = 0.99999999999999988898;
  371. }
  372. return;
  373. }
  374. frexp :: proc{frexp_f32, frexp_f64};
  375. binomial :: proc(n, k: int) -> int {
  376. switch {
  377. case k <= 0: return 1;
  378. case 2*k > n: return binomial(n, n-k);
  379. }
  380. b := n;
  381. for i in 2..<k {
  382. b = (b * (n+1-i))/i;
  383. }
  384. return b;
  385. }
  386. factorial :: proc(n: int) -> int {
  387. when size_of(int) == size_of(i64) {
  388. @static table := [21]int{
  389. 1,
  390. 1,
  391. 2,
  392. 6,
  393. 24,
  394. 120,
  395. 720,
  396. 5_040,
  397. 40_320,
  398. 362_880,
  399. 3_628_800,
  400. 39_916_800,
  401. 479_001_600,
  402. 6_227_020_800,
  403. 87_178_291_200,
  404. 1_307_674_368_000,
  405. 20_922_789_888_000,
  406. 355_687_428_096_000,
  407. 6_402_373_705_728_000,
  408. 121_645_100_408_832_000,
  409. 2_432_902_008_176_640_000,
  410. };
  411. } else {
  412. @static table := [13]int{
  413. 1,
  414. 1,
  415. 2,
  416. 6,
  417. 24,
  418. 120,
  419. 720,
  420. 5_040,
  421. 40_320,
  422. 362_880,
  423. 3_628_800,
  424. 39_916_800,
  425. 479_001_600,
  426. };
  427. }
  428. assert(n >= 0, "parameter must not be negative");
  429. assert(n < len(table), "parameter is too large to lookup in the table");
  430. return 0;
  431. }
  432. classify_f32 :: proc(x: f32) -> Float_Class {
  433. switch {
  434. case x == 0:
  435. i := transmute(i32)x;
  436. if i < 0 {
  437. return .Neg_Zero;
  438. }
  439. return .Zero;
  440. case x*0.5 == x:
  441. if x < 0 {
  442. return .Neg_Inf;
  443. }
  444. return .Inf;
  445. case !(x == x):
  446. return .NaN;
  447. }
  448. u := transmute(u32)x;
  449. exp := int(u>>23) & (1<<8 - 1);
  450. if exp == 0 {
  451. return .Subnormal;
  452. }
  453. return .Normal;
  454. }
  455. classify_f64 :: proc(x: f64) -> Float_Class {
  456. switch {
  457. case x == 0:
  458. i := transmute(i64)x;
  459. if i < 0 {
  460. return .Neg_Zero;
  461. }
  462. return .Zero;
  463. case x*0.5 == x:
  464. if x < 0 {
  465. return .Neg_Inf;
  466. }
  467. return .Inf;
  468. case !(x == x):
  469. return .NaN;
  470. }
  471. u := transmute(u64)x;
  472. exp := int(u>>52) & (1<<11 - 1);
  473. if exp == 0 {
  474. return .Subnormal;
  475. }
  476. return .Normal;
  477. }
  478. classify :: proc{classify_f32, classify_f64};
  479. is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN; }
  480. is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN; }
  481. is_nan :: proc{is_nan_f32, is_nan_f64};
  482. // is_inf reports whether f is an infinity, according to sign.
  483. // If sign > 0, is_inf reports whether f is positive infinity.
  484. // If sign < 0, is_inf reports whether f is negative infinity.
  485. // If sign == 0, is_inf reports whether f is either infinity.
  486. is_inf_f32 :: proc(x: f32, sign: int = 0) -> bool {
  487. class := classify(abs(x));
  488. switch {
  489. case sign > 0:
  490. return class == .Inf;
  491. case sign < 0:
  492. return class == .Neg_Inf;
  493. }
  494. return class == .Inf || class == .Neg_Inf;
  495. }
  496. is_inf_f64 :: proc(x: f64, sign: int = 0) -> bool {
  497. class := classify(abs(x));
  498. switch {
  499. case sign > 0:
  500. return class == .Inf;
  501. case sign < 0:
  502. return class == .Neg_Inf;
  503. }
  504. return class == .Inf || class == .Neg_Inf;
  505. }
  506. is_inf :: proc{is_inf_f32, is_inf_f64};
  507. is_power_of_two :: proc(x: int) -> bool {
  508. return x > 0 && (x & (x-1)) == 0;
  509. }
  510. next_power_of_two :: proc(x: int) -> int {
  511. k := x -1;
  512. when size_of(int) == 8 {
  513. k = k | (k >> 32);
  514. }
  515. k = k | (k >> 16);
  516. k = k | (k >> 8);
  517. k = k | (k >> 4);
  518. k = k | (k >> 2);
  519. k = k | (k >> 1);
  520. k += 1 + int(x <= 0);
  521. return k;
  522. }
  523. sum :: proc(x: $T/[]$E) -> (res: E)
  524. where intrinsics.BuiltinProc_type_is_numeric(E) {
  525. for i in x {
  526. res += i;
  527. }
  528. return;
  529. }
  530. prod :: proc(x: $T/[]$E) -> (res: E)
  531. where intrinsics.BuiltinProc_type_is_numeric(E) {
  532. for i in x {
  533. res *= i;
  534. }
  535. return;
  536. }
  537. cumsum_inplace :: proc(x: $T/[]$E) -> T
  538. where intrinsics.BuiltinProc_type_is_numeric(E) {
  539. for i in 1..<len(x) {
  540. x[i] = x[i-1] + x[i];
  541. }
  542. }
  543. cumsum :: proc(dst, src: $T/[]$E) -> T
  544. where intrinsics.BuiltinProc_type_is_numeric(E) {
  545. N := min(len(dst), len(src));
  546. if N > 0 {
  547. dst[0] = src[0];
  548. for i in 1..<N {
  549. dst[i] = dst[i-1] + src[i];
  550. }
  551. }
  552. return dst[:N];
  553. }
  554. atan2_f32 :: proc(y, x: f32) -> f32 {
  555. // TODO(bill): Better atan2_f32
  556. return f32(atan2_f64(f64(y), f64(x)));
  557. }
  558. atan2_f64 :: proc(y, x: f64) -> f64 {
  559. // TODO(bill): Faster atan2_f64 if possible
  560. // The original C code:
  561. // Stephen L. Moshier
  562. // [email protected]
  563. NAN :: 0h7fff_ffff_ffff_ffff;
  564. INF :: 0h7FF0_0000_0000_0000;
  565. PI :: 0h4009_21fb_5444_2d18;
  566. atan :: proc(x: f64) -> f64 {
  567. if x == 0 {
  568. return x;
  569. }
  570. if x > 0 {
  571. return s_atan(x);
  572. }
  573. return -s_atan(-x);
  574. }
  575. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  576. s_atan :: proc(x: f64) -> f64 {
  577. MORE_BITS :: 6.123233995736765886130e-17; // pi/2 = PIO2 + MORE_BITS
  578. TAN3PI08 :: 2.41421356237309504880; // tan(3*pi/8)
  579. if x <= 0.66 {
  580. return x_atan(x);
  581. }
  582. if x > TAN3PI08 {
  583. return PI/2 - x_atan(1/x) + MORE_BITS;
  584. }
  585. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS;
  586. }
  587. // x_atan evaluates a series valid in the range [0, 0.66].
  588. x_atan :: proc(x: f64) -> f64 {
  589. P0 :: -8.750608600031904122785e-01;
  590. P1 :: -1.615753718733365076637e+01;
  591. P2 :: -7.500855792314704667340e+01;
  592. P3 :: -1.228866684490136173410e+02;
  593. P4 :: -6.485021904942025371773e+01;
  594. Q0 :: +2.485846490142306297962e+01;
  595. Q1 :: +1.650270098316988542046e+02;
  596. Q2 :: +4.328810604912902668951e+02;
  597. Q3 :: +4.853903996359136964868e+02;
  598. Q4 :: +1.945506571482613964425e+02;
  599. z := x * x;
  600. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4);
  601. z = x*z + x;
  602. return z;
  603. }
  604. switch {
  605. case is_nan(y) || is_nan(x):
  606. return NAN;
  607. case y == 0:
  608. if x >= 0 && !sign_bit(x) {
  609. return copy_sign(0.0, y);
  610. }
  611. return copy_sign(PI, y);
  612. case x == 0:
  613. return copy_sign(PI*0.5, y);
  614. case is_inf(x, 0):
  615. if is_inf(x, 1) {
  616. if is_inf(y, 0) {
  617. return copy_sign(PI*0.25, y);
  618. }
  619. return copy_sign(0, y);
  620. }
  621. if is_inf(y, 0) {
  622. return copy_sign(PI*0.75, y);
  623. }
  624. return copy_sign(PI, y);
  625. case is_inf(y, 0):
  626. return copy_sign(PI*0.5, y);
  627. }
  628. q := atan(y / x);
  629. if x < 0 {
  630. if q <= 0 {
  631. return q + PI;
  632. }
  633. return q - PI;
  634. }
  635. return q;
  636. }
  637. atan2 :: proc{atan2_f32, atan2_f64};
  638. atan_f32 :: proc(x: f32) -> f32 {
  639. return atan2_f32(1.0, x);
  640. }
  641. atan :: proc{atan_f32};
  642. asin_f32 :: proc(x: f32) -> f32 {
  643. return atan2_f32(x, sqrt_f32(1 - x*x));
  644. }
  645. asin_f64 :: proc(x: f64) -> f64 {
  646. return atan2_f64(x, sqrt_f64(1 - x*x));
  647. }
  648. asin :: proc{asin_f32};
  649. acos_f32 :: proc(x: f32) -> f32 {
  650. return atan2_f32(sqrt_f32(1 - x), sqrt_f32(1 + x));
  651. }
  652. acos_f64 :: proc(x: f64) -> f64 {
  653. return atan2_f64(sqrt_f64(1 - x), sqrt_f64(1 + x));
  654. }
  655. acos :: proc{acos_f32};
  656. sinh_f32 :: proc(x: f32) -> f32 {
  657. return (exp(x) - exp(-x))*0.5;
  658. }
  659. sinh_f64 :: proc(x: f64) -> f64 {
  660. return (exp(x) - exp(-x))*0.5;
  661. }
  662. sinh :: proc{sinh_f32, sinh_f64};
  663. cosh_f32 :: proc(x: f32) -> f32 {
  664. return (exp(x) + exp(-x))*0.5;
  665. }
  666. cosh_f64 :: proc(x: f64) -> f64 {
  667. return (exp(x) + exp(-x))*0.5;
  668. }
  669. cosh :: proc{cosh_f32, cosh_f64};
  670. tanh_f32 :: proc(x: f32) -> f32 {
  671. t := exp(2*x);
  672. return (t - 1) / (t + 1);
  673. }
  674. tanh_f64 :: proc(x: f64) -> f64 {
  675. t := exp(2*x);
  676. return (t - 1) / (t + 1);
  677. }
  678. tanh :: proc{tanh_f32, tanh_f64};
  679. F32_DIG :: 6;
  680. F32_EPSILON :: 1.192092896e-07;
  681. F32_GUARD :: 0;
  682. F32_MANT_DIG :: 24;
  683. F32_MAX :: 3.402823466e+38;
  684. F32_MAX_10_EXP :: 38;
  685. F32_MAX_EXP :: 128;
  686. F32_MIN :: 1.175494351e-38;
  687. F32_MIN_10_EXP :: -37;
  688. F32_MIN_EXP :: -125;
  689. F32_NORMALIZE :: 0;
  690. F32_RADIX :: 2;
  691. F32_ROUNDS :: 1;
  692. F64_DIG :: 15; // # of decimal digits of precision
  693. F64_EPSILON :: 2.2204460492503131e-016; // smallest such that 1.0+F64_EPSILON != 1.0
  694. F64_MANT_DIG :: 53; // # of bits in mantissa
  695. F64_MAX :: 1.7976931348623158e+308; // max value
  696. F64_MAX_10_EXP :: 308; // max decimal exponent
  697. F64_MAX_EXP :: 1024; // max binary exponent
  698. F64_MIN :: 2.2250738585072014e-308; // min positive value
  699. F64_MIN_10_EXP :: -307; // min decimal exponent
  700. F64_MIN_EXP :: -1021; // min binary exponent
  701. F64_RADIX :: 2; // exponent radix
  702. F64_ROUNDS :: 1; // addition rounding: near