math.odin 53 KB

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