math.odin 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. package math
  2. import "core: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_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(sqrt_f16(f16(x))) }
  81. sqrt_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(sqrt_f16(f16(x))) }
  82. sqrt_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(sqrt_f32(f32(x))) }
  83. sqrt_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(sqrt_f32(f32(x))) }
  84. sqrt_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(sqrt_f64(f64(x))) }
  85. sqrt_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(sqrt_f64(f64(x))) }
  86. sqrt :: proc{
  87. sqrt_f16, sqrt_f16le, sqrt_f16be,
  88. sqrt_f32, sqrt_f32le, sqrt_f32be,
  89. sqrt_f64, sqrt_f64le, sqrt_f64be,
  90. }
  91. sin_f16le :: proc(θ: f16le) -> f16le { return #force_inline f16le(sin_f16(f16(θ))) }
  92. sin_f16be :: proc(θ: f16be) -> f16be { return #force_inline f16be(sin_f16(f16(θ))) }
  93. sin_f32le :: proc(θ: f32le) -> f32le { return #force_inline f32le(sin_f32(f32(θ))) }
  94. sin_f32be :: proc(θ: f32be) -> f32be { return #force_inline f32be(sin_f32(f32(θ))) }
  95. sin_f64le :: proc(θ: f64le) -> f64le { return #force_inline f64le(sin_f64(f64(θ))) }
  96. sin_f64be :: proc(θ: f64be) -> f64be { return #force_inline f64be(sin_f64(f64(θ))) }
  97. sin :: proc{
  98. sin_f16, sin_f16le, sin_f16be,
  99. sin_f32, sin_f32le, sin_f32be,
  100. sin_f64, sin_f64le, sin_f64be,
  101. }
  102. cos_f16le :: proc(θ: f16le) -> f16le { return #force_inline f16le(cos_f16(f16(θ))) }
  103. cos_f16be :: proc(θ: f16be) -> f16be { return #force_inline f16be(cos_f16(f16(θ))) }
  104. cos_f32le :: proc(θ: f32le) -> f32le { return #force_inline f32le(cos_f32(f32(θ))) }
  105. cos_f32be :: proc(θ: f32be) -> f32be { return #force_inline f32be(cos_f32(f32(θ))) }
  106. cos_f64le :: proc(θ: f64le) -> f64le { return #force_inline f64le(cos_f64(f64(θ))) }
  107. cos_f64be :: proc(θ: f64be) -> f64be { return #force_inline f64be(cos_f64(f64(θ))) }
  108. cos :: proc{
  109. cos_f16, cos_f16le, cos_f16be,
  110. cos_f32, cos_f32le, cos_f32be,
  111. cos_f64, cos_f64le, cos_f64be,
  112. }
  113. pow_f16le :: proc(x, power: f16le) -> f16le { return #force_inline f16le(pow_f16(f16(x), f16(power))) }
  114. pow_f16be :: proc(x, power: f16be) -> f16be { return #force_inline f16be(pow_f16(f16(x), f16(power))) }
  115. pow_f32le :: proc(x, power: f32le) -> f32le { return #force_inline f32le(pow_f32(f32(x), f32(power))) }
  116. pow_f32be :: proc(x, power: f32be) -> f32be { return #force_inline f32be(pow_f32(f32(x), f32(power))) }
  117. pow_f64le :: proc(x, power: f64le) -> f64le { return #force_inline f64le(pow_f64(f64(x), f64(power))) }
  118. pow_f64be :: proc(x, power: f64be) -> f64be { return #force_inline f64be(pow_f64(f64(x), f64(power))) }
  119. pow :: proc{
  120. pow_f16, pow_f16le, pow_f16be,
  121. pow_f32, pow_f32le, pow_f32be,
  122. pow_f64, pow_f64le, pow_f64be,
  123. }
  124. fmuladd_f16le :: proc(a, b, c: f16le) -> f16le { return #force_inline f16le(fmuladd_f16(f16(a), f16(b), f16(c))) }
  125. fmuladd_f16be :: proc(a, b, c: f16be) -> f16be { return #force_inline f16be(fmuladd_f16(f16(a), f16(b), f16(c))) }
  126. fmuladd_f32le :: proc(a, b, c: f32le) -> f32le { return #force_inline f32le(fmuladd_f32(f32(a), f32(b), f32(c))) }
  127. fmuladd_f32be :: proc(a, b, c: f32be) -> f32be { return #force_inline f32be(fmuladd_f32(f32(a), f32(b), f32(c))) }
  128. fmuladd_f64le :: proc(a, b, c: f64le) -> f64le { return #force_inline f64le(fmuladd_f64(f64(a), f64(b), f64(c))) }
  129. fmuladd_f64be :: proc(a, b, c: f64be) -> f64be { return #force_inline f64be(fmuladd_f64(f64(a), f64(b), f64(c))) }
  130. fmuladd :: proc{
  131. fmuladd_f16, fmuladd_f16le, fmuladd_f16be,
  132. fmuladd_f32, fmuladd_f32le, fmuladd_f32be,
  133. fmuladd_f64, fmuladd_f64le, fmuladd_f64be,
  134. }
  135. ln_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(ln_f16(f16(x))) }
  136. ln_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(ln_f16(f16(x))) }
  137. ln_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(ln_f32(f32(x))) }
  138. ln_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(ln_f32(f32(x))) }
  139. ln_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(ln_f64(f64(x))) }
  140. ln_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(ln_f64(f64(x))) }
  141. ln :: proc{
  142. ln_f16, ln_f16le, ln_f16be,
  143. ln_f32, ln_f32le, ln_f32be,
  144. ln_f64, ln_f64le, ln_f64be,
  145. }
  146. exp_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(exp_f16(f16(x))) }
  147. exp_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(exp_f16(f16(x))) }
  148. exp_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(exp_f32(f32(x))) }
  149. exp_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(exp_f32(f32(x))) }
  150. exp_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(exp_f64(f64(x))) }
  151. exp_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(exp_f64(f64(x))) }
  152. exp :: proc{
  153. exp_f16, exp_f16le, exp_f16be,
  154. exp_f32, exp_f32le, exp_f32be,
  155. exp_f64, exp_f64le, exp_f64be,
  156. }
  157. ldexp_f16le :: proc(val: f16le, exp: i32) -> f16le { return #force_inline f16le(ldexp_f16(f16(val), exp)) }
  158. ldexp_f16be :: proc(val: f16be, exp: i32) -> f16be { return #force_inline f16be(ldexp_f16(f16(val), exp)) }
  159. ldexp_f32le :: proc(val: f32le, exp: i32) -> f32le { return #force_inline f32le(ldexp_f32(f32(val), exp)) }
  160. ldexp_f32be :: proc(val: f32be, exp: i32) -> f32be { return #force_inline f32be(ldexp_f32(f32(val), exp)) }
  161. ldexp_f64le :: proc(val: f64le, exp: i32) -> f64le { return #force_inline f64le(ldexp_f64(f64(val), exp)) }
  162. ldexp_f64be :: proc(val: f64be, exp: i32) -> f64be { return #force_inline f64be(ldexp_f64(f64(val), exp)) }
  163. ldexp :: proc{
  164. ldexp_f16, ldexp_f16le, ldexp_f16be,
  165. ldexp_f32, ldexp_f32le, ldexp_f32be,
  166. ldexp_f64, ldexp_f64le, ldexp_f64be,
  167. }
  168. log_f16 :: proc(x, base: f16) -> f16 { return ln(x) / ln(base) }
  169. log_f16le :: proc(x, base: f16le) -> f16le { return f16le(log_f16(f16(x), f16(base))) }
  170. log_f16be :: proc(x, base: f16be) -> f16be { return f16be(log_f16(f16(x), f16(base))) }
  171. log_f32 :: proc(x, base: f32) -> f32 { return ln(x) / ln(base) }
  172. log_f32le :: proc(x, base: f32le) -> f32le { return f32le(log_f32(f32(x), f32(base))) }
  173. log_f32be :: proc(x, base: f32be) -> f32be { return f32be(log_f32(f32(x), f32(base))) }
  174. log_f64 :: proc(x, base: f64) -> f64 { return ln(x) / ln(base) }
  175. log_f64le :: proc(x, base: f64le) -> f64le { return f64le(log_f64(f64(x), f64(base))) }
  176. log_f64be :: proc(x, base: f64be) -> f64be { return f64be(log_f64(f64(x), f64(base))) }
  177. log :: proc{
  178. log_f16, log_f16le, log_f16be,
  179. log_f32, log_f32le, log_f32be,
  180. log_f64, log_f64le, log_f64be,
  181. }
  182. log2_f16 :: proc(x: f16) -> f16 { return ln(x)/LN2 }
  183. log2_f16le :: proc(x: f16le) -> f16le { return f16le(log2_f16(f16(x))) }
  184. log2_f16be :: proc(x: f16be) -> f16be { return f16be(log2_f16(f16(x))) }
  185. log2_f32 :: proc(x: f32) -> f32 { return ln(x)/LN2 }
  186. log2_f32le :: proc(x: f32le) -> f32le { return f32le(log2_f32(f32(x))) }
  187. log2_f32be :: proc(x: f32be) -> f32be { return f32be(log2_f32(f32(x))) }
  188. log2_f64 :: proc(x: f64) -> f64 { return ln(x)/LN2 }
  189. log2_f64le :: proc(x: f64le) -> f64le { return f64le(log2_f64(f64(x))) }
  190. log2_f64be :: proc(x: f64be) -> f64be { return f64be(log2_f64(f64(x))) }
  191. log2 :: proc{
  192. log2_f16, log2_f16le, log2_f16be,
  193. log2_f32, log2_f32le, log2_f32be,
  194. log2_f64, log2_f64le, log2_f64be,
  195. }
  196. log10_f16 :: proc(x: f16) -> f16 { return ln(x)/LN10 }
  197. log10_f16le :: proc(x: f16le) -> f16le { return f16le(log10_f16(f16(x))) }
  198. log10_f16be :: proc(x: f16be) -> f16be { return f16be(log10_f16(f16(x))) }
  199. log10_f32 :: proc(x: f32) -> f32 { return ln(x)/LN10 }
  200. log10_f32le :: proc(x: f32le) -> f32le { return f32le(log10_f32(f32(x))) }
  201. log10_f32be :: proc(x: f32be) -> f32be { return f32be(log10_f32(f32(x))) }
  202. log10_f64 :: proc(x: f64) -> f64 { return ln(x)/LN10 }
  203. log10_f64le :: proc(x: f64le) -> f64le { return f64le(log10_f64(f64(x))) }
  204. log10_f64be :: proc(x: f64be) -> f64be { return f64be(log10_f64(f64(x))) }
  205. log10 :: proc{
  206. log10_f16, log10_f16le, log10_f16be,
  207. log10_f32, log10_f32le, log10_f32be,
  208. log10_f64, log10_f64le, log10_f64be,
  209. }
  210. tan_f16 :: proc(θ: f16) -> f16 { return sin(θ)/cos(θ) }
  211. tan_f16le :: proc(θ: f16le) -> f16le { return f16le(tan_f16(f16(θ))) }
  212. tan_f16be :: proc(θ: f16be) -> f16be { return f16be(tan_f16(f16(θ))) }
  213. tan_f32 :: proc(θ: f32) -> f32 { return sin(θ)/cos(θ) }
  214. tan_f32le :: proc(θ: f32le) -> f32le { return f32le(tan_f32(f32(θ))) }
  215. tan_f32be :: proc(θ: f32be) -> f32be { return f32be(tan_f32(f32(θ))) }
  216. tan_f64 :: proc(θ: f64) -> f64 { return sin(θ)/cos(θ) }
  217. tan_f64le :: proc(θ: f64le) -> f64le { return f64le(tan_f64(f64(θ))) }
  218. tan_f64be :: proc(θ: f64be) -> f64be { return f64be(tan_f64(f64(θ))) }
  219. tan :: proc{
  220. tan_f16, tan_f16le, tan_f16be,
  221. tan_f32, tan_f32le, tan_f32be,
  222. tan_f64, tan_f64le, tan_f64be,
  223. }
  224. lerp :: proc(a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t }
  225. saturate :: proc(a: $T) -> (x: T) { return clamp(a, 0, 1) }
  226. unlerp :: proc(a, b, x: $T) -> (t: T) where intrinsics.type_is_float(T), !intrinsics.type_is_array(T) {
  227. return (x-a)/(b-a)
  228. }
  229. remap :: proc(old_value, old_min, old_max, new_min, new_max: $T) -> (x: T) where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  230. old_range := old_max - old_min
  231. new_range := new_max - new_min
  232. if old_range == 0 {
  233. return new_range / 2
  234. }
  235. return ((old_value - old_min) / old_range) * new_range + new_min
  236. }
  237. wrap :: proc(x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  238. tmp := mod(x, y)
  239. return y + tmp if tmp < 0 else tmp
  240. }
  241. angle_diff :: proc(a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  242. dist := wrap(b - a, TAU)
  243. return wrap(dist*2, TAU) - dist
  244. }
  245. angle_lerp :: proc(a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  246. return a + angle_diff(a, b) * t
  247. }
  248. step :: proc(edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  249. return 0 if x < edge else 1
  250. }
  251. smoothstep :: proc(edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  252. t := clamp((x - edge0) / (edge1 - edge0), 0, 1)
  253. return t * t * (3 - 2*t)
  254. }
  255. bias :: proc(t, b: $T) -> T where intrinsics.type_is_numeric(T) {
  256. return t / (((1/b) - 2) * (1 - t) + 1)
  257. }
  258. gain :: proc(t, g: $T) -> T where intrinsics.type_is_numeric(T) {
  259. if t < 0.5 {
  260. return bias(t*2, g)*0.5
  261. }
  262. return bias(t*2 - 1, 1 - g)*0.5 + 0.5
  263. }
  264. sign_f16 :: proc(x: f16) -> f16 { return f16(int(0 < x) - int(x < 0)) }
  265. sign_f16le :: proc(x: f16le) -> f16le { return f16le(int(0 < x) - int(x < 0)) }
  266. sign_f16be :: proc(x: f16be) -> f16be { return f16be(int(0 < x) - int(x < 0)) }
  267. sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)) }
  268. sign_f32le :: proc(x: f32le) -> f32le { return f32le(int(0 < x) - int(x < 0)) }
  269. sign_f32be :: proc(x: f32be) -> f32be { return f32be(int(0 < x) - int(x < 0)) }
  270. sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)) }
  271. sign_f64le :: proc(x: f64le) -> f64le { return f64le(int(0 < x) - int(x < 0)) }
  272. sign_f64be :: proc(x: f64be) -> f64be { return f64be(int(0 < x) - int(x < 0)) }
  273. sign :: proc{
  274. sign_f16, sign_f16le, sign_f16be,
  275. sign_f32, sign_f32le, sign_f32be,
  276. sign_f64, sign_f64le, sign_f64be,
  277. }
  278. sign_bit_f16 :: proc(x: f16) -> bool {
  279. return (transmute(u16)x) & (1<<15) != 0
  280. }
  281. sign_bit_f16le :: proc(x: f16le) -> bool { return #force_inline sign_bit_f16(f16(x)) }
  282. sign_bit_f16be :: proc(x: f16be) -> bool { return #force_inline sign_bit_f16(f16(x)) }
  283. sign_bit_f32 :: proc(x: f32) -> bool {
  284. return (transmute(u32)x) & (1<<31) != 0
  285. }
  286. sign_bit_f32le :: proc(x: f32le) -> bool { return #force_inline sign_bit_f32(f32(x)) }
  287. sign_bit_f32be :: proc(x: f32be) -> bool { return #force_inline sign_bit_f32(f32(x)) }
  288. sign_bit_f64 :: proc(x: f64) -> bool {
  289. return (transmute(u64)x) & (1<<63) != 0
  290. }
  291. sign_bit_f64le :: proc(x: f64le) -> bool { return #force_inline sign_bit_f64(f64(x)) }
  292. sign_bit_f64be :: proc(x: f64be) -> bool { return #force_inline sign_bit_f64(f64(x)) }
  293. sign_bit :: proc{
  294. sign_bit_f16, sign_bit_f16le, sign_bit_f16be,
  295. sign_bit_f32, sign_bit_f32le, sign_bit_f32be,
  296. sign_bit_f64, sign_bit_f64le, sign_bit_f64be,
  297. }
  298. copy_sign_f16 :: proc(x, y: f16) -> f16 {
  299. ix := transmute(u16)x
  300. iy := transmute(u16)y
  301. ix &= 0x7fff
  302. ix |= iy & 0x8000
  303. return transmute(f16)ix
  304. }
  305. copy_sign_f16le :: proc(x, y: f16le) -> f16le { return #force_inline f16le(copy_sign_f16(f16(x), f16(y))) }
  306. copy_sign_f16be :: proc(x, y: f16be) -> f16be { return #force_inline f16be(copy_sign_f16(f16(x), f16(y))) }
  307. copy_sign_f32 :: proc(x, y: f32) -> f32 {
  308. ix := transmute(u32)x
  309. iy := transmute(u32)y
  310. ix &= 0x7fff_ffff
  311. ix |= iy & 0x8000_0000
  312. return transmute(f32)ix
  313. }
  314. copy_sign_f32le :: proc(x, y: f32le) -> f32le { return #force_inline f32le(copy_sign_f32(f32(x), f32(y))) }
  315. copy_sign_f32be :: proc(x, y: f32be) -> f32be { return #force_inline f32be(copy_sign_f32(f32(x), f32(y))) }
  316. copy_sign_f64 :: proc(x, y: f64) -> f64 {
  317. ix := transmute(u64)x
  318. iy := transmute(u64)y
  319. ix &= 0x7fff_ffff_ffff_ffff
  320. ix |= iy & 0x8000_0000_0000_0000
  321. return transmute(f64)ix
  322. }
  323. copy_sign_f64le :: proc(x, y: f64le) -> f64le { return #force_inline f64le(copy_sign_f64(f64(x), f64(y))) }
  324. copy_sign_f64be :: proc(x, y: f64be) -> f64be { return #force_inline f64be(copy_sign_f64(f64(x), f64(y))) }
  325. copy_sign :: proc{
  326. copy_sign_f16, copy_sign_f16le, copy_sign_f16be,
  327. copy_sign_f32, copy_sign_f32le, copy_sign_f32be,
  328. copy_sign_f64, copy_sign_f64le, copy_sign_f64be,
  329. }
  330. to_radians_f16 :: proc(degrees: f16) -> f16 { return degrees * RAD_PER_DEG }
  331. to_radians_f16le :: proc(degrees: f16le) -> f16le { return degrees * RAD_PER_DEG }
  332. to_radians_f16be :: proc(degrees: f16be) -> f16be { return degrees * RAD_PER_DEG }
  333. to_radians_f32 :: proc(degrees: f32) -> f32 { return degrees * RAD_PER_DEG }
  334. to_radians_f32le :: proc(degrees: f32le) -> f32le { return degrees * RAD_PER_DEG }
  335. to_radians_f32be :: proc(degrees: f32be) -> f32be { return degrees * RAD_PER_DEG }
  336. to_radians_f64 :: proc(degrees: f64) -> f64 { return degrees * RAD_PER_DEG }
  337. to_radians_f64le :: proc(degrees: f64le) -> f64le { return degrees * RAD_PER_DEG }
  338. to_radians_f64be :: proc(degrees: f64be) -> f64be { return degrees * RAD_PER_DEG }
  339. to_degrees_f16 :: proc(radians: f16) -> f16 { return radians * DEG_PER_RAD }
  340. to_degrees_f16le :: proc(radians: f16le) -> f16le { return radians * DEG_PER_RAD }
  341. to_degrees_f16be :: proc(radians: f16be) -> f16be { return radians * DEG_PER_RAD }
  342. to_degrees_f32 :: proc(radians: f32) -> f32 { return radians * DEG_PER_RAD }
  343. to_degrees_f32le :: proc(radians: f32le) -> f32le { return radians * DEG_PER_RAD }
  344. to_degrees_f32be :: proc(radians: f32be) -> f32be { return radians * DEG_PER_RAD }
  345. to_degrees_f64 :: proc(radians: f64) -> f64 { return radians * DEG_PER_RAD }
  346. to_degrees_f64le :: proc(radians: f64le) -> f64le { return radians * DEG_PER_RAD }
  347. to_degrees_f64be :: proc(radians: f64be) -> f64be { return radians * DEG_PER_RAD }
  348. to_radians :: proc{
  349. to_radians_f16, to_radians_f16le, to_radians_f16be,
  350. to_radians_f32, to_radians_f32le, to_radians_f32be,
  351. to_radians_f64, to_radians_f64le, to_radians_f64be,
  352. }
  353. to_degrees :: proc{
  354. to_degrees_f16, to_degrees_f16le, to_degrees_f16be,
  355. to_degrees_f32, to_degrees_f32le, to_degrees_f32be,
  356. to_degrees_f64, to_degrees_f64le, to_degrees_f64be,
  357. }
  358. trunc_f16 :: proc(x: f16) -> f16 {
  359. trunc_internal :: proc(f: f16) -> f16 {
  360. mask :: 0x1f
  361. shift :: 16 - 6
  362. bias :: 0xf
  363. if f < 1 {
  364. switch {
  365. case f < 0: return -trunc_internal(-f)
  366. case f == 0: return f
  367. case: return 0
  368. }
  369. }
  370. x := transmute(u16)f
  371. e := (x >> shift) & mask - bias
  372. if e < shift {
  373. x &= ~(1 << (shift-e)) - 1
  374. }
  375. return transmute(f16)x
  376. }
  377. switch classify(x) {
  378. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  379. return x
  380. case .Normal, .Subnormal: // carry on
  381. }
  382. return trunc_internal(x)
  383. }
  384. trunc_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(trunc_f16(f16(x))) }
  385. trunc_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(trunc_f16(f16(x))) }
  386. trunc_f32 :: proc(x: f32) -> f32 {
  387. trunc_internal :: proc(f: f32) -> f32 {
  388. mask :: 0xff
  389. shift :: 32 - 9
  390. bias :: 0x7f
  391. if f < 1 {
  392. switch {
  393. case f < 0: return -trunc_internal(-f)
  394. case f == 0: return f
  395. case: return 0
  396. }
  397. }
  398. x := transmute(u32)f
  399. e := (x >> shift) & mask - bias
  400. if e < shift {
  401. x &= ~(1 << (shift-e)) - 1
  402. }
  403. return transmute(f32)x
  404. }
  405. switch classify(x) {
  406. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  407. return x
  408. case .Normal, .Subnormal: // carry on
  409. }
  410. return trunc_internal(x)
  411. }
  412. trunc_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(trunc_f32(f32(x))) }
  413. trunc_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(trunc_f32(f32(x))) }
  414. trunc_f64 :: proc(x: f64) -> f64 {
  415. trunc_internal :: proc(f: f64) -> f64 {
  416. mask :: 0x7ff
  417. shift :: 64 - 12
  418. bias :: 0x3ff
  419. if f < 1 {
  420. switch {
  421. case f < 0: return -trunc_internal(-f)
  422. case f == 0: return f
  423. case: return 0
  424. }
  425. }
  426. x := transmute(u64)f
  427. e := (x >> shift) & mask - bias
  428. if e < shift {
  429. x &= ~(1 << (shift-e)) - 1
  430. }
  431. return transmute(f64)x
  432. }
  433. switch classify(x) {
  434. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  435. return x
  436. case .Normal, .Subnormal: // carry on
  437. }
  438. return trunc_internal(x)
  439. }
  440. trunc_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(trunc_f64(f64(x))) }
  441. trunc_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(trunc_f64(f64(x))) }
  442. trunc :: proc{
  443. trunc_f16, trunc_f16le, trunc_f16be,
  444. trunc_f32, trunc_f32le, trunc_f32be,
  445. trunc_f64, trunc_f64le, trunc_f64be,
  446. }
  447. round_f16 :: proc(x: f16) -> f16 {
  448. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  449. }
  450. round_f16le :: proc(x: f16le) -> f16le {
  451. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  452. }
  453. round_f16be :: proc(x: f16be) -> f16be {
  454. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  455. }
  456. round_f32 :: proc(x: f32) -> f32 {
  457. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  458. }
  459. round_f32le :: proc(x: f32le) -> f32le {
  460. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  461. }
  462. round_f32be :: proc(x: f32be) -> f32be {
  463. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  464. }
  465. round_f64 :: proc(x: f64) -> f64 {
  466. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  467. }
  468. round_f64le :: proc(x: f64le) -> f64le {
  469. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  470. }
  471. round_f64be :: proc(x: f64be) -> f64be {
  472. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  473. }
  474. round :: proc{
  475. round_f16, round_f16le, round_f16be,
  476. round_f32, round_f32le, round_f32be,
  477. round_f64, round_f64le, round_f64be,
  478. }
  479. ceil_f16 :: proc(x: f16) -> f16 { return -floor(-x) }
  480. ceil_f16le :: proc(x: f16le) -> f16le { return -floor(-x) }
  481. ceil_f16be :: proc(x: f16be) -> f16be { return -floor(-x) }
  482. ceil_f32 :: proc(x: f32) -> f32 { return -floor(-x) }
  483. ceil_f32le :: proc(x: f32le) -> f32le { return -floor(-x) }
  484. ceil_f32be :: proc(x: f32be) -> f32be { return -floor(-x) }
  485. ceil_f64 :: proc(x: f64) -> f64 { return -floor(-x) }
  486. ceil_f64le :: proc(x: f64le) -> f64le { return -floor(-x) }
  487. ceil_f64be :: proc(x: f64be) -> f64be { return -floor(-x) }
  488. ceil :: proc{
  489. ceil_f16, ceil_f16le, ceil_f16be,
  490. ceil_f32, ceil_f32le, ceil_f32be,
  491. ceil_f64, ceil_f64le, ceil_f64be,
  492. }
  493. floor_f16 :: proc(x: f16) -> f16 {
  494. if x == 0 || is_nan(x) || is_inf(x) {
  495. return x
  496. }
  497. if x < 0 {
  498. d, fract := modf(-x)
  499. if fract != 0.0 {
  500. d = d + 1
  501. }
  502. return -d
  503. }
  504. d, _ := modf(x)
  505. return d
  506. }
  507. floor_f16le :: proc(x: f16le) -> f16le { return #force_inline f16le(floor_f16(f16(x))) }
  508. floor_f16be :: proc(x: f16be) -> f16be { return #force_inline f16be(floor_f16(f16(x))) }
  509. floor_f32 :: proc(x: f32) -> f32 {
  510. if x == 0 || is_nan(x) || is_inf(x) {
  511. return x
  512. }
  513. if x < 0 {
  514. d, fract := modf(-x)
  515. if fract != 0.0 {
  516. d = d + 1
  517. }
  518. return -d
  519. }
  520. d, _ := modf(x)
  521. return d
  522. }
  523. floor_f32le :: proc(x: f32le) -> f32le { return #force_inline f32le(floor_f32(f32(x))) }
  524. floor_f32be :: proc(x: f32be) -> f32be { return #force_inline f32be(floor_f32(f32(x))) }
  525. floor_f64 :: proc(x: f64) -> f64 {
  526. if x == 0 || is_nan(x) || is_inf(x) {
  527. return x
  528. }
  529. if x < 0 {
  530. d, fract := modf(-x)
  531. if fract != 0.0 {
  532. d = d + 1
  533. }
  534. return -d
  535. }
  536. d, _ := modf(x)
  537. return d
  538. }
  539. floor_f64le :: proc(x: f64le) -> f64le { return #force_inline f64le(floor_f64(f64(x))) }
  540. floor_f64be :: proc(x: f64be) -> f64be { return #force_inline f64be(floor_f64(f64(x))) }
  541. floor :: proc{
  542. floor_f16, floor_f16le, floor_f16be,
  543. floor_f32, floor_f32le, floor_f32be,
  544. floor_f64, floor_f64le, floor_f64be,
  545. }
  546. floor_div :: proc(x, y: $T) -> T
  547. where intrinsics.type_is_integer(T) {
  548. a := x / y
  549. r := x % y
  550. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  551. a -= 1
  552. }
  553. return a
  554. }
  555. floor_mod :: proc(x, y: $T) -> T
  556. where intrinsics.type_is_integer(T) {
  557. r := x % y
  558. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  559. r += y
  560. }
  561. return r
  562. }
  563. modf_f16 :: proc(x: f16) -> (int: f16, frac: f16) {
  564. shift :: 16 - 5 - 1
  565. mask :: 0x1f
  566. bias :: 15
  567. if x < 1 {
  568. switch {
  569. case x < 0:
  570. int, frac = modf(-x)
  571. return -int, -frac
  572. case x == 0:
  573. return x, x
  574. }
  575. return 0, x
  576. }
  577. i := transmute(u16)x
  578. e := uint(i>>shift)&mask - bias
  579. if e < shift {
  580. i &~= 1<<(shift-e) - 1
  581. }
  582. int = transmute(f16)i
  583. frac = x - int
  584. return
  585. }
  586. modf_f16le :: proc(x: f16le) -> (int: f16le, frac: f16le) {
  587. i, f := #force_inline modf_f16(f16(x))
  588. return f16le(i), f16le(f)
  589. }
  590. modf_f16be :: proc(x: f16be) -> (int: f16be, frac: f16be) {
  591. i, f := #force_inline modf_f16(f16(x))
  592. return f16be(i), f16be(f)
  593. }
  594. modf_f32 :: proc(x: f32) -> (int: f32, frac: f32) {
  595. shift :: 32 - 8 - 1
  596. mask :: 0xff
  597. bias :: 127
  598. if x < 1 {
  599. switch {
  600. case x < 0:
  601. int, frac = modf(-x)
  602. return -int, -frac
  603. case x == 0:
  604. return x, x
  605. }
  606. return 0, x
  607. }
  608. i := transmute(u32)x
  609. e := uint(i>>shift)&mask - bias
  610. if e < shift {
  611. i &~= 1<<(shift-e) - 1
  612. }
  613. int = transmute(f32)i
  614. frac = x - int
  615. return
  616. }
  617. modf_f32le :: proc(x: f32le) -> (int: f32le, frac: f32le) {
  618. i, f := #force_inline modf_f32(f32(x))
  619. return f32le(i), f32le(f)
  620. }
  621. modf_f32be :: proc(x: f32be) -> (int: f32be, frac: f32be) {
  622. i, f := #force_inline modf_f32(f32(x))
  623. return f32be(i), f32be(f)
  624. }
  625. modf_f64 :: proc(x: f64) -> (int: f64, frac: f64) {
  626. shift :: 64 - 11 - 1
  627. mask :: 0x7ff
  628. bias :: 1023
  629. if x < 1 {
  630. switch {
  631. case x < 0:
  632. int, frac = modf(-x)
  633. return -int, -frac
  634. case x == 0:
  635. return x, x
  636. }
  637. return 0, x
  638. }
  639. i := transmute(u64)x
  640. e := uint(i>>shift)&mask - bias
  641. if e < shift {
  642. i &~= 1<<(shift-e) - 1
  643. }
  644. int = transmute(f64)i
  645. frac = x - int
  646. return
  647. }
  648. modf_f64le :: proc(x: f64le) -> (int: f64le, frac: f64le) {
  649. i, f := #force_inline modf_f64(f64(x))
  650. return f64le(i), f64le(f)
  651. }
  652. modf_f64be :: proc(x: f64be) -> (int: f64be, frac: f64be) {
  653. i, f := #force_inline modf_f64(f64(x))
  654. return f64be(i), f64be(f)
  655. }
  656. modf :: proc{
  657. modf_f16, modf_f16le, modf_f16be,
  658. modf_f32, modf_f32le, modf_f32be,
  659. modf_f64, modf_f64le, modf_f64be,
  660. }
  661. split_decimal :: modf
  662. mod_f16 :: proc(x, y: f16) -> (n: f16) {
  663. z := abs(y)
  664. n = remainder(abs(x), z)
  665. if sign(n) < 0 {
  666. n += z
  667. }
  668. return copy_sign(n, x)
  669. }
  670. mod_f16le :: proc(x, y: f16le) -> (n: f16le) { return #force_inline f16le(mod_f16(f16(x), f16(y))) }
  671. mod_f16be :: proc(x, y: f16be) -> (n: f16be) { return #force_inline f16be(mod_f16(f16(x), f16(y))) }
  672. mod_f32 :: proc(x, y: f32) -> (n: f32) {
  673. z := abs(y)
  674. n = remainder(abs(x), z)
  675. if sign(n) < 0 {
  676. n += z
  677. }
  678. return copy_sign(n, x)
  679. }
  680. mod_f32le :: proc(x, y: f32le) -> (n: f32le) { return #force_inline f32le(mod_f32(f32(x), f32(y))) }
  681. mod_f32be :: proc(x, y: f32be) -> (n: f32be) { return #force_inline f32be(mod_f32(f32(x), f32(y))) }
  682. mod_f64 :: proc(x, y: f64) -> (n: f64) {
  683. z := abs(y)
  684. n = remainder(abs(x), z)
  685. if sign(n) < 0 {
  686. n += z
  687. }
  688. return copy_sign(n, x)
  689. }
  690. mod_f64le :: proc(x, y: f64le) -> (n: f64le) { return #force_inline f64le(mod_f64(f64(x), f64(y))) }
  691. mod_f64be :: proc(x, y: f64be) -> (n: f64be) { return #force_inline f64be(mod_f64(f64(x), f64(y))) }
  692. mod :: proc{
  693. mod_f16, mod_f16le, mod_f16be,
  694. mod_f32, mod_f32le, mod_f32be,
  695. mod_f64, mod_f64le, mod_f64be,
  696. }
  697. remainder_f16 :: proc(x, y: f16 ) -> f16 { return x - round(x/y) * y }
  698. remainder_f16le :: proc(x, y: f16le) -> f16le { return x - round(x/y) * y }
  699. remainder_f16be :: proc(x, y: f16be) -> f16be { return x - round(x/y) * y }
  700. remainder_f32 :: proc(x, y: f32 ) -> f32 { return x - round(x/y) * y }
  701. remainder_f32le :: proc(x, y: f32le) -> f32le { return x - round(x/y) * y }
  702. remainder_f32be :: proc(x, y: f32be) -> f32be { return x - round(x/y) * y }
  703. remainder_f64 :: proc(x, y: f64 ) -> f64 { return x - round(x/y) * y }
  704. remainder_f64le :: proc(x, y: f64le) -> f64le { return x - round(x/y) * y }
  705. remainder_f64be :: proc(x, y: f64be) -> f64be { return x - round(x/y) * y }
  706. remainder :: proc{
  707. remainder_f16, remainder_f16le, remainder_f16be,
  708. remainder_f32, remainder_f32le, remainder_f32be,
  709. remainder_f64, remainder_f64le, remainder_f64be,
  710. }
  711. gcd :: proc(x, y: $T) -> T
  712. where intrinsics.type_is_ordered_numeric(T) {
  713. x, y := x, y
  714. for y != 0 {
  715. x %= y
  716. x, y = y, x
  717. }
  718. return abs(x)
  719. }
  720. lcm :: proc(x, y: $T) -> T
  721. where intrinsics.type_is_ordered_numeric(T) {
  722. return x / gcd(x, y) * y
  723. }
  724. frexp_f16 :: proc(x: f16) -> (significand: f16, exponent: int) {
  725. f, e := frexp_f64(f64(x))
  726. return f16(f), e
  727. }
  728. frexp_f16le :: proc(x: f16le) -> (significand: f16le, exponent: int) {
  729. f, e := frexp_f64(f64(x))
  730. return f16le(f), e
  731. }
  732. frexp_f16be :: proc(x: f16be) -> (significand: f16be, exponent: int) {
  733. f, e := frexp_f64(f64(x))
  734. return f16be(f), e
  735. }
  736. frexp_f32 :: proc(x: f32) -> (significand: f32, exponent: int) {
  737. f, e := frexp_f64(f64(x))
  738. return f32(f), e
  739. }
  740. frexp_f32le :: proc(x: f32le) -> (significand: f32le, exponent: int) {
  741. f, e := frexp_f64(f64(x))
  742. return f32le(f), e
  743. }
  744. frexp_f32be :: proc(x: f32be) -> (significand: f32be, exponent: int) {
  745. f, e := frexp_f64(f64(x))
  746. return f32be(f), e
  747. }
  748. frexp_f64 :: proc(x: f64) -> (significand: f64, exponent: int) {
  749. switch {
  750. case x == 0:
  751. return 0, 0
  752. case x < 0:
  753. significand, exponent = frexp(-x)
  754. return -significand, exponent
  755. }
  756. ex := trunc(log2(x))
  757. exponent = int(ex)
  758. significand = x / pow(2.0, ex)
  759. if abs(significand) >= 1 {
  760. exponent += 1
  761. significand /= 2
  762. }
  763. if exponent == 1024 && significand == 0 {
  764. significand = 0.99999999999999988898
  765. }
  766. return
  767. }
  768. frexp_f64le :: proc(x: f64le) -> (significand: f64le, exponent: int) {
  769. f, e := frexp_f64(f64(x))
  770. return f64le(f), e
  771. }
  772. frexp_f64be :: proc(x: f64be) -> (significand: f64be, exponent: int) {
  773. f, e := frexp_f64(f64(x))
  774. return f64be(f), e
  775. }
  776. frexp :: proc{
  777. frexp_f16, frexp_f16le, frexp_f16be,
  778. frexp_f32, frexp_f32le, frexp_f32be,
  779. frexp_f64, frexp_f64le, frexp_f64be,
  780. }
  781. binomial :: proc(n, k: int) -> int {
  782. switch {
  783. case k <= 0: return 1
  784. case 2*k > n: return binomial(n, n-k)
  785. }
  786. b := n
  787. for i in 2..<k {
  788. b = (b * (n+1-i))/i
  789. }
  790. return b
  791. }
  792. factorial :: proc(n: int) -> int {
  793. when size_of(int) == size_of(i64) {
  794. @static table := [21]int{
  795. 1,
  796. 1,
  797. 2,
  798. 6,
  799. 24,
  800. 120,
  801. 720,
  802. 5_040,
  803. 40_320,
  804. 362_880,
  805. 3_628_800,
  806. 39_916_800,
  807. 479_001_600,
  808. 6_227_020_800,
  809. 87_178_291_200,
  810. 1_307_674_368_000,
  811. 20_922_789_888_000,
  812. 355_687_428_096_000,
  813. 6_402_373_705_728_000,
  814. 121_645_100_408_832_000,
  815. 2_432_902_008_176_640_000,
  816. }
  817. } else {
  818. @static table := [13]int{
  819. 1,
  820. 1,
  821. 2,
  822. 6,
  823. 24,
  824. 120,
  825. 720,
  826. 5_040,
  827. 40_320,
  828. 362_880,
  829. 3_628_800,
  830. 39_916_800,
  831. 479_001_600,
  832. }
  833. }
  834. assert(n >= 0, "parameter must not be negative")
  835. assert(n < len(table), "parameter is too large to lookup in the table")
  836. return table[n]
  837. }
  838. classify_f16 :: proc(x: f16) -> Float_Class {
  839. switch {
  840. case x == 0:
  841. i := transmute(i16)x
  842. if i < 0 {
  843. return .Neg_Zero
  844. }
  845. return .Zero
  846. case x*0.5 == x:
  847. if x < 0 {
  848. return .Neg_Inf
  849. }
  850. return .Inf
  851. case !(x == x):
  852. return .NaN
  853. }
  854. u := transmute(u16)x
  855. exp := int(u>>10) & (1<<5 - 1)
  856. if exp == 0 {
  857. return .Subnormal
  858. }
  859. return .Normal
  860. }
  861. classify_f16le :: proc(x: f16le) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  862. classify_f16be :: proc(x: f16be) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  863. classify_f32 :: proc(x: f32) -> Float_Class {
  864. switch {
  865. case x == 0:
  866. i := transmute(i32)x
  867. if i < 0 {
  868. return .Neg_Zero
  869. }
  870. return .Zero
  871. case x*0.5 == x:
  872. if x < 0 {
  873. return .Neg_Inf
  874. }
  875. return .Inf
  876. case !(x == x):
  877. return .NaN
  878. }
  879. u := transmute(u32)x
  880. exp := int(u>>23) & (1<<8 - 1)
  881. if exp == 0 {
  882. return .Subnormal
  883. }
  884. return .Normal
  885. }
  886. classify_f32le :: proc(x: f32le) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  887. classify_f32be :: proc(x: f32be) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  888. classify_f64 :: proc(x: f64) -> Float_Class {
  889. switch {
  890. case x == 0:
  891. i := transmute(i64)x
  892. if i < 0 {
  893. return .Neg_Zero
  894. }
  895. return .Zero
  896. case x*0.5 == x:
  897. if x < 0 {
  898. return .Neg_Inf
  899. }
  900. return .Inf
  901. case !(x == x):
  902. return .NaN
  903. }
  904. u := transmute(u64)x
  905. exp := int(u>>52) & (1<<11 - 1)
  906. if exp == 0 {
  907. return .Subnormal
  908. }
  909. return .Normal
  910. }
  911. classify_f64le :: proc(x: f64le) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  912. classify_f64be :: proc(x: f64be) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  913. classify :: proc{
  914. classify_f16, classify_f16le, classify_f16be,
  915. classify_f32, classify_f32le, classify_f32be,
  916. classify_f64, classify_f64le, classify_f64be,
  917. }
  918. is_nan_f16 :: proc(x: f16) -> bool { return classify(x) == .NaN }
  919. is_nan_f16le :: proc(x: f16le) -> bool { return classify(x) == .NaN }
  920. is_nan_f16be :: proc(x: f16be) -> bool { return classify(x) == .NaN }
  921. is_nan_f32 :: proc(x: f32) -> bool { return classify(x) == .NaN }
  922. is_nan_f32le :: proc(x: f32le) -> bool { return classify(x) == .NaN }
  923. is_nan_f32be :: proc(x: f32be) -> bool { return classify(x) == .NaN }
  924. is_nan_f64 :: proc(x: f64) -> bool { return classify(x) == .NaN }
  925. is_nan_f64le :: proc(x: f64le) -> bool { return classify(x) == .NaN }
  926. is_nan_f64be :: proc(x: f64be) -> bool { return classify(x) == .NaN }
  927. is_nan :: proc{
  928. is_nan_f16, is_nan_f16le, is_nan_f16be,
  929. is_nan_f32, is_nan_f32le, is_nan_f32be,
  930. is_nan_f64, is_nan_f64le, is_nan_f64be,
  931. }
  932. // is_inf reports whether f is an infinity, according to sign.
  933. // If sign > 0, is_inf reports whether f is positive infinity.
  934. // If sign < 0, is_inf reports whether f is negative infinity.
  935. // If sign == 0, is_inf reports whether f is either infinity.
  936. is_inf_f16 :: proc(x: f16, sign: int = 0) -> bool {
  937. class := classify(abs(x))
  938. switch {
  939. case sign > 0:
  940. return class == .Inf
  941. case sign < 0:
  942. return class == .Neg_Inf
  943. }
  944. return class == .Inf || class == .Neg_Inf
  945. }
  946. is_inf_f16le :: proc(x: f16le, sign: int = 0) -> bool {
  947. return #force_inline is_inf_f16(f16(x), sign)
  948. }
  949. is_inf_f16be :: proc(x: f16be, sign: int = 0) -> bool {
  950. return #force_inline is_inf_f16(f16(x), sign)
  951. }
  952. is_inf_f32 :: proc(x: f32, sign: int = 0) -> bool {
  953. class := classify(abs(x))
  954. switch {
  955. case sign > 0:
  956. return class == .Inf
  957. case sign < 0:
  958. return class == .Neg_Inf
  959. }
  960. return class == .Inf || class == .Neg_Inf
  961. }
  962. is_inf_f32le :: proc(x: f32le, sign: int = 0) -> bool {
  963. return #force_inline is_inf_f32(f32(x), sign)
  964. }
  965. is_inf_f32be :: proc(x: f32be, sign: int = 0) -> bool {
  966. return #force_inline is_inf_f32(f32(x), sign)
  967. }
  968. is_inf_f64 :: proc(x: f64, sign: int = 0) -> bool {
  969. class := classify(abs(x))
  970. switch {
  971. case sign > 0:
  972. return class == .Inf
  973. case sign < 0:
  974. return class == .Neg_Inf
  975. }
  976. return class == .Inf || class == .Neg_Inf
  977. }
  978. is_inf_f64le :: proc(x: f64le, sign: int = 0) -> bool {
  979. return #force_inline is_inf_f64(f64(x), sign)
  980. }
  981. is_inf_f64be :: proc(x: f64be, sign: int = 0) -> bool {
  982. return #force_inline is_inf_f64(f64(x), sign)
  983. }
  984. is_inf :: proc{
  985. is_inf_f16, is_inf_f16le, is_inf_f16be,
  986. is_inf_f32, is_inf_f32le, is_inf_f32be,
  987. is_inf_f64, is_inf_f64le, is_inf_f64be,
  988. }
  989. inf_f16 :: proc(sign: int) -> f16 {
  990. return f16(inf_f64(sign))
  991. }
  992. inf_f16le :: proc(sign: int) -> f16le {
  993. return f16le(inf_f64(sign))
  994. }
  995. inf_f16be :: proc(sign: int) -> f16be {
  996. return f16be(inf_f64(sign))
  997. }
  998. inf_f32 :: proc(sign: int) -> f32 {
  999. return f32(inf_f64(sign))
  1000. }
  1001. inf_f32le :: proc(sign: int) -> f32le {
  1002. return f32le(inf_f64(sign))
  1003. }
  1004. inf_f32be :: proc(sign: int) -> f32be {
  1005. return f32be(inf_f64(sign))
  1006. }
  1007. inf_f64 :: proc(sign: int) -> f64 {
  1008. v: u64
  1009. if sign >= 0 {
  1010. v = 0x7ff00000_00000000
  1011. } else {
  1012. v = 0xfff00000_00000000
  1013. }
  1014. return transmute(f64)v
  1015. }
  1016. inf_f64le :: proc(sign: int) -> f64le {
  1017. return f64le(inf_f64(sign))
  1018. }
  1019. inf_f64be :: proc(sign: int) -> f64be {
  1020. return f64be(inf_f64(sign))
  1021. }
  1022. nan_f16 :: proc() -> f16 {
  1023. return f16(nan_f64())
  1024. }
  1025. nan_f16le :: proc() -> f16le {
  1026. return f16le(nan_f64())
  1027. }
  1028. nan_f16be :: proc() -> f16be {
  1029. return f16be(nan_f64())
  1030. }
  1031. nan_f32 :: proc() -> f32 {
  1032. return f32(nan_f64())
  1033. }
  1034. nan_f32le :: proc() -> f32le {
  1035. return f32le(nan_f64())
  1036. }
  1037. nan_f32be :: proc() -> f32be {
  1038. return f32be(nan_f64())
  1039. }
  1040. nan_f64 :: proc() -> f64 {
  1041. v: u64 = 0x7ff80000_00000001
  1042. return transmute(f64)v
  1043. }
  1044. nan_f64le :: proc() -> f64le {
  1045. return f64le(nan_f64())
  1046. }
  1047. nan_f64be :: proc() -> f64be {
  1048. return f64be(nan_f64())
  1049. }
  1050. is_power_of_two :: proc(x: int) -> bool {
  1051. return x > 0 && (x & (x-1)) == 0
  1052. }
  1053. next_power_of_two :: proc(x: int) -> int {
  1054. k := x -1
  1055. when size_of(int) == 8 {
  1056. k = k | (k >> 32)
  1057. }
  1058. k = k | (k >> 16)
  1059. k = k | (k >> 8)
  1060. k = k | (k >> 4)
  1061. k = k | (k >> 2)
  1062. k = k | (k >> 1)
  1063. k += 1 + int(x <= 0)
  1064. return k
  1065. }
  1066. sum :: proc(x: $T/[]$E) -> (res: E)
  1067. where intrinsics.type_is_numeric(E) {
  1068. for i in x {
  1069. res += i
  1070. }
  1071. return
  1072. }
  1073. prod :: proc(x: $T/[]$E) -> (res: E)
  1074. where intrinsics.type_is_numeric(E) {
  1075. for i in x {
  1076. res *= i
  1077. }
  1078. return
  1079. }
  1080. cumsum_inplace :: proc(x: $T/[]$E) -> T
  1081. where intrinsics.type_is_numeric(E) {
  1082. for i in 1..<len(x) {
  1083. x[i] = x[i-1] + x[i]
  1084. }
  1085. }
  1086. cumsum :: proc(dst, src: $T/[]$E) -> T
  1087. where intrinsics.type_is_numeric(E) {
  1088. N := min(len(dst), len(src))
  1089. if N > 0 {
  1090. dst[0] = src[0]
  1091. for i in 1..<N {
  1092. dst[i] = dst[i-1] + src[i]
  1093. }
  1094. }
  1095. return dst[:N]
  1096. }
  1097. atan2_f16 :: proc(y, x: f16) -> f16 {
  1098. // TODO(bill): Better atan2_f16
  1099. return f16(atan2_f64(f64(y), f64(x)))
  1100. }
  1101. atan2_f16le :: proc(y, x: f16le) -> f16le {
  1102. // TODO(bill): Better atan2_f16
  1103. return f16le(atan2_f64(f64(y), f64(x)))
  1104. }
  1105. atan2_f16be :: proc(y, x: f16be) -> f16be {
  1106. // TODO(bill): Better atan2_f16
  1107. return f16be(atan2_f64(f64(y), f64(x)))
  1108. }
  1109. atan2_f32 :: proc(y, x: f32) -> f32 {
  1110. // TODO(bill): Better atan2_f32
  1111. return f32(atan2_f64(f64(y), f64(x)))
  1112. }
  1113. atan2_f32le :: proc(y, x: f32le) -> f32le {
  1114. // TODO(bill): Better atan2_f32
  1115. return f32le(atan2_f64(f64(y), f64(x)))
  1116. }
  1117. atan2_f32be :: proc(y, x: f32be) -> f32be {
  1118. // TODO(bill): Better atan2_f32
  1119. return f32be(atan2_f64(f64(y), f64(x)))
  1120. }
  1121. atan2_f64 :: proc(y, x: f64) -> f64 {
  1122. // TODO(bill): Faster atan2_f64 if possible
  1123. // The original C code:
  1124. // Stephen L. Moshier
  1125. // [email protected]
  1126. NAN :: 0h7fff_ffff_ffff_ffff
  1127. INF :: 0h7FF0_0000_0000_0000
  1128. PI :: 0h4009_21fb_5444_2d18
  1129. atan :: proc(x: f64) -> f64 {
  1130. if x == 0 {
  1131. return x
  1132. }
  1133. if x > 0 {
  1134. return s_atan(x)
  1135. }
  1136. return -s_atan(-x)
  1137. }
  1138. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  1139. s_atan :: proc(x: f64) -> f64 {
  1140. MORE_BITS :: 6.123233995736765886130e-17 // pi/2 = PIO2 + MORE_BITS
  1141. TAN3PI08 :: 2.41421356237309504880 // tan(3*pi/8)
  1142. if x <= 0.66 {
  1143. return x_atan(x)
  1144. }
  1145. if x > TAN3PI08 {
  1146. return PI/2 - x_atan(1/x) + MORE_BITS
  1147. }
  1148. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS
  1149. }
  1150. // x_atan evaluates a series valid in the range [0, 0.66].
  1151. x_atan :: proc(x: f64) -> f64 {
  1152. P0 :: -8.750608600031904122785e-01
  1153. P1 :: -1.615753718733365076637e+01
  1154. P2 :: -7.500855792314704667340e+01
  1155. P3 :: -1.228866684490136173410e+02
  1156. P4 :: -6.485021904942025371773e+01
  1157. Q0 :: +2.485846490142306297962e+01
  1158. Q1 :: +1.650270098316988542046e+02
  1159. Q2 :: +4.328810604912902668951e+02
  1160. Q3 :: +4.853903996359136964868e+02
  1161. Q4 :: +1.945506571482613964425e+02
  1162. z := x * x
  1163. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4)
  1164. z = x*z + x
  1165. return z
  1166. }
  1167. switch {
  1168. case is_nan(y) || is_nan(x):
  1169. return NAN
  1170. case y == 0:
  1171. if x >= 0 && !sign_bit(x) {
  1172. return copy_sign(0.0, y)
  1173. }
  1174. return copy_sign(PI, y)
  1175. case x == 0:
  1176. return copy_sign(PI*0.5, y)
  1177. case is_inf(x, 0):
  1178. if is_inf(x, 1) {
  1179. if is_inf(y, 0) {
  1180. return copy_sign(PI*0.25, y)
  1181. }
  1182. return copy_sign(0, y)
  1183. }
  1184. if is_inf(y, 0) {
  1185. return copy_sign(PI*0.75, y)
  1186. }
  1187. return copy_sign(PI, y)
  1188. case is_inf(y, 0):
  1189. return copy_sign(PI*0.5, y)
  1190. }
  1191. q := atan(y / x)
  1192. if x < 0 {
  1193. if q <= 0 {
  1194. return q + PI
  1195. }
  1196. return q - PI
  1197. }
  1198. return q
  1199. }
  1200. atan2_f64le :: proc(y, x: f64le) -> f64le {
  1201. // TODO(bill): Better atan2_f32
  1202. return f64le(atan2_f64(f64(y), f64(x)))
  1203. }
  1204. atan2_f64be :: proc(y, x: f64be) -> f64be {
  1205. // TODO(bill): Better atan2_f32
  1206. return f64be(atan2_f64(f64(y), f64(x)))
  1207. }
  1208. atan2 :: proc{
  1209. atan2_f16, atan2_f16le, atan2_f16be,
  1210. atan2_f32, atan2_f32le, atan2_f32be,
  1211. atan2_f64, atan2_f64le, atan2_f64be,
  1212. }
  1213. atan :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1214. return atan2(x, 1)
  1215. }
  1216. asin :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1217. return atan2(x, 1 + sqrt(1 - x*x))
  1218. }
  1219. acos :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1220. return 2 * atan2(sqrt(1 - x), sqrt(1 + x))
  1221. }
  1222. sinh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1223. return (exp(x) - exp(-x))*0.5
  1224. }
  1225. cosh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1226. return (exp(x) + exp(-x))*0.5
  1227. }
  1228. tanh :: proc(x: $T) -> T where intrinsics.type_is_float(T) {
  1229. t := exp(2*x)
  1230. return (t - 1) / (t + 1)
  1231. }
  1232. F16_DIG :: 3
  1233. F16_EPSILON :: 0.00097656
  1234. F16_GUARD :: 0
  1235. F16_MANT_DIG :: 11
  1236. F16_MAX :: 65504.0
  1237. F16_MAX_10_EXP :: 4
  1238. F16_MAX_EXP :: 15
  1239. F16_MIN :: 6.10351562e-5
  1240. F16_MIN_10_EXP :: -4
  1241. F16_MIN_EXP :: -14
  1242. F16_NORMALIZE :: 0
  1243. F16_RADIX :: 2
  1244. F16_ROUNDS :: 1
  1245. F32_DIG :: 6
  1246. F32_EPSILON :: 1.192092896e-07
  1247. F32_GUARD :: 0
  1248. F32_MANT_DIG :: 24
  1249. F32_MAX :: 3.402823466e+38
  1250. F32_MAX_10_EXP :: 38
  1251. F32_MAX_EXP :: 128
  1252. F32_MIN :: 1.175494351e-38
  1253. F32_MIN_10_EXP :: -37
  1254. F32_MIN_EXP :: -125
  1255. F32_NORMALIZE :: 0
  1256. F32_RADIX :: 2
  1257. F32_ROUNDS :: 1
  1258. F64_DIG :: 15 // # of decimal digits of precision
  1259. F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
  1260. F64_MANT_DIG :: 53 // # of bits in mantissa
  1261. F64_MAX :: 1.7976931348623158e+308 // max value
  1262. F64_MAX_10_EXP :: 308 // max decimal exponent
  1263. F64_MAX_EXP :: 1024 // max binary exponent
  1264. F64_MIN :: 2.2250738585072014e-308 // min positive value
  1265. F64_MIN_10_EXP :: -307 // min decimal exponent
  1266. F64_MIN_EXP :: -1021 // min binary exponent
  1267. F64_RADIX :: 2 // exponent radix
  1268. F64_ROUNDS :: 1 // addition rounding: near