math.odin 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. package math
  2. import "core:intrinsics"
  3. import "core:builtin"
  4. _ :: intrinsics
  5. Float_Class :: enum {
  6. Normal, // an ordinary nonzero floating point value
  7. Subnormal, // a subnormal floating point value
  8. Zero, // zero
  9. Neg_Zero, // the negative zero
  10. NaN, // Not-A-Number (NaN)
  11. Inf, // positive infinity
  12. Neg_Inf, // negative infinity
  13. }
  14. TAU :: 6.28318530717958647692528676655900576
  15. PI :: 3.14159265358979323846264338327950288
  16. E :: 2.71828182845904523536
  17. τ :: TAU
  18. π :: PI
  19. e :: E
  20. SQRT_TWO :: 1.41421356237309504880168872420969808
  21. SQRT_THREE :: 1.73205080756887729352744634150587236
  22. SQRT_FIVE :: 2.23606797749978969640917366873127623
  23. LN2 :: 0.693147180559945309417232121458176568
  24. LN10 :: 2.30258509299404568401799145468436421
  25. MAX_F64_PRECISION :: 16 // Maximum number of meaningful digits after the decimal point for 'f64'
  26. MAX_F32_PRECISION :: 8 // Maximum number of meaningful digits after the decimal point for 'f32'
  27. MAX_F16_PRECISION :: 4 // Maximum number of meaningful digits after the decimal point for 'f16'
  28. RAD_PER_DEG :: TAU/360.0
  29. DEG_PER_RAD :: 360.0/TAU
  30. abs :: builtin.abs
  31. min :: builtin.min
  32. max :: builtin.max
  33. clamp :: builtin.clamp
  34. sqrt_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(sqrt_f16(f16(x))) }
  35. sqrt_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(sqrt_f16(f16(x))) }
  36. sqrt_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(sqrt_f32(f32(x))) }
  37. sqrt_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(sqrt_f32(f32(x))) }
  38. sqrt_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(sqrt_f64(f64(x))) }
  39. sqrt_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(sqrt_f64(f64(x))) }
  40. sqrt :: proc{
  41. sqrt_f16, sqrt_f16le, sqrt_f16be,
  42. sqrt_f32, sqrt_f32le, sqrt_f32be,
  43. sqrt_f64, sqrt_f64le, sqrt_f64be,
  44. }
  45. sin_f16le :: proc "contextless" (θ: f16le) -> f16le { return #force_inline f16le(sin_f16(f16(θ))) }
  46. sin_f16be :: proc "contextless" (θ: f16be) -> f16be { return #force_inline f16be(sin_f16(f16(θ))) }
  47. sin_f32le :: proc "contextless" (θ: f32le) -> f32le { return #force_inline f32le(sin_f32(f32(θ))) }
  48. sin_f32be :: proc "contextless" (θ: f32be) -> f32be { return #force_inline f32be(sin_f32(f32(θ))) }
  49. sin_f64le :: proc "contextless" (θ: f64le) -> f64le { return #force_inline f64le(sin_f64(f64(θ))) }
  50. sin_f64be :: proc "contextless" (θ: f64be) -> f64be { return #force_inline f64be(sin_f64(f64(θ))) }
  51. sin :: proc{
  52. sin_f16, sin_f16le, sin_f16be,
  53. sin_f32, sin_f32le, sin_f32be,
  54. sin_f64, sin_f64le, sin_f64be,
  55. }
  56. cos_f16le :: proc "contextless" (θ: f16le) -> f16le { return #force_inline f16le(cos_f16(f16(θ))) }
  57. cos_f16be :: proc "contextless" (θ: f16be) -> f16be { return #force_inline f16be(cos_f16(f16(θ))) }
  58. cos_f32le :: proc "contextless" (θ: f32le) -> f32le { return #force_inline f32le(cos_f32(f32(θ))) }
  59. cos_f32be :: proc "contextless" (θ: f32be) -> f32be { return #force_inline f32be(cos_f32(f32(θ))) }
  60. cos_f64le :: proc "contextless" (θ: f64le) -> f64le { return #force_inline f64le(cos_f64(f64(θ))) }
  61. cos_f64be :: proc "contextless" (θ: f64be) -> f64be { return #force_inline f64be(cos_f64(f64(θ))) }
  62. cos :: proc{
  63. cos_f16, cos_f16le, cos_f16be,
  64. cos_f32, cos_f32le, cos_f32be,
  65. cos_f64, cos_f64le, cos_f64be,
  66. }
  67. pow_f16le :: proc "contextless" (x, power: f16le) -> f16le { return #force_inline f16le(pow_f16(f16(x), f16(power))) }
  68. pow_f16be :: proc "contextless" (x, power: f16be) -> f16be { return #force_inline f16be(pow_f16(f16(x), f16(power))) }
  69. pow_f32le :: proc "contextless" (x, power: f32le) -> f32le { return #force_inline f32le(pow_f32(f32(x), f32(power))) }
  70. pow_f32be :: proc "contextless" (x, power: f32be) -> f32be { return #force_inline f32be(pow_f32(f32(x), f32(power))) }
  71. pow_f64le :: proc "contextless" (x, power: f64le) -> f64le { return #force_inline f64le(pow_f64(f64(x), f64(power))) }
  72. pow_f64be :: proc "contextless" (x, power: f64be) -> f64be { return #force_inline f64be(pow_f64(f64(x), f64(power))) }
  73. pow :: proc{
  74. pow_f16, pow_f16le, pow_f16be,
  75. pow_f32, pow_f32le, pow_f32be,
  76. pow_f64, pow_f64le, pow_f64be,
  77. }
  78. fmuladd_f16le :: proc "contextless" (a, b, c: f16le) -> f16le { return #force_inline f16le(fmuladd_f16(f16(a), f16(b), f16(c))) }
  79. fmuladd_f16be :: proc "contextless" (a, b, c: f16be) -> f16be { return #force_inline f16be(fmuladd_f16(f16(a), f16(b), f16(c))) }
  80. fmuladd_f32le :: proc "contextless" (a, b, c: f32le) -> f32le { return #force_inline f32le(fmuladd_f32(f32(a), f32(b), f32(c))) }
  81. fmuladd_f32be :: proc "contextless" (a, b, c: f32be) -> f32be { return #force_inline f32be(fmuladd_f32(f32(a), f32(b), f32(c))) }
  82. fmuladd_f64le :: proc "contextless" (a, b, c: f64le) -> f64le { return #force_inline f64le(fmuladd_f64(f64(a), f64(b), f64(c))) }
  83. fmuladd_f64be :: proc "contextless" (a, b, c: f64be) -> f64be { return #force_inline f64be(fmuladd_f64(f64(a), f64(b), f64(c))) }
  84. fmuladd :: proc{
  85. fmuladd_f16, fmuladd_f16le, fmuladd_f16be,
  86. fmuladd_f32, fmuladd_f32le, fmuladd_f32be,
  87. fmuladd_f64, fmuladd_f64le, fmuladd_f64be,
  88. }
  89. exp_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(exp_f16(f16(x))) }
  90. exp_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(exp_f16(f16(x))) }
  91. exp_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(exp_f32(f32(x))) }
  92. exp_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(exp_f32(f32(x))) }
  93. exp_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(exp_f64(f64(x))) }
  94. exp_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(exp_f64(f64(x))) }
  95. exp :: proc{
  96. exp_f16, exp_f16le, exp_f16be,
  97. exp_f32, exp_f32le, exp_f32be,
  98. exp_f64, exp_f64le, exp_f64be,
  99. }
  100. ldexp_f64 :: proc "contextless" (val: f64, exp: int) -> f64 {
  101. mask :: F64_MASK
  102. shift :: F64_SHIFT
  103. bias :: F64_BIAS
  104. switch {
  105. case val == 0:
  106. return val
  107. case is_inf(val) || is_nan(val):
  108. return val
  109. }
  110. exp := exp
  111. frac, e := normalize_f64(val)
  112. exp += e
  113. x := transmute(u64)frac
  114. exp += int(x>>shift)&mask - bias
  115. if exp < -1075 { // underflow
  116. return copy_sign(0, frac)
  117. } else if exp > 1023 { // overflow
  118. if frac < 0 {
  119. return inf_f64(-1)
  120. }
  121. return inf_f64(+1)
  122. }
  123. m: f64 = 1
  124. if exp < -1022 { // denormal
  125. exp += 53
  126. m = 1.0 / (1<<53)
  127. }
  128. x &~= mask << shift
  129. x |= u64(exp+bias) << shift
  130. return m * transmute(f64)x
  131. }
  132. ldexp_f16 :: proc "contextless" (val: f16, exp: int) -> f16 { return f16(ldexp_f64(f64(val), exp)) }
  133. ldexp_f32 :: proc "contextless" (val: f32, exp: int) -> f32 { return f32(ldexp_f64(f64(val), exp)) }
  134. ldexp_f16le :: proc "contextless" (val: f16le, exp: int) -> f16le { return #force_inline f16le(ldexp_f16(f16(val), exp)) }
  135. ldexp_f16be :: proc "contextless" (val: f16be, exp: int) -> f16be { return #force_inline f16be(ldexp_f16(f16(val), exp)) }
  136. ldexp_f32le :: proc "contextless" (val: f32le, exp: int) -> f32le { return #force_inline f32le(ldexp_f32(f32(val), exp)) }
  137. ldexp_f32be :: proc "contextless" (val: f32be, exp: int) -> f32be { return #force_inline f32be(ldexp_f32(f32(val), exp)) }
  138. ldexp_f64le :: proc "contextless" (val: f64le, exp: int) -> f64le { return #force_inline f64le(ldexp_f64(f64(val), exp)) }
  139. ldexp_f64be :: proc "contextless" (val: f64be, exp: int) -> f64be { return #force_inline f64be(ldexp_f64(f64(val), exp)) }
  140. // ldexp is the inverse of frexp
  141. // it returns val * 2**exp.
  142. //
  143. // Special cases:
  144. // ldexp(+0, exp) = +0
  145. // ldexp(-0, exp) = -0
  146. // ldexp(+inf, exp) = +inf
  147. // ldexp(-inf, exp) = -inf
  148. // ldexp(NaN, exp) = NaN
  149. ldexp :: proc{
  150. ldexp_f16, ldexp_f16le, ldexp_f16be,
  151. ldexp_f32, ldexp_f32le, ldexp_f32be,
  152. ldexp_f64, ldexp_f64le, ldexp_f64be,
  153. }
  154. log_f16 :: proc "contextless" (x, base: f16) -> f16 { return ln(x) / ln(base) }
  155. log_f16le :: proc "contextless" (x, base: f16le) -> f16le { return f16le(log_f16(f16(x), f16(base))) }
  156. log_f16be :: proc "contextless" (x, base: f16be) -> f16be { return f16be(log_f16(f16(x), f16(base))) }
  157. log_f32 :: proc "contextless" (x, base: f32) -> f32 { return ln(x) / ln(base) }
  158. log_f32le :: proc "contextless" (x, base: f32le) -> f32le { return f32le(log_f32(f32(x), f32(base))) }
  159. log_f32be :: proc "contextless" (x, base: f32be) -> f32be { return f32be(log_f32(f32(x), f32(base))) }
  160. log_f64 :: proc "contextless" (x, base: f64) -> f64 { return ln(x) / ln(base) }
  161. log_f64le :: proc "contextless" (x, base: f64le) -> f64le { return f64le(log_f64(f64(x), f64(base))) }
  162. log_f64be :: proc "contextless" (x, base: f64be) -> f64be { return f64be(log_f64(f64(x), f64(base))) }
  163. log :: proc{
  164. log_f16, log_f16le, log_f16be,
  165. log_f32, log_f32le, log_f32be,
  166. log_f64, log_f64le, log_f64be,
  167. }
  168. log2_f16 :: proc "contextless" (x: f16) -> f16 { return log(f16(x), f16(2.0)) }
  169. log2_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(log_f16(f16(x), f16(2.0))) }
  170. log2_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(log_f16(f16(x), f16(2.0))) }
  171. log2_f32 :: proc "contextless" (x: f32) -> f32 { return log(f32(x), f32(2.0)) }
  172. log2_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(log_f32(f32(x), f32(2.0))) }
  173. log2_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(log_f32(f32(x), f32(2.0))) }
  174. log2_f64 :: proc "contextless" (x: f64) -> f64 { return log(f64(x), f64(2.0)) }
  175. log2_f64le :: proc "contextless" (x: f64le) -> f64le { return f64le(log_f64(f64(x), f64(2.0))) }
  176. log2_f64be :: proc "contextless" (x: f64be) -> f64be { return f64be(log_f64(f64(x), f64(2.0))) }
  177. log2 :: proc{
  178. log2_f16, log2_f16le, log2_f16be,
  179. log2_f32, log2_f32le, log2_f32be,
  180. log2_f64, log2_f64le, log2_f64be,
  181. }
  182. log10_f16 :: proc "contextless" (x: f16) -> f16 { return ln(x)/LN10 }
  183. log10_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(log10_f16(f16(x))) }
  184. log10_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(log10_f16(f16(x))) }
  185. log10_f32 :: proc "contextless" (x: f32) -> f32 { return ln(x)/LN10 }
  186. log10_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(log10_f32(f32(x))) }
  187. log10_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(log10_f32(f32(x))) }
  188. log10_f64 :: proc "contextless" (x: f64) -> f64 { return ln(x)/LN10 }
  189. log10_f64le :: proc "contextless" (x: f64le) -> f64le { return f64le(log10_f64(f64(x))) }
  190. log10_f64be :: proc "contextless" (x: f64be) -> f64be { return f64be(log10_f64(f64(x))) }
  191. log10 :: proc{
  192. log10_f16, log10_f16le, log10_f16be,
  193. log10_f32, log10_f32le, log10_f32be,
  194. log10_f64, log10_f64le, log10_f64be,
  195. }
  196. tan_f16 :: proc "contextless" (θ: f16) -> f16 { return sin(θ)/cos(θ) }
  197. tan_f16le :: proc "contextless" (θ: f16le) -> f16le { return f16le(tan_f16(f16(θ))) }
  198. tan_f16be :: proc "contextless" (θ: f16be) -> f16be { return f16be(tan_f16(f16(θ))) }
  199. tan_f32 :: proc "contextless" (θ: f32) -> f32 { return sin(θ)/cos(θ) }
  200. tan_f32le :: proc "contextless" (θ: f32le) -> f32le { return f32le(tan_f32(f32(θ))) }
  201. tan_f32be :: proc "contextless" (θ: f32be) -> f32be { return f32be(tan_f32(f32(θ))) }
  202. tan_f64 :: proc "contextless" (θ: f64) -> f64 { return sin(θ)/cos(θ) }
  203. tan_f64le :: proc "contextless" (θ: f64le) -> f64le { return f64le(tan_f64(f64(θ))) }
  204. tan_f64be :: proc "contextless" (θ: f64be) -> f64be { return f64be(tan_f64(f64(θ))) }
  205. tan :: proc{
  206. tan_f16, tan_f16le, tan_f16be,
  207. tan_f32, tan_f32le, tan_f32be,
  208. tan_f64, tan_f64le, tan_f64be,
  209. }
  210. lerp :: proc "contextless" (a, b: $T, t: $E) -> (x: T) { return a*(1-t) + b*t }
  211. saturate :: proc "contextless" (a: $T) -> (x: T) { return clamp(a, 0, 1) }
  212. unlerp :: proc "contextless" (a, b, x: $T) -> (t: T) where intrinsics.type_is_float(T), !intrinsics.type_is_array(T) {
  213. return (x-a)/(b-a)
  214. }
  215. remap :: proc "contextless" (old_value, old_min, old_max, new_min, new_max: $T) -> (x: T) where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  216. old_range := old_max - old_min
  217. new_range := new_max - new_min
  218. if old_range == 0 {
  219. return new_range / 2
  220. }
  221. return ((old_value - old_min) / old_range) * new_range + new_min
  222. }
  223. wrap :: proc "contextless" (x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  224. tmp := mod(x, y)
  225. return y + tmp if tmp < 0 else tmp
  226. }
  227. angle_diff :: proc "contextless" (a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  228. dist := wrap(b - a, TAU)
  229. return wrap(dist*2, TAU) - dist
  230. }
  231. angle_lerp :: proc "contextless" (a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  232. return a + angle_diff(a, b) * t
  233. }
  234. step :: proc "contextless" (edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  235. return 0 if x < edge else 1
  236. }
  237. smoothstep :: proc "contextless" (edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
  238. t := clamp((x - edge0) / (edge1 - edge0), 0, 1)
  239. return t * t * (3 - 2*t)
  240. }
  241. bias :: proc "contextless" (t, b: $T) -> T where intrinsics.type_is_numeric(T) {
  242. return t / (((1/b) - 2) * (1 - t) + 1)
  243. }
  244. gain :: proc "contextless" (t, g: $T) -> T where intrinsics.type_is_numeric(T) {
  245. if t < 0.5 {
  246. return bias(t*2, g)*0.5
  247. }
  248. return bias(t*2 - 1, 1 - g)*0.5 + 0.5
  249. }
  250. sign_f16 :: proc "contextless" (x: f16) -> f16 { return f16(int(0 < x) - int(x < 0)) }
  251. sign_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(int(0 < x) - int(x < 0)) }
  252. sign_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(int(0 < x) - int(x < 0)) }
  253. sign_f32 :: proc "contextless" (x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)) }
  254. sign_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(int(0 < x) - int(x < 0)) }
  255. sign_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(int(0 < x) - int(x < 0)) }
  256. sign_f64 :: proc "contextless" (x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)) }
  257. sign_f64le :: proc "contextless" (x: f64le) -> f64le { return f64le(int(0 < x) - int(x < 0)) }
  258. sign_f64be :: proc "contextless" (x: f64be) -> f64be { return f64be(int(0 < x) - int(x < 0)) }
  259. sign :: proc{
  260. sign_f16, sign_f16le, sign_f16be,
  261. sign_f32, sign_f32le, sign_f32be,
  262. sign_f64, sign_f64le, sign_f64be,
  263. }
  264. sign_bit_f16 :: proc "contextless" (x: f16) -> bool {
  265. return (transmute(u16)x) & (1<<15) != 0
  266. }
  267. sign_bit_f16le :: proc "contextless" (x: f16le) -> bool { return #force_inline sign_bit_f16(f16(x)) }
  268. sign_bit_f16be :: proc "contextless" (x: f16be) -> bool { return #force_inline sign_bit_f16(f16(x)) }
  269. sign_bit_f32 :: proc "contextless" (x: f32) -> bool {
  270. return (transmute(u32)x) & (1<<31) != 0
  271. }
  272. sign_bit_f32le :: proc "contextless" (x: f32le) -> bool { return #force_inline sign_bit_f32(f32(x)) }
  273. sign_bit_f32be :: proc "contextless" (x: f32be) -> bool { return #force_inline sign_bit_f32(f32(x)) }
  274. sign_bit_f64 :: proc "contextless" (x: f64) -> bool {
  275. return (transmute(u64)x) & (1<<63) != 0
  276. }
  277. sign_bit_f64le :: proc "contextless" (x: f64le) -> bool { return #force_inline sign_bit_f64(f64(x)) }
  278. sign_bit_f64be :: proc "contextless" (x: f64be) -> bool { return #force_inline sign_bit_f64(f64(x)) }
  279. sign_bit :: proc{
  280. sign_bit_f16, sign_bit_f16le, sign_bit_f16be,
  281. sign_bit_f32, sign_bit_f32le, sign_bit_f32be,
  282. sign_bit_f64, sign_bit_f64le, sign_bit_f64be,
  283. }
  284. copy_sign_f16 :: proc "contextless" (x, y: f16) -> f16 {
  285. ix := transmute(u16)x
  286. iy := transmute(u16)y
  287. ix &= 0x7fff
  288. ix |= iy & 0x8000
  289. return transmute(f16)ix
  290. }
  291. copy_sign_f16le :: proc "contextless" (x, y: f16le) -> f16le { return #force_inline f16le(copy_sign_f16(f16(x), f16(y))) }
  292. copy_sign_f16be :: proc "contextless" (x, y: f16be) -> f16be { return #force_inline f16be(copy_sign_f16(f16(x), f16(y))) }
  293. copy_sign_f32 :: proc "contextless" (x, y: f32) -> f32 {
  294. ix := transmute(u32)x
  295. iy := transmute(u32)y
  296. ix &= 0x7fff_ffff
  297. ix |= iy & 0x8000_0000
  298. return transmute(f32)ix
  299. }
  300. copy_sign_f32le :: proc "contextless" (x, y: f32le) -> f32le { return #force_inline f32le(copy_sign_f32(f32(x), f32(y))) }
  301. copy_sign_f32be :: proc "contextless" (x, y: f32be) -> f32be { return #force_inline f32be(copy_sign_f32(f32(x), f32(y))) }
  302. copy_sign_f64 :: proc "contextless" (x, y: f64) -> f64 {
  303. ix := transmute(u64)x
  304. iy := transmute(u64)y
  305. ix &= 0x7fff_ffff_ffff_ffff
  306. ix |= iy & 0x8000_0000_0000_0000
  307. return transmute(f64)ix
  308. }
  309. copy_sign_f64le :: proc "contextless" (x, y: f64le) -> f64le { return #force_inline f64le(copy_sign_f64(f64(x), f64(y))) }
  310. copy_sign_f64be :: proc "contextless" (x, y: f64be) -> f64be { return #force_inline f64be(copy_sign_f64(f64(x), f64(y))) }
  311. copy_sign :: proc{
  312. copy_sign_f16, copy_sign_f16le, copy_sign_f16be,
  313. copy_sign_f32, copy_sign_f32le, copy_sign_f32be,
  314. copy_sign_f64, copy_sign_f64le, copy_sign_f64be,
  315. }
  316. to_radians_f16 :: proc "contextless" (degrees: f16) -> f16 { return degrees * RAD_PER_DEG }
  317. to_radians_f16le :: proc "contextless" (degrees: f16le) -> f16le { return degrees * RAD_PER_DEG }
  318. to_radians_f16be :: proc "contextless" (degrees: f16be) -> f16be { return degrees * RAD_PER_DEG }
  319. to_radians_f32 :: proc "contextless" (degrees: f32) -> f32 { return degrees * RAD_PER_DEG }
  320. to_radians_f32le :: proc "contextless" (degrees: f32le) -> f32le { return degrees * RAD_PER_DEG }
  321. to_radians_f32be :: proc "contextless" (degrees: f32be) -> f32be { return degrees * RAD_PER_DEG }
  322. to_radians_f64 :: proc "contextless" (degrees: f64) -> f64 { return degrees * RAD_PER_DEG }
  323. to_radians_f64le :: proc "contextless" (degrees: f64le) -> f64le { return degrees * RAD_PER_DEG }
  324. to_radians_f64be :: proc "contextless" (degrees: f64be) -> f64be { return degrees * RAD_PER_DEG }
  325. to_degrees_f16 :: proc "contextless" (radians: f16) -> f16 { return radians * DEG_PER_RAD }
  326. to_degrees_f16le :: proc "contextless" (radians: f16le) -> f16le { return radians * DEG_PER_RAD }
  327. to_degrees_f16be :: proc "contextless" (radians: f16be) -> f16be { return radians * DEG_PER_RAD }
  328. to_degrees_f32 :: proc "contextless" (radians: f32) -> f32 { return radians * DEG_PER_RAD }
  329. to_degrees_f32le :: proc "contextless" (radians: f32le) -> f32le { return radians * DEG_PER_RAD }
  330. to_degrees_f32be :: proc "contextless" (radians: f32be) -> f32be { return radians * DEG_PER_RAD }
  331. to_degrees_f64 :: proc "contextless" (radians: f64) -> f64 { return radians * DEG_PER_RAD }
  332. to_degrees_f64le :: proc "contextless" (radians: f64le) -> f64le { return radians * DEG_PER_RAD }
  333. to_degrees_f64be :: proc "contextless" (radians: f64be) -> f64be { return radians * DEG_PER_RAD }
  334. to_radians :: proc{
  335. to_radians_f16, to_radians_f16le, to_radians_f16be,
  336. to_radians_f32, to_radians_f32le, to_radians_f32be,
  337. to_radians_f64, to_radians_f64le, to_radians_f64be,
  338. }
  339. to_degrees :: proc{
  340. to_degrees_f16, to_degrees_f16le, to_degrees_f16be,
  341. to_degrees_f32, to_degrees_f32le, to_degrees_f32be,
  342. to_degrees_f64, to_degrees_f64le, to_degrees_f64be,
  343. }
  344. trunc_f16 :: proc "contextless" (x: f16) -> f16 {
  345. trunc_internal :: proc "contextless" (f: f16) -> f16 {
  346. mask :: F16_MASK
  347. shift :: F16_SHIFT
  348. bias :: F16_BIAS
  349. if f < 1 {
  350. switch {
  351. case f < 0: return -trunc_internal(-f)
  352. case f == 0: return f
  353. case: return 0
  354. }
  355. }
  356. x := transmute(u16)f
  357. e := (x >> shift) & mask - bias
  358. if e < shift {
  359. x &~= 1 << (shift-e) - 1
  360. }
  361. return transmute(f16)x
  362. }
  363. switch classify(x) {
  364. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  365. return x
  366. case .Normal, .Subnormal: // carry on
  367. }
  368. return trunc_internal(x)
  369. }
  370. trunc_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(trunc_f16(f16(x))) }
  371. trunc_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(trunc_f16(f16(x))) }
  372. trunc_f32 :: proc "contextless" (x: f32) -> f32 {
  373. trunc_internal :: proc "contextless" (f: f32) -> f32 {
  374. mask :: F32_MASK
  375. shift :: F32_SHIFT
  376. bias :: F32_BIAS
  377. if f < 1 {
  378. switch {
  379. case f < 0: return -trunc_internal(-f)
  380. case f == 0: return f
  381. case: return 0
  382. }
  383. }
  384. x := transmute(u32)f
  385. e := (x >> shift) & mask - bias
  386. if e < shift {
  387. x &~= 1 << (shift-e) - 1
  388. }
  389. return transmute(f32)x
  390. }
  391. switch classify(x) {
  392. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  393. return x
  394. case .Normal, .Subnormal: // carry on
  395. }
  396. return trunc_internal(x)
  397. }
  398. trunc_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(trunc_f32(f32(x))) }
  399. trunc_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(trunc_f32(f32(x))) }
  400. trunc_f64 :: proc "contextless" (x: f64) -> f64 {
  401. trunc_internal :: proc "contextless" (f: f64) -> f64 {
  402. mask :: F64_MASK
  403. shift :: F64_SHIFT
  404. bias :: F64_BIAS
  405. if f < 1 {
  406. switch {
  407. case f < 0: return -trunc_internal(-f)
  408. case f == 0: return f
  409. case: return 0
  410. }
  411. }
  412. x := transmute(u64)f
  413. e := (x >> shift) & mask - bias
  414. if e < shift {
  415. x &~= 1 << (shift-e) - 1
  416. }
  417. return transmute(f64)x
  418. }
  419. switch classify(x) {
  420. case .Zero, .Neg_Zero, .NaN, .Inf, .Neg_Inf:
  421. return x
  422. case .Normal, .Subnormal: // carry on
  423. }
  424. return trunc_internal(x)
  425. }
  426. trunc_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(trunc_f64(f64(x))) }
  427. trunc_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(trunc_f64(f64(x))) }
  428. // Removes the fractional part of the value, i.e. rounds towards zero.
  429. trunc :: proc{
  430. trunc_f16, trunc_f16le, trunc_f16be,
  431. trunc_f32, trunc_f32le, trunc_f32be,
  432. trunc_f64, trunc_f64le, trunc_f64be,
  433. }
  434. round_f16 :: proc "contextless" (x: f16) -> f16 {
  435. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  436. }
  437. round_f16le :: proc "contextless" (x: f16le) -> f16le {
  438. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  439. }
  440. round_f16be :: proc "contextless" (x: f16be) -> f16be {
  441. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  442. }
  443. round_f32 :: proc "contextless" (x: f32) -> f32 {
  444. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  445. }
  446. round_f32le :: proc "contextless" (x: f32le) -> f32le {
  447. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  448. }
  449. round_f32be :: proc "contextless" (x: f32be) -> f32be {
  450. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  451. }
  452. round_f64 :: proc "contextless" (x: f64) -> f64 {
  453. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  454. }
  455. round_f64le :: proc "contextless" (x: f64le) -> f64le {
  456. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  457. }
  458. round_f64be :: proc "contextless" (x: f64be) -> f64be {
  459. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  460. }
  461. round :: proc{
  462. round_f16, round_f16le, round_f16be,
  463. round_f32, round_f32le, round_f32be,
  464. round_f64, round_f64le, round_f64be,
  465. }
  466. ceil_f16 :: proc "contextless" (x: f16) -> f16 { return -floor(-x) }
  467. ceil_f16le :: proc "contextless" (x: f16le) -> f16le { return -floor(-x) }
  468. ceil_f16be :: proc "contextless" (x: f16be) -> f16be { return -floor(-x) }
  469. ceil_f32 :: proc "contextless" (x: f32) -> f32 { return -floor(-x) }
  470. ceil_f32le :: proc "contextless" (x: f32le) -> f32le { return -floor(-x) }
  471. ceil_f32be :: proc "contextless" (x: f32be) -> f32be { return -floor(-x) }
  472. ceil_f64 :: proc "contextless" (x: f64) -> f64 { return -floor(-x) }
  473. ceil_f64le :: proc "contextless" (x: f64le) -> f64le { return -floor(-x) }
  474. ceil_f64be :: proc "contextless" (x: f64be) -> f64be { return -floor(-x) }
  475. ceil :: proc{
  476. ceil_f16, ceil_f16le, ceil_f16be,
  477. ceil_f32, ceil_f32le, ceil_f32be,
  478. ceil_f64, ceil_f64le, ceil_f64be,
  479. }
  480. floor_f16 :: proc "contextless" (x: f16) -> f16 {
  481. if x == 0 || is_nan(x) || is_inf(x) {
  482. return x
  483. }
  484. if x < 0 {
  485. d, fract := modf(-x)
  486. if fract != 0.0 {
  487. d = d + 1
  488. }
  489. return -d
  490. }
  491. d, _ := modf(x)
  492. return d
  493. }
  494. floor_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(floor_f16(f16(x))) }
  495. floor_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(floor_f16(f16(x))) }
  496. floor_f32 :: proc "contextless" (x: f32) -> f32 {
  497. if x == 0 || is_nan(x) || is_inf(x) {
  498. return x
  499. }
  500. if x < 0 {
  501. d, fract := modf(-x)
  502. if fract != 0.0 {
  503. d = d + 1
  504. }
  505. return -d
  506. }
  507. d, _ := modf(x)
  508. return d
  509. }
  510. floor_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(floor_f32(f32(x))) }
  511. floor_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(floor_f32(f32(x))) }
  512. floor_f64 :: proc "contextless" (x: f64) -> f64 {
  513. if x == 0 || is_nan(x) || is_inf(x) {
  514. return x
  515. }
  516. if x < 0 {
  517. d, fract := modf(-x)
  518. if fract != 0.0 {
  519. d = d + 1
  520. }
  521. return -d
  522. }
  523. d, _ := modf(x)
  524. return d
  525. }
  526. floor_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(floor_f64(f64(x))) }
  527. floor_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(floor_f64(f64(x))) }
  528. floor :: proc{
  529. floor_f16, floor_f16le, floor_f16be,
  530. floor_f32, floor_f32le, floor_f32be,
  531. floor_f64, floor_f64le, floor_f64be,
  532. }
  533. floor_div :: proc "contextless" (x, y: $T) -> T
  534. where intrinsics.type_is_integer(T) {
  535. a := x / y
  536. r := x % y
  537. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  538. a -= 1
  539. }
  540. return a
  541. }
  542. floor_mod :: proc "contextless" (x, y: $T) -> T
  543. where intrinsics.type_is_integer(T) {
  544. r := x % y
  545. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  546. r += y
  547. }
  548. return r
  549. }
  550. divmod :: #force_inline proc "contextless" (x, y: $T) -> (div, mod: T)
  551. where intrinsics.type_is_integer(T) {
  552. div = x / y
  553. mod = x % y
  554. return
  555. }
  556. floor_divmod :: #force_inline proc "contextless" (x, y: $T) -> (div, mod: T)
  557. where intrinsics.type_is_integer(T) {
  558. div = x / y
  559. mod = x % y
  560. if (div > 0 && y < 0) || (mod < 0 && y > 0) {
  561. div -= 1
  562. mod += y
  563. }
  564. return
  565. }
  566. modf_f16 :: proc "contextless" (x: f16) -> (int: f16, frac: f16) {
  567. shift :: F16_SHIFT
  568. mask :: F16_MASK
  569. bias :: F16_BIAS
  570. if x < 1 {
  571. switch {
  572. case x < 0:
  573. int, frac = modf(-x)
  574. return -int, -frac
  575. case x == 0:
  576. return x, x
  577. }
  578. return 0, x
  579. }
  580. i := transmute(u16)x
  581. e := uint(i>>shift)&mask - bias
  582. if e < shift {
  583. i &~= 1<<(shift-e) - 1
  584. }
  585. int = transmute(f16)i
  586. frac = x - int
  587. return
  588. }
  589. modf_f16le :: proc "contextless" (x: f16le) -> (int: f16le, frac: f16le) {
  590. i, f := #force_inline modf_f16(f16(x))
  591. return f16le(i), f16le(f)
  592. }
  593. modf_f16be :: proc "contextless" (x: f16be) -> (int: f16be, frac: f16be) {
  594. i, f := #force_inline modf_f16(f16(x))
  595. return f16be(i), f16be(f)
  596. }
  597. modf_f32 :: proc "contextless" (x: f32) -> (int: f32, frac: f32) {
  598. shift :: F32_SHIFT
  599. mask :: F32_MASK
  600. bias :: F32_BIAS
  601. if x < 1 {
  602. switch {
  603. case x < 0:
  604. int, frac = modf(-x)
  605. return -int, -frac
  606. case x == 0:
  607. return x, x
  608. }
  609. return 0, x
  610. }
  611. i := transmute(u32)x
  612. e := uint(i>>shift)&mask - bias
  613. if e < shift {
  614. i &~= 1<<(shift-e) - 1
  615. }
  616. int = transmute(f32)i
  617. frac = x - int
  618. return
  619. }
  620. modf_f32le :: proc "contextless" (x: f32le) -> (int: f32le, frac: f32le) {
  621. i, f := #force_inline modf_f32(f32(x))
  622. return f32le(i), f32le(f)
  623. }
  624. modf_f32be :: proc "contextless" (x: f32be) -> (int: f32be, frac: f32be) {
  625. i, f := #force_inline modf_f32(f32(x))
  626. return f32be(i), f32be(f)
  627. }
  628. modf_f64 :: proc "contextless" (x: f64) -> (int: f64, frac: f64) {
  629. shift :: F64_SHIFT
  630. mask :: F64_MASK
  631. bias :: F64_BIAS
  632. if x < 1 {
  633. switch {
  634. case x < 0:
  635. int, frac = modf(-x)
  636. return -int, -frac
  637. case x == 0:
  638. return x, x
  639. }
  640. return 0, x
  641. }
  642. i := transmute(u64)x
  643. e := uint(i>>shift)&mask - bias
  644. if e < shift {
  645. i &~= 1<<(shift-e) - 1
  646. }
  647. int = transmute(f64)i
  648. frac = x - int
  649. return
  650. }
  651. modf_f64le :: proc "contextless" (x: f64le) -> (int: f64le, frac: f64le) {
  652. i, f := #force_inline modf_f64(f64(x))
  653. return f64le(i), f64le(f)
  654. }
  655. modf_f64be :: proc "contextless" (x: f64be) -> (int: f64be, frac: f64be) {
  656. i, f := #force_inline modf_f64(f64(x))
  657. return f64be(i), f64be(f)
  658. }
  659. modf :: proc{
  660. modf_f16, modf_f16le, modf_f16be,
  661. modf_f32, modf_f32le, modf_f32be,
  662. modf_f64, modf_f64le, modf_f64be,
  663. }
  664. split_decimal :: modf
  665. mod_f16 :: proc "contextless" (x, y: f16) -> (n: f16) {
  666. z := abs(y)
  667. n = remainder(abs(x), z)
  668. if sign(n) < 0 {
  669. n += z
  670. }
  671. return copy_sign(n, x)
  672. }
  673. mod_f16le :: proc "contextless" (x, y: f16le) -> (n: f16le) { return #force_inline f16le(mod_f16(f16(x), f16(y))) }
  674. mod_f16be :: proc "contextless" (x, y: f16be) -> (n: f16be) { return #force_inline f16be(mod_f16(f16(x), f16(y))) }
  675. mod_f32 :: proc "contextless" (x, y: f32) -> (n: f32) {
  676. z := abs(y)
  677. n = remainder(abs(x), z)
  678. if sign(n) < 0 {
  679. n += z
  680. }
  681. return copy_sign(n, x)
  682. }
  683. mod_f32le :: proc "contextless" (x, y: f32le) -> (n: f32le) { return #force_inline f32le(mod_f32(f32(x), f32(y))) }
  684. mod_f32be :: proc "contextless" (x, y: f32be) -> (n: f32be) { return #force_inline f32be(mod_f32(f32(x), f32(y))) }
  685. mod_f64 :: proc "contextless" (x, y: f64) -> (n: f64) {
  686. z := abs(y)
  687. n = remainder(abs(x), z)
  688. if sign(n) < 0 {
  689. n += z
  690. }
  691. return copy_sign(n, x)
  692. }
  693. mod_f64le :: proc "contextless" (x, y: f64le) -> (n: f64le) { return #force_inline f64le(mod_f64(f64(x), f64(y))) }
  694. mod_f64be :: proc "contextless" (x, y: f64be) -> (n: f64be) { return #force_inline f64be(mod_f64(f64(x), f64(y))) }
  695. mod :: proc{
  696. mod_f16, mod_f16le, mod_f16be,
  697. mod_f32, mod_f32le, mod_f32be,
  698. mod_f64, mod_f64le, mod_f64be,
  699. }
  700. remainder_f16 :: proc "contextless" (x, y: f16 ) -> f16 { return x - round(x/y) * y }
  701. remainder_f16le :: proc "contextless" (x, y: f16le) -> f16le { return x - round(x/y) * y }
  702. remainder_f16be :: proc "contextless" (x, y: f16be) -> f16be { return x - round(x/y) * y }
  703. remainder_f32 :: proc "contextless" (x, y: f32 ) -> f32 { return x - round(x/y) * y }
  704. remainder_f32le :: proc "contextless" (x, y: f32le) -> f32le { return x - round(x/y) * y }
  705. remainder_f32be :: proc "contextless" (x, y: f32be) -> f32be { return x - round(x/y) * y }
  706. remainder_f64 :: proc "contextless" (x, y: f64 ) -> f64 { return x - round(x/y) * y }
  707. remainder_f64le :: proc "contextless" (x, y: f64le) -> f64le { return x - round(x/y) * y }
  708. remainder_f64be :: proc "contextless" (x, y: f64be) -> f64be { return x - round(x/y) * y }
  709. remainder :: proc{
  710. remainder_f16, remainder_f16le, remainder_f16be,
  711. remainder_f32, remainder_f32le, remainder_f32be,
  712. remainder_f64, remainder_f64le, remainder_f64be,
  713. }
  714. gcd :: proc "contextless" (x, y: $T) -> T
  715. where intrinsics.type_is_ordered_numeric(T) {
  716. x, y := x, y
  717. for y != 0 {
  718. x %= y
  719. x, y = y, x
  720. }
  721. return abs(x)
  722. }
  723. lcm :: proc "contextless" (x, y: $T) -> T
  724. where intrinsics.type_is_ordered_numeric(T) {
  725. return x / gcd(x, y) * y
  726. }
  727. normalize_f16 :: proc "contextless" (x: f16) -> (y: f16, exponent: int) {
  728. if abs(x) < F16_MIN {
  729. return x * (1<<F16_SHIFT), -F16_SHIFT
  730. }
  731. return x, 0
  732. }
  733. normalize_f32 :: proc "contextless" (x: f32) -> (y: f32, exponent: int) {
  734. if abs(x) < F32_MIN {
  735. return x * (1<<F32_SHIFT), -F32_SHIFT
  736. }
  737. return x, 0
  738. }
  739. normalize_f64 :: proc "contextless" (x: f64) -> (y: f64, exponent: int) {
  740. if abs(x) < F64_MIN {
  741. return x * (1<<F64_SHIFT), -F64_SHIFT
  742. }
  743. return x, 0
  744. }
  745. normalize_f16le :: proc "contextless" (x: f16le) -> (y: f16le, exponent: int) { y0, e := normalize_f16(f16(x)); return f16le(y0), e }
  746. normalize_f16be :: proc "contextless" (x: f16be) -> (y: f16be, exponent: int) { y0, e := normalize_f16(f16(x)); return f16be(y0), e }
  747. normalize_f32le :: proc "contextless" (x: f32le) -> (y: f32le, exponent: int) { y0, e := normalize_f32(f32(x)); return f32le(y0), e }
  748. normalize_f32be :: proc "contextless" (x: f32be) -> (y: f32be, exponent: int) { y0, e := normalize_f32(f32(x)); return f32be(y0), e }
  749. normalize_f64le :: proc "contextless" (x: f64le) -> (y: f64le, exponent: int) { y0, e := normalize_f64(f64(x)); return f64le(y0), e }
  750. normalize_f64be :: proc "contextless" (x: f64be) -> (y: f64be, exponent: int) { y0, e := normalize_f64(f64(x)); return f64be(y0), e }
  751. normalize :: proc{
  752. normalize_f16,
  753. normalize_f32,
  754. normalize_f64,
  755. normalize_f16le,
  756. normalize_f16be,
  757. normalize_f32le,
  758. normalize_f32be,
  759. normalize_f64le,
  760. normalize_f64be,
  761. }
  762. frexp_f16 :: proc "contextless" (x: f16) -> (significand: f16, exponent: int) {
  763. f, e := frexp_f64(f64(x))
  764. return f16(f), e
  765. }
  766. frexp_f16le :: proc "contextless" (x: f16le) -> (significand: f16le, exponent: int) {
  767. f, e := frexp_f64(f64(x))
  768. return f16le(f), e
  769. }
  770. frexp_f16be :: proc "contextless" (x: f16be) -> (significand: f16be, exponent: int) {
  771. f, e := frexp_f64(f64(x))
  772. return f16be(f), e
  773. }
  774. frexp_f32 :: proc "contextless" (x: f32) -> (significand: f32, exponent: int) {
  775. f, e := frexp_f64(f64(x))
  776. return f32(f), e
  777. }
  778. frexp_f32le :: proc "contextless" (x: f32le) -> (significand: f32le, exponent: int) {
  779. f, e := frexp_f64(f64(x))
  780. return f32le(f), e
  781. }
  782. frexp_f32be :: proc "contextless" (x: f32be) -> (significand: f32be, exponent: int) {
  783. f, e := frexp_f64(f64(x))
  784. return f32be(f), e
  785. }
  786. frexp_f64 :: proc "contextless" (f: f64) -> (significand: f64, exponent: int) {
  787. mask :: F64_MASK
  788. shift :: F64_SHIFT
  789. bias :: F64_BIAS
  790. switch {
  791. case f == 0:
  792. return 0, 0
  793. case is_inf(f) || is_nan(f):
  794. return f, 0
  795. }
  796. f := f
  797. f, exponent = normalize_f64(f)
  798. x := transmute(u64)f
  799. exponent += int((x>>shift)&mask) - bias + 1
  800. x &~= mask << shift
  801. x |= (-1 + bias) << shift
  802. significand = transmute(f64)x
  803. return
  804. }
  805. frexp_f64le :: proc "contextless" (x: f64le) -> (significand: f64le, exponent: int) {
  806. f, e := frexp_f64(f64(x))
  807. return f64le(f), e
  808. }
  809. frexp_f64be :: proc "contextless" (x: f64be) -> (significand: f64be, exponent: int) {
  810. f, e := frexp_f64(f64(x))
  811. return f64be(f), e
  812. }
  813. // frexp breaks the value into a normalized fraction, and an integral power of two
  814. // It returns a significand and exponent satisfying x == significand * 2**exponent
  815. // with the absolute value of significand in the intervalue of [0.5, 1).
  816. //
  817. // Special cases:
  818. // frexp(+0) = +0, 0
  819. // frexp(-0) = -0, 0
  820. // frexp(+inf) = +inf, 0
  821. // frexp(-inf) = -inf, 0
  822. // frexp(NaN) = NaN, 0
  823. frexp :: proc{
  824. frexp_f16, frexp_f16le, frexp_f16be,
  825. frexp_f32, frexp_f32le, frexp_f32be,
  826. frexp_f64, frexp_f64le, frexp_f64be,
  827. }
  828. binomial :: proc "contextless" (n, k: int) -> int {
  829. switch {
  830. case k <= 0: return 1
  831. case 2*k > n: return binomial(n, n-k)
  832. }
  833. b := n
  834. for i in 2..<k {
  835. b = (b * (n+1-i))/i
  836. }
  837. return b
  838. }
  839. factorial :: proc "contextless" (n: int) -> int {
  840. when size_of(int) == size_of(i64) {
  841. @static table := [21]int{
  842. 1,
  843. 1,
  844. 2,
  845. 6,
  846. 24,
  847. 120,
  848. 720,
  849. 5_040,
  850. 40_320,
  851. 362_880,
  852. 3_628_800,
  853. 39_916_800,
  854. 479_001_600,
  855. 6_227_020_800,
  856. 87_178_291_200,
  857. 1_307_674_368_000,
  858. 20_922_789_888_000,
  859. 355_687_428_096_000,
  860. 6_402_373_705_728_000,
  861. 121_645_100_408_832_000,
  862. 2_432_902_008_176_640_000,
  863. }
  864. } else {
  865. @static table := [13]int{
  866. 1,
  867. 1,
  868. 2,
  869. 6,
  870. 24,
  871. 120,
  872. 720,
  873. 5_040,
  874. 40_320,
  875. 362_880,
  876. 3_628_800,
  877. 39_916_800,
  878. 479_001_600,
  879. }
  880. }
  881. return table[n]
  882. }
  883. classify_f16 :: proc "contextless" (x: f16) -> Float_Class {
  884. switch {
  885. case x == 0:
  886. i := transmute(i16)x
  887. if i < 0 {
  888. return .Neg_Zero
  889. }
  890. return .Zero
  891. case x*0.25 == x:
  892. if x < 0 {
  893. return .Neg_Inf
  894. }
  895. return .Inf
  896. case !(x == x):
  897. return .NaN
  898. }
  899. u := transmute(u16)x
  900. exp := int(u>>10) & (1<<5 - 1)
  901. if exp == 0 {
  902. return .Subnormal
  903. }
  904. return .Normal
  905. }
  906. classify_f16le :: proc "contextless" (x: f16le) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  907. classify_f16be :: proc "contextless" (x: f16be) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  908. classify_f32 :: proc "contextless" (x: f32) -> Float_Class {
  909. switch {
  910. case x == 0:
  911. i := transmute(i32)x
  912. if i < 0 {
  913. return .Neg_Zero
  914. }
  915. return .Zero
  916. case x*0.5 == x:
  917. if x < 0 {
  918. return .Neg_Inf
  919. }
  920. return .Inf
  921. case !(x == x):
  922. return .NaN
  923. }
  924. u := transmute(u32)x
  925. exp := int(u>>23) & (1<<8 - 1)
  926. if exp == 0 {
  927. return .Subnormal
  928. }
  929. return .Normal
  930. }
  931. classify_f32le :: proc "contextless" (x: f32le) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  932. classify_f32be :: proc "contextless" (x: f32be) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  933. classify_f64 :: proc "contextless" (x: f64) -> Float_Class {
  934. switch {
  935. case x == 0:
  936. i := transmute(i64)x
  937. if i < 0 {
  938. return .Neg_Zero
  939. }
  940. return .Zero
  941. case x*0.5 == x:
  942. if x < 0 {
  943. return .Neg_Inf
  944. }
  945. return .Inf
  946. case !(x == x):
  947. return .NaN
  948. }
  949. u := transmute(u64)x
  950. exp := int(u>>52) & (1<<11 - 1)
  951. if exp == 0 {
  952. return .Subnormal
  953. }
  954. return .Normal
  955. }
  956. classify_f64le :: proc "contextless" (x: f64le) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  957. classify_f64be :: proc "contextless" (x: f64be) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  958. // Returns the `Float_Class` of the value, i.e. whether normal, subnormal, zero, negative zero, NaN, infinity or
  959. // negative infinity.
  960. classify :: proc{
  961. classify_f16, classify_f16le, classify_f16be,
  962. classify_f32, classify_f32le, classify_f32be,
  963. classify_f64, classify_f64le, classify_f64be,
  964. }
  965. is_nan_f16 :: proc "contextless" (x: f16) -> bool { return classify(x) == .NaN }
  966. is_nan_f16le :: proc "contextless" (x: f16le) -> bool { return classify(x) == .NaN }
  967. is_nan_f16be :: proc "contextless" (x: f16be) -> bool { return classify(x) == .NaN }
  968. is_nan_f32 :: proc "contextless" (x: f32) -> bool { return classify(x) == .NaN }
  969. is_nan_f32le :: proc "contextless" (x: f32le) -> bool { return classify(x) == .NaN }
  970. is_nan_f32be :: proc "contextless" (x: f32be) -> bool { return classify(x) == .NaN }
  971. is_nan_f64 :: proc "contextless" (x: f64) -> bool { return classify(x) == .NaN }
  972. is_nan_f64le :: proc "contextless" (x: f64le) -> bool { return classify(x) == .NaN }
  973. is_nan_f64be :: proc "contextless" (x: f64be) -> bool { return classify(x) == .NaN }
  974. is_nan :: proc{
  975. is_nan_f16, is_nan_f16le, is_nan_f16be,
  976. is_nan_f32, is_nan_f32le, is_nan_f32be,
  977. is_nan_f64, is_nan_f64le, is_nan_f64be,
  978. }
  979. // is_inf reports whether f is an infinity, according to sign.
  980. // If sign > 0, is_inf reports whether f is positive infinity.
  981. // If sign < 0, is_inf reports whether f is negative infinity.
  982. // If sign == 0, is_inf reports whether f is either infinity.
  983. is_inf_f16 :: proc "contextless" (x: f16, sign: int = 0) -> bool {
  984. class := classify(abs(x))
  985. switch {
  986. case sign > 0:
  987. return class == .Inf
  988. case sign < 0:
  989. return class == .Neg_Inf
  990. }
  991. return class == .Inf || class == .Neg_Inf
  992. }
  993. is_inf_f16le :: proc "contextless" (x: f16le, sign: int = 0) -> bool {
  994. return #force_inline is_inf_f16(f16(x), sign)
  995. }
  996. is_inf_f16be :: proc "contextless" (x: f16be, sign: int = 0) -> bool {
  997. return #force_inline is_inf_f16(f16(x), sign)
  998. }
  999. is_inf_f32 :: proc "contextless" (x: f32, sign: int = 0) -> bool {
  1000. class := classify(abs(x))
  1001. switch {
  1002. case sign > 0:
  1003. return class == .Inf
  1004. case sign < 0:
  1005. return class == .Neg_Inf
  1006. }
  1007. return class == .Inf || class == .Neg_Inf
  1008. }
  1009. is_inf_f32le :: proc "contextless" (x: f32le, sign: int = 0) -> bool {
  1010. return #force_inline is_inf_f32(f32(x), sign)
  1011. }
  1012. is_inf_f32be :: proc "contextless" (x: f32be, sign: int = 0) -> bool {
  1013. return #force_inline is_inf_f32(f32(x), sign)
  1014. }
  1015. is_inf_f64 :: proc "contextless" (x: f64, sign: int = 0) -> bool {
  1016. class := classify(abs(x))
  1017. switch {
  1018. case sign > 0:
  1019. return class == .Inf
  1020. case sign < 0:
  1021. return class == .Neg_Inf
  1022. }
  1023. return class == .Inf || class == .Neg_Inf
  1024. }
  1025. is_inf_f64le :: proc "contextless" (x: f64le, sign: int = 0) -> bool {
  1026. return #force_inline is_inf_f64(f64(x), sign)
  1027. }
  1028. is_inf_f64be :: proc "contextless" (x: f64be, sign: int = 0) -> bool {
  1029. return #force_inline is_inf_f64(f64(x), sign)
  1030. }
  1031. is_inf :: proc{
  1032. is_inf_f16, is_inf_f16le, is_inf_f16be,
  1033. is_inf_f32, is_inf_f32le, is_inf_f32be,
  1034. is_inf_f64, is_inf_f64le, is_inf_f64be,
  1035. }
  1036. inf_f16 :: proc "contextless" (sign: int) -> f16 {
  1037. return f16(inf_f64(sign))
  1038. }
  1039. inf_f16le :: proc "contextless" (sign: int) -> f16le {
  1040. return f16le(inf_f64(sign))
  1041. }
  1042. inf_f16be :: proc "contextless" (sign: int) -> f16be {
  1043. return f16be(inf_f64(sign))
  1044. }
  1045. inf_f32 :: proc "contextless" (sign: int) -> f32 {
  1046. return f32(inf_f64(sign))
  1047. }
  1048. inf_f32le :: proc "contextless" (sign: int) -> f32le {
  1049. return f32le(inf_f64(sign))
  1050. }
  1051. inf_f32be :: proc "contextless" (sign: int) -> f32be {
  1052. return f32be(inf_f64(sign))
  1053. }
  1054. inf_f64 :: proc "contextless" (sign: int) -> f64 {
  1055. if sign >= 0 {
  1056. return 0h7ff00000_00000000
  1057. } else {
  1058. return 0hfff00000_00000000
  1059. }
  1060. }
  1061. inf_f64le :: proc "contextless" (sign: int) -> f64le {
  1062. return f64le(inf_f64(sign))
  1063. }
  1064. inf_f64be :: proc "contextless" (sign: int) -> f64be {
  1065. return f64be(inf_f64(sign))
  1066. }
  1067. nan_f16 :: proc "contextless" () -> f16 {
  1068. return f16(nan_f64())
  1069. }
  1070. nan_f16le :: proc "contextless" () -> f16le {
  1071. return f16le(nan_f64())
  1072. }
  1073. nan_f16be :: proc "contextless" () -> f16be {
  1074. return f16be(nan_f64())
  1075. }
  1076. nan_f32 :: proc "contextless" () -> f32 {
  1077. return f32(nan_f64())
  1078. }
  1079. nan_f32le :: proc "contextless" () -> f32le {
  1080. return f32le(nan_f64())
  1081. }
  1082. nan_f32be :: proc "contextless" () -> f32be {
  1083. return f32be(nan_f64())
  1084. }
  1085. nan_f64 :: proc "contextless" () -> f64 {
  1086. return 0h7ff80000_00000001
  1087. }
  1088. nan_f64le :: proc "contextless" () -> f64le {
  1089. return f64le(nan_f64())
  1090. }
  1091. nan_f64be :: proc "contextless" () -> f64be {
  1092. return f64be(nan_f64())
  1093. }
  1094. is_power_of_two :: proc "contextless" (x: int) -> bool {
  1095. return x > 0 && (x & (x-1)) == 0
  1096. }
  1097. next_power_of_two :: proc "contextless" (x: int) -> int {
  1098. k := x -1
  1099. when size_of(int) == 8 {
  1100. k = k | (k >> 32)
  1101. }
  1102. k = k | (k >> 16)
  1103. k = k | (k >> 8)
  1104. k = k | (k >> 4)
  1105. k = k | (k >> 2)
  1106. k = k | (k >> 1)
  1107. k += 1 + int(x <= 0)
  1108. return k
  1109. }
  1110. sum :: proc "contextless" (x: $T/[]$E) -> (res: E)
  1111. where intrinsics.type_is_numeric(E) {
  1112. for i in x {
  1113. res += i
  1114. }
  1115. return
  1116. }
  1117. prod :: proc "contextless" (x: $T/[]$E) -> (res: E)
  1118. where intrinsics.type_is_numeric(E) {
  1119. res = 1
  1120. for i in x {
  1121. res *= i
  1122. }
  1123. return
  1124. }
  1125. cumsum_inplace :: proc "contextless" (x: $T/[]$E)
  1126. where intrinsics.type_is_numeric(E) {
  1127. for i in 1..<len(x) {
  1128. x[i] = x[i-1] + x[i]
  1129. }
  1130. }
  1131. cumsum :: proc "contextless" (dst, src: $T/[]$E) -> T
  1132. where intrinsics.type_is_numeric(E) {
  1133. N := min(len(dst), len(src))
  1134. if N > 0 {
  1135. dst[0] = src[0]
  1136. for i in 1..<N {
  1137. dst[i] = dst[i-1] + src[i]
  1138. }
  1139. }
  1140. return dst[:N]
  1141. }
  1142. atan2_f16 :: proc "contextless" (y, x: f16) -> f16 {
  1143. // TODO(bill): Better atan2_f16
  1144. return f16(atan2_f64(f64(y), f64(x)))
  1145. }
  1146. atan2_f16le :: proc "contextless" (y, x: f16le) -> f16le {
  1147. // TODO(bill): Better atan2_f16
  1148. return f16le(atan2_f64(f64(y), f64(x)))
  1149. }
  1150. atan2_f16be :: proc "contextless" (y, x: f16be) -> f16be {
  1151. // TODO(bill): Better atan2_f16
  1152. return f16be(atan2_f64(f64(y), f64(x)))
  1153. }
  1154. atan2_f32 :: proc "contextless" (y, x: f32) -> f32 {
  1155. // TODO(bill): Better atan2_f32
  1156. return f32(atan2_f64(f64(y), f64(x)))
  1157. }
  1158. atan2_f32le :: proc "contextless" (y, x: f32le) -> f32le {
  1159. // TODO(bill): Better atan2_f32
  1160. return f32le(atan2_f64(f64(y), f64(x)))
  1161. }
  1162. atan2_f32be :: proc "contextless" (y, x: f32be) -> f32be {
  1163. // TODO(bill): Better atan2_f32
  1164. return f32be(atan2_f64(f64(y), f64(x)))
  1165. }
  1166. atan2_f64 :: proc "contextless" (y, x: f64) -> f64 {
  1167. // TODO(bill): Faster atan2_f64 if possible
  1168. // The original C code:
  1169. // Stephen L. Moshier
  1170. // [email protected]
  1171. NAN :: 0h7fff_ffff_ffff_ffff
  1172. INF :: 0h7FF0_0000_0000_0000
  1173. PI :: 0h4009_21fb_5444_2d18
  1174. atan :: proc "contextless" (x: f64) -> f64 {
  1175. if x == 0 {
  1176. return x
  1177. }
  1178. if x > 0 {
  1179. return s_atan(x)
  1180. }
  1181. return -s_atan(-x)
  1182. }
  1183. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  1184. s_atan :: proc "contextless" (x: f64) -> f64 {
  1185. MORE_BITS :: 6.123233995736765886130e-17 // pi/2 = PIO2 + MORE_BITS
  1186. TAN3PI08 :: 2.41421356237309504880 // tan(3*pi/8)
  1187. if x <= 0.66 {
  1188. return x_atan(x)
  1189. }
  1190. if x > TAN3PI08 {
  1191. return PI/2 - x_atan(1/x) + MORE_BITS
  1192. }
  1193. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS
  1194. }
  1195. // x_atan evaluates a series valid in the range [0, 0.66].
  1196. x_atan :: proc "contextless" (x: f64) -> f64 {
  1197. P0 :: -8.750608600031904122785e-01
  1198. P1 :: -1.615753718733365076637e+01
  1199. P2 :: -7.500855792314704667340e+01
  1200. P3 :: -1.228866684490136173410e+02
  1201. P4 :: -6.485021904942025371773e+01
  1202. Q0 :: +2.485846490142306297962e+01
  1203. Q1 :: +1.650270098316988542046e+02
  1204. Q2 :: +4.328810604912902668951e+02
  1205. Q3 :: +4.853903996359136964868e+02
  1206. Q4 :: +1.945506571482613964425e+02
  1207. z := x * x
  1208. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4)
  1209. z = x*z + x
  1210. return z
  1211. }
  1212. switch {
  1213. case is_nan(y) || is_nan(x):
  1214. return NAN
  1215. case y == 0:
  1216. if x >= 0 && !sign_bit(x) {
  1217. return copy_sign(0.0, y)
  1218. }
  1219. return copy_sign(PI, y)
  1220. case x == 0:
  1221. return copy_sign(PI*0.5, y)
  1222. case is_inf(x, 0):
  1223. if is_inf(x, 1) {
  1224. if is_inf(y, 0) {
  1225. return copy_sign(PI*0.25, y)
  1226. }
  1227. return copy_sign(0, y)
  1228. }
  1229. if is_inf(y, 0) {
  1230. return copy_sign(PI*0.75, y)
  1231. }
  1232. return copy_sign(PI, y)
  1233. case is_inf(y, 0):
  1234. return copy_sign(PI*0.5, y)
  1235. }
  1236. q := atan(y / x)
  1237. if x < 0 {
  1238. if q <= 0 {
  1239. return q + PI
  1240. }
  1241. return q - PI
  1242. }
  1243. return q
  1244. }
  1245. atan2_f64le :: proc "contextless" (y, x: f64le) -> f64le {
  1246. // TODO(bill): Better atan2_f32
  1247. return f64le(atan2_f64(f64(y), f64(x)))
  1248. }
  1249. atan2_f64be :: proc "contextless" (y, x: f64be) -> f64be {
  1250. // TODO(bill): Better atan2_f32
  1251. return f64be(atan2_f64(f64(y), f64(x)))
  1252. }
  1253. atan2 :: proc{
  1254. atan2_f16, atan2_f16le, atan2_f16be,
  1255. atan2_f32, atan2_f32le, atan2_f32be,
  1256. atan2_f64, atan2_f64le, atan2_f64be,
  1257. }
  1258. atan :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1259. return atan2(x, 1)
  1260. }
  1261. asin :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1262. return atan2(x, sqrt(1 - x*x))
  1263. }
  1264. acos :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1265. return 2 * atan2(sqrt(1 - x), sqrt(1 + x))
  1266. }
  1267. sinh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1268. return (exp(x) - exp(-x))*0.5
  1269. }
  1270. cosh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1271. return (exp(x) + exp(-x))*0.5
  1272. }
  1273. tanh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1274. t := exp(2*x)
  1275. return (t - 1) / (t + 1)
  1276. }
  1277. asinh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1278. // The original C code, the long comment, and the constants
  1279. // below are from FreeBSD's /usr/src/lib/msun/src/s_asinh.c
  1280. // and came with this notice.
  1281. //
  1282. // ====================================================
  1283. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1284. //
  1285. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1286. // Permission to use, copy, modify, and distribute this
  1287. // software is freely granted, provided that this notice
  1288. // is preserved.
  1289. // ====================================================
  1290. LN2 :: 0h3FE62E42FEFA39EF
  1291. NEAR_ZERO :: 1.0 / (1 << 28)
  1292. LARGE :: 1 << 28
  1293. x := f64(y)
  1294. if is_nan(x) || is_inf(x) {
  1295. return T(x)
  1296. }
  1297. sign := false
  1298. if x < 0 {
  1299. x = -x
  1300. sign = true
  1301. }
  1302. temp: f64
  1303. switch {
  1304. case x > LARGE:
  1305. temp = ln(x) + LN2
  1306. case x > 2:
  1307. temp = ln(2*x + 1/(sqrt(x*x + 1) + x))
  1308. case x < NEAR_ZERO:
  1309. temp = x
  1310. case:
  1311. temp = log1p(x + x*x/(1 + sqrt(1 + x*x)))
  1312. }
  1313. if sign {
  1314. temp = -temp
  1315. }
  1316. return T(temp)
  1317. }
  1318. acosh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1319. // The original C code, the long comment, and the constants
  1320. // below are from FreeBSD's /usr/src/lib/msun/src/e_acosh.c
  1321. // and came with this notice.
  1322. //
  1323. // ====================================================
  1324. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1325. //
  1326. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1327. // Permission to use, copy, modify, and distribute this
  1328. // software is freely granted, provided that this notice
  1329. // is preserved.
  1330. // ====================================================
  1331. LARGE :: 1<<28
  1332. LN2 :: 0h3FE62E42FEFA39EF
  1333. x := f64(y)
  1334. switch {
  1335. case x < 1 || is_nan(x):
  1336. return T(nan_f64())
  1337. case x == 1:
  1338. return 0
  1339. case x >= LARGE:
  1340. return T(ln(x) + LN2)
  1341. case x > 2:
  1342. return T(ln(2*x - 1/(x+sqrt(x*x-1))))
  1343. }
  1344. t := x-1
  1345. return T(log1p(t + sqrt(2*t + t*t)))
  1346. }
  1347. atanh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1348. // The original C code, the long comment, and the constants
  1349. // below are from FreeBSD's /usr/src/lib/msun/src/e_atanh.c
  1350. // and came with this notice.
  1351. //
  1352. // ====================================================
  1353. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1354. //
  1355. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1356. // Permission to use, copy, modify, and distribute this
  1357. // software is freely granted, provided that this notice
  1358. // is preserved.
  1359. // ====================================================
  1360. NEAR_ZERO :: 1.0 / (1 << 28)
  1361. x := f64(y)
  1362. switch {
  1363. case x < -1 || x > 1 || is_nan(x):
  1364. return T(nan_f64())
  1365. case x == 1:
  1366. return T(inf_f64(1))
  1367. case x == -1:
  1368. return T(inf_f64(-1))
  1369. }
  1370. sign := false
  1371. if x < 0 {
  1372. x = -x
  1373. sign = true
  1374. }
  1375. temp: f64
  1376. switch {
  1377. case x < NEAR_ZERO:
  1378. temp = x
  1379. case x < 0.5:
  1380. temp = x + x
  1381. temp = 0.5 * log1p(temp + temp*x/(1-x))
  1382. case:
  1383. temp = 0.5 * log1p((x+x)/(1-x))
  1384. }
  1385. if sign {
  1386. temp = -temp
  1387. }
  1388. return T(temp)
  1389. }
  1390. ilogb_f16 :: proc "contextless" (val: f16) -> int {
  1391. switch {
  1392. case val == 0: return int(min(i32))
  1393. case is_nan(val): return int(max(i32))
  1394. case is_inf(val): return int(max(i32))
  1395. }
  1396. x, exp := normalize_f16(val)
  1397. return int(((transmute(u16)x)>>F16_SHIFT)&F16_MASK) - F16_BIAS + exp
  1398. }
  1399. ilogb_f32 :: proc "contextless" (val: f32) -> int {
  1400. switch {
  1401. case val == 0: return int(min(i32))
  1402. case is_nan(val): return int(max(i32))
  1403. case is_inf(val): return int(max(i32))
  1404. }
  1405. x, exp := normalize_f32(val)
  1406. return int(((transmute(u32)x)>>F32_SHIFT)&F32_MASK) - F32_BIAS + exp
  1407. }
  1408. ilogb_f64 :: proc "contextless" (val: f64) -> int {
  1409. switch {
  1410. case val == 0: return int(min(i32))
  1411. case is_nan(val): return int(max(i32))
  1412. case is_inf(val): return int(max(i32))
  1413. }
  1414. x, exp := normalize_f64(val)
  1415. return int(((transmute(u64)x)>>F64_SHIFT)&F64_MASK) - F64_BIAS + exp
  1416. }
  1417. ilogb_f16le :: proc "contextless" (value: f16le) -> int { return ilogb_f16(f16(value)) }
  1418. ilogb_f16be :: proc "contextless" (value: f16be) -> int { return ilogb_f16(f16(value)) }
  1419. ilogb_f32le :: proc "contextless" (value: f32le) -> int { return ilogb_f32(f32(value)) }
  1420. ilogb_f32be :: proc "contextless" (value: f32be) -> int { return ilogb_f32(f32(value)) }
  1421. ilogb_f64le :: proc "contextless" (value: f64le) -> int { return ilogb_f64(f64(value)) }
  1422. ilogb_f64be :: proc "contextless" (value: f64be) -> int { return ilogb_f64(f64(value)) }
  1423. ilogb :: proc {
  1424. ilogb_f16,
  1425. ilogb_f32,
  1426. ilogb_f64,
  1427. ilogb_f16le,
  1428. ilogb_f16be,
  1429. ilogb_f32le,
  1430. ilogb_f32be,
  1431. ilogb_f64le,
  1432. ilogb_f64be,
  1433. }
  1434. logb_f16 :: proc "contextless" (val: f16) -> f16 {
  1435. switch {
  1436. case val == 0: return inf_f16(-1)
  1437. case is_inf(val): return inf_f16(+1)
  1438. case is_nan(val): return val
  1439. }
  1440. return f16(ilogb(val))
  1441. }
  1442. logb_f32 :: proc "contextless" (val: f32) -> f32 {
  1443. switch {
  1444. case val == 0: return inf_f32(-1)
  1445. case is_inf(val): return inf_f32(+1)
  1446. case is_nan(val): return val
  1447. }
  1448. return f32(ilogb(val))
  1449. }
  1450. logb_f64 :: proc "contextless" (val: f64) -> f64 {
  1451. switch {
  1452. case val == 0: return inf_f64(-1)
  1453. case is_inf(val): return inf_f64(+1)
  1454. case is_nan(val): return val
  1455. }
  1456. return f64(ilogb(val))
  1457. }
  1458. logb_f16le :: proc "contextless" (value: f16le) -> f16le { return f16le(logb_f16(f16(value))) }
  1459. logb_f16be :: proc "contextless" (value: f16be) -> f16be { return f16be(logb_f16(f16(value))) }
  1460. logb_f32le :: proc "contextless" (value: f32le) -> f32le { return f32le(logb_f32(f32(value))) }
  1461. logb_f32be :: proc "contextless" (value: f32be) -> f32be { return f32be(logb_f32(f32(value))) }
  1462. logb_f64le :: proc "contextless" (value: f64le) -> f64le { return f64le(logb_f64(f64(value))) }
  1463. logb_f64be :: proc "contextless" (value: f64be) -> f64be { return f64be(logb_f64(f64(value))) }
  1464. logb :: proc {
  1465. logb_f16,
  1466. logb_f32,
  1467. logb_f64,
  1468. logb_f16le,
  1469. logb_f16be,
  1470. logb_f32le,
  1471. logb_f32be,
  1472. logb_f64le,
  1473. logb_f64be,
  1474. }
  1475. nextafter_f16 :: proc "contextless" (x, y: f16) -> (r: f16) {
  1476. switch {
  1477. case is_nan(x) || is_nan(y):
  1478. r = nan_f16()
  1479. case x == y:
  1480. r = x
  1481. case x == 0:
  1482. r = copy_sign_f16(1, y)
  1483. case (y > x) == (x > 0):
  1484. r = transmute(f16)(transmute(u16)x + 1)
  1485. case:
  1486. r = transmute(f16)(transmute(u16)x - 1)
  1487. }
  1488. return
  1489. }
  1490. nextafter_f32 :: proc "contextless" (x, y: f32) -> (r: f32) {
  1491. switch {
  1492. case is_nan(x) || is_nan(y):
  1493. r = nan_f32()
  1494. case x == y:
  1495. r = x
  1496. case x == 0:
  1497. r = copy_sign_f32(1, y)
  1498. case (y > x) == (x > 0):
  1499. r = transmute(f32)(transmute(u32)x + 1)
  1500. case:
  1501. r = transmute(f32)(transmute(u32)x - 1)
  1502. }
  1503. return
  1504. }
  1505. nextafter_f64 :: proc "contextless" (x, y: f64) -> (r: f64) {
  1506. switch {
  1507. case is_nan(x) || is_nan(y):
  1508. r = nan_f64()
  1509. case x == y:
  1510. r = x
  1511. case x == 0:
  1512. r = copy_sign_f64(1, y)
  1513. case (y > x) == (x > 0):
  1514. r = transmute(f64)(transmute(u64)x + 1)
  1515. case:
  1516. r = transmute(f64)(transmute(u64)x - 1)
  1517. }
  1518. return
  1519. }
  1520. nextafter_f16le :: proc "contextless" (x, y: f16le) -> (r: f16le) { return f16le(nextafter_f16(f16(x), f16(y))) }
  1521. nextafter_f16be :: proc "contextless" (x, y: f16be) -> (r: f16be) { return f16be(nextafter_f16(f16(x), f16(y))) }
  1522. nextafter_f32le :: proc "contextless" (x, y: f32le) -> (r: f32le) { return f32le(nextafter_f32(f32(x), f32(y))) }
  1523. nextafter_f32be :: proc "contextless" (x, y: f32be) -> (r: f32be) { return f32be(nextafter_f32(f32(x), f32(y))) }
  1524. nextafter_f64le :: proc "contextless" (x, y: f64le) -> (r: f64le) { return f64le(nextafter_f64(f64(x), f64(y))) }
  1525. nextafter_f64be :: proc "contextless" (x, y: f64be) -> (r: f64be) { return f64be(nextafter_f64(f64(x), f64(y))) }
  1526. nextafter :: proc{
  1527. nextafter_f16, nextafter_f16le, nextafter_f16be,
  1528. nextafter_f32, nextafter_f32le, nextafter_f32be,
  1529. nextafter_f64, nextafter_f64le, nextafter_f64be,
  1530. }
  1531. signbit_f16 :: proc "contextless" (x: f16) -> bool {
  1532. return (transmute(u16)x)&(1<<15) != 0
  1533. }
  1534. signbit_f32 :: proc "contextless" (x: f32) -> bool {
  1535. return (transmute(u32)x)&(1<<31) != 0
  1536. }
  1537. signbit_f64 :: proc "contextless" (x: f64) -> bool {
  1538. return (transmute(u64)x)&(1<<63) != 0
  1539. }
  1540. signbit_f16le :: proc "contextless" (x: f16le) -> bool { return signbit_f16(f16(x)) }
  1541. signbit_f32le :: proc "contextless" (x: f32le) -> bool { return signbit_f32(f32(x)) }
  1542. signbit_f64le :: proc "contextless" (x: f64le) -> bool { return signbit_f64(f64(x)) }
  1543. signbit_f16be :: proc "contextless" (x: f16be) -> bool { return signbit_f16(f16(x)) }
  1544. signbit_f32be :: proc "contextless" (x: f32be) -> bool { return signbit_f32(f32(x)) }
  1545. signbit_f64be :: proc "contextless" (x: f64be) -> bool { return signbit_f64(f64(x)) }
  1546. signbit :: proc{
  1547. signbit_f16, signbit_f16le, signbit_f16be,
  1548. signbit_f32, signbit_f32le, signbit_f32be,
  1549. signbit_f64, signbit_f64le, signbit_f64be,
  1550. }
  1551. F16_DIG :: 3
  1552. F16_EPSILON :: 0.00097656
  1553. F16_GUARD :: 0
  1554. F16_MANT_DIG :: 11
  1555. F16_MAX :: 65504.0
  1556. F16_MAX_10_EXP :: 4
  1557. F16_MAX_EXP :: 15
  1558. F16_MIN :: 6.10351562e-5
  1559. F16_MIN_10_EXP :: -4
  1560. F16_MIN_EXP :: -14
  1561. F16_NORMALIZE :: 0
  1562. F16_RADIX :: 2
  1563. F16_ROUNDS :: 1
  1564. F32_DIG :: 6
  1565. F32_EPSILON :: 1.192092896e-07
  1566. F32_GUARD :: 0
  1567. F32_MANT_DIG :: 24
  1568. F32_MAX :: 3.402823466e+38
  1569. F32_MAX_10_EXP :: 38
  1570. F32_MAX_EXP :: 128
  1571. F32_MIN :: 1.175494351e-38
  1572. F32_MIN_10_EXP :: -37
  1573. F32_MIN_EXP :: -125
  1574. F32_NORMALIZE :: 0
  1575. F32_RADIX :: 2
  1576. F32_ROUNDS :: 1
  1577. F64_DIG :: 15 // # of decimal digits of precision
  1578. F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
  1579. F64_MANT_DIG :: 53 // # of bits in mantissa
  1580. F64_MAX :: 1.7976931348623158e+308 // max value
  1581. F64_MAX_10_EXP :: 308 // max decimal exponent
  1582. F64_MAX_EXP :: 1024 // max binary exponent
  1583. F64_MIN :: 2.2250738585072014e-308 // min positive value
  1584. F64_MIN_10_EXP :: -307 // min decimal exponent
  1585. F64_MIN_EXP :: -1021 // min binary exponent
  1586. F64_RADIX :: 2 // exponent radix
  1587. F64_ROUNDS :: 1 // addition rounding: near
  1588. F16_MASK :: 0x1f
  1589. F16_SHIFT :: 16 - 6
  1590. F16_BIAS :: 0xf
  1591. F32_MASK :: 0xff
  1592. F32_SHIFT :: 32 - 9
  1593. F32_BIAS :: 0x7f
  1594. F64_MASK :: 0x7ff
  1595. F64_SHIFT :: 64 - 12
  1596. F64_BIAS :: 0x3ff
  1597. INF_F16 :f16: 0h7C00
  1598. NEG_INF_F16 :f16: 0hFC00
  1599. SNAN_F16 :f16: 0h7C01
  1600. QNAN_F16 :f16: 0h7E01
  1601. INF_F32 :f32: 0h7F80_0000
  1602. NEG_INF_F32 :f32: 0hFF80_0000
  1603. SNAN_F32 :f32: 0hFF80_0001
  1604. QNAN_F32 :f32: 0hFFC0_0001
  1605. INF_F64 :f64: 0h7FF0_0000_0000_0000
  1606. NEG_INF_F64 :f64: 0hFFF0_0000_0000_0000
  1607. SNAN_F64 :f64: 0h7FF0_0000_0000_0001
  1608. QNAN_F64 :f64: 0h7FF8_0000_0000_0001