math.odin 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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. res = 1
  1092. for i in x {
  1093. res *= i
  1094. }
  1095. return
  1096. }
  1097. cumsum_inplace :: proc "contextless" (x: $T/[]$E) -> T
  1098. where intrinsics.type_is_numeric(E) {
  1099. for i in 1..<len(x) {
  1100. x[i] = x[i-1] + x[i]
  1101. }
  1102. }
  1103. cumsum :: proc "contextless" (dst, src: $T/[]$E) -> T
  1104. where intrinsics.type_is_numeric(E) {
  1105. N := min(len(dst), len(src))
  1106. if N > 0 {
  1107. dst[0] = src[0]
  1108. for i in 1..<N {
  1109. dst[i] = dst[i-1] + src[i]
  1110. }
  1111. }
  1112. return dst[:N]
  1113. }
  1114. atan2_f16 :: proc "contextless" (y, x: f16) -> f16 {
  1115. // TODO(bill): Better atan2_f16
  1116. return f16(atan2_f64(f64(y), f64(x)))
  1117. }
  1118. atan2_f16le :: proc "contextless" (y, x: f16le) -> f16le {
  1119. // TODO(bill): Better atan2_f16
  1120. return f16le(atan2_f64(f64(y), f64(x)))
  1121. }
  1122. atan2_f16be :: proc "contextless" (y, x: f16be) -> f16be {
  1123. // TODO(bill): Better atan2_f16
  1124. return f16be(atan2_f64(f64(y), f64(x)))
  1125. }
  1126. atan2_f32 :: proc "contextless" (y, x: f32) -> f32 {
  1127. // TODO(bill): Better atan2_f32
  1128. return f32(atan2_f64(f64(y), f64(x)))
  1129. }
  1130. atan2_f32le :: proc "contextless" (y, x: f32le) -> f32le {
  1131. // TODO(bill): Better atan2_f32
  1132. return f32le(atan2_f64(f64(y), f64(x)))
  1133. }
  1134. atan2_f32be :: proc "contextless" (y, x: f32be) -> f32be {
  1135. // TODO(bill): Better atan2_f32
  1136. return f32be(atan2_f64(f64(y), f64(x)))
  1137. }
  1138. atan2_f64 :: proc "contextless" (y, x: f64) -> f64 {
  1139. // TODO(bill): Faster atan2_f64 if possible
  1140. // The original C code:
  1141. // Stephen L. Moshier
  1142. // [email protected]
  1143. NAN :: 0h7fff_ffff_ffff_ffff
  1144. INF :: 0h7FF0_0000_0000_0000
  1145. PI :: 0h4009_21fb_5444_2d18
  1146. atan :: proc "contextless" (x: f64) -> f64 {
  1147. if x == 0 {
  1148. return x
  1149. }
  1150. if x > 0 {
  1151. return s_atan(x)
  1152. }
  1153. return -s_atan(-x)
  1154. }
  1155. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  1156. s_atan :: proc "contextless" (x: f64) -> f64 {
  1157. MORE_BITS :: 6.123233995736765886130e-17 // pi/2 = PIO2 + MORE_BITS
  1158. TAN3PI08 :: 2.41421356237309504880 // tan(3*pi/8)
  1159. if x <= 0.66 {
  1160. return x_atan(x)
  1161. }
  1162. if x > TAN3PI08 {
  1163. return PI/2 - x_atan(1/x) + MORE_BITS
  1164. }
  1165. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS
  1166. }
  1167. // x_atan evaluates a series valid in the range [0, 0.66].
  1168. x_atan :: proc "contextless" (x: f64) -> f64 {
  1169. P0 :: -8.750608600031904122785e-01
  1170. P1 :: -1.615753718733365076637e+01
  1171. P2 :: -7.500855792314704667340e+01
  1172. P3 :: -1.228866684490136173410e+02
  1173. P4 :: -6.485021904942025371773e+01
  1174. Q0 :: +2.485846490142306297962e+01
  1175. Q1 :: +1.650270098316988542046e+02
  1176. Q2 :: +4.328810604912902668951e+02
  1177. Q3 :: +4.853903996359136964868e+02
  1178. Q4 :: +1.945506571482613964425e+02
  1179. z := x * x
  1180. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4)
  1181. z = x*z + x
  1182. return z
  1183. }
  1184. switch {
  1185. case is_nan(y) || is_nan(x):
  1186. return NAN
  1187. case y == 0:
  1188. if x >= 0 && !sign_bit(x) {
  1189. return copy_sign(0.0, y)
  1190. }
  1191. return copy_sign(PI, y)
  1192. case x == 0:
  1193. return copy_sign(PI*0.5, y)
  1194. case is_inf(x, 0):
  1195. if is_inf(x, 1) {
  1196. if is_inf(y, 0) {
  1197. return copy_sign(PI*0.25, y)
  1198. }
  1199. return copy_sign(0, y)
  1200. }
  1201. if is_inf(y, 0) {
  1202. return copy_sign(PI*0.75, y)
  1203. }
  1204. return copy_sign(PI, y)
  1205. case is_inf(y, 0):
  1206. return copy_sign(PI*0.5, y)
  1207. }
  1208. q := atan(y / x)
  1209. if x < 0 {
  1210. if q <= 0 {
  1211. return q + PI
  1212. }
  1213. return q - PI
  1214. }
  1215. return q
  1216. }
  1217. atan2_f64le :: proc "contextless" (y, x: f64le) -> f64le {
  1218. // TODO(bill): Better atan2_f32
  1219. return f64le(atan2_f64(f64(y), f64(x)))
  1220. }
  1221. atan2_f64be :: proc "contextless" (y, x: f64be) -> f64be {
  1222. // TODO(bill): Better atan2_f32
  1223. return f64be(atan2_f64(f64(y), f64(x)))
  1224. }
  1225. atan2 :: proc{
  1226. atan2_f16, atan2_f16le, atan2_f16be,
  1227. atan2_f32, atan2_f32le, atan2_f32be,
  1228. atan2_f64, atan2_f64le, atan2_f64be,
  1229. }
  1230. atan :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1231. return atan2(x, 1)
  1232. }
  1233. asin :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1234. return atan2(x, 1 + sqrt(1 - x*x))
  1235. }
  1236. acos :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1237. return 2 * atan2(sqrt(1 - x), sqrt(1 + x))
  1238. }
  1239. sinh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1240. return (exp(x) - exp(-x))*0.5
  1241. }
  1242. cosh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1243. return (exp(x) + exp(-x))*0.5
  1244. }
  1245. tanh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1246. t := exp(2*x)
  1247. return (t - 1) / (t + 1)
  1248. }
  1249. asinh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1250. // The original C code, the long comment, and the constants
  1251. // below are from FreeBSD's /usr/src/lib/msun/src/s_asinh.c
  1252. // and came with this notice.
  1253. //
  1254. // ====================================================
  1255. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1256. //
  1257. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1258. // Permission to use, copy, modify, and distribute this
  1259. // software is freely granted, provided that this notice
  1260. // is preserved.
  1261. // ====================================================
  1262. LN2 :: 0h3FE62E42FEFA39EF
  1263. NEAR_ZERO :: 1.0 / (1 << 28)
  1264. LARGE :: 1 << 28
  1265. x := f64(y)
  1266. if is_nan(x) || is_inf(x) {
  1267. return T(x)
  1268. }
  1269. sign := false
  1270. if x < 0 {
  1271. x = -x
  1272. sign = true
  1273. }
  1274. temp: f64
  1275. switch {
  1276. case x > LARGE:
  1277. temp = ln(x) + LN2
  1278. case x > 2:
  1279. temp = ln(2*x + 1/(sqrt(x*x + 1) + x))
  1280. case x < NEAR_ZERO:
  1281. temp = x
  1282. case:
  1283. temp = log1p(x + x*x/(1 + sqrt(1 + x*x)))
  1284. }
  1285. if sign {
  1286. temp = -temp
  1287. }
  1288. return T(temp)
  1289. }
  1290. acosh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1291. // The original C code, the long comment, and the constants
  1292. // below are from FreeBSD's /usr/src/lib/msun/src/e_acosh.c
  1293. // and came with this notice.
  1294. //
  1295. // ====================================================
  1296. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1297. //
  1298. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1299. // Permission to use, copy, modify, and distribute this
  1300. // software is freely granted, provided that this notice
  1301. // is preserved.
  1302. // ====================================================
  1303. LARGE :: 1<<28
  1304. LN2 :: 0h3FE62E42FEFA39EF
  1305. x := f64(y)
  1306. switch {
  1307. case x < 1 || is_nan(x):
  1308. return T(nan_f64())
  1309. case x == 1:
  1310. return 0
  1311. case x >= LARGE:
  1312. return T(ln(x) + LN2)
  1313. case x > 2:
  1314. return T(ln(2*x - 1/(x+sqrt(x*x-1))))
  1315. }
  1316. t := x-1
  1317. return T(log1p(t + sqrt(2*t + t*t)))
  1318. }
  1319. atanh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1320. // The original C code, the long comment, and the constants
  1321. // below are from FreeBSD's /usr/src/lib/msun/src/e_atanh.c
  1322. // and came with this notice.
  1323. //
  1324. // ====================================================
  1325. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1326. //
  1327. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1328. // Permission to use, copy, modify, and distribute this
  1329. // software is freely granted, provided that this notice
  1330. // is preserved.
  1331. // ====================================================
  1332. NEAR_ZERO :: 1.0 / (1 << 28)
  1333. x := f64(y)
  1334. switch {
  1335. case x < -1 || x > 1 || is_nan(x):
  1336. return T(nan_f64())
  1337. case x == 1:
  1338. return T(inf_f64(1))
  1339. case x == -1:
  1340. return T(inf_f64(-1))
  1341. }
  1342. sign := false
  1343. if x < 0 {
  1344. x = -x
  1345. sign = true
  1346. }
  1347. temp: f64
  1348. switch {
  1349. case x < NEAR_ZERO:
  1350. temp = x
  1351. case x < 0.5:
  1352. temp = x + x
  1353. temp = 0.5 * log1p(temp + temp*x/(1-x))
  1354. case:
  1355. temp = 0.5 * log1p((x+x)/(1-x))
  1356. }
  1357. if sign {
  1358. temp = -temp
  1359. }
  1360. return T(temp)
  1361. }
  1362. ilogb_f16 :: proc "contextless" (val: f16) -> int {
  1363. switch {
  1364. case val == 0: return int(min(i32))
  1365. case is_nan(val): return int(max(i32))
  1366. case is_inf(val): return int(max(i32))
  1367. }
  1368. x, exp := normalize_f16(val)
  1369. return int(((transmute(u16)x)>>F16_SHIFT)&F16_MASK) - F16_BIAS + exp
  1370. }
  1371. ilogb_f32 :: proc "contextless" (val: f32) -> int {
  1372. switch {
  1373. case val == 0: return int(min(i32))
  1374. case is_nan(val): return int(max(i32))
  1375. case is_inf(val): return int(max(i32))
  1376. }
  1377. x, exp := normalize_f32(val)
  1378. return int(((transmute(u32)x)>>F32_SHIFT)&F32_MASK) - F32_BIAS + exp
  1379. }
  1380. ilogb_f64 :: proc "contextless" (val: f64) -> int {
  1381. switch {
  1382. case val == 0: return int(min(i32))
  1383. case is_nan(val): return int(max(i32))
  1384. case is_inf(val): return int(max(i32))
  1385. }
  1386. x, exp := normalize_f64(val)
  1387. return int(((transmute(u64)x)>>F64_SHIFT)&F64_MASK) - F64_BIAS + exp
  1388. }
  1389. ilogb_f16le :: proc "contextless" (value: f16le) -> int { return ilogb_f16(f16(value)) }
  1390. ilogb_f16be :: proc "contextless" (value: f16be) -> int { return ilogb_f16(f16(value)) }
  1391. ilogb_f32le :: proc "contextless" (value: f32le) -> int { return ilogb_f32(f32(value)) }
  1392. ilogb_f32be :: proc "contextless" (value: f32be) -> int { return ilogb_f32(f32(value)) }
  1393. ilogb_f64le :: proc "contextless" (value: f64le) -> int { return ilogb_f64(f64(value)) }
  1394. ilogb_f64be :: proc "contextless" (value: f64be) -> int { return ilogb_f64(f64(value)) }
  1395. ilogb :: proc {
  1396. ilogb_f16,
  1397. ilogb_f32,
  1398. ilogb_f64,
  1399. ilogb_f16le,
  1400. ilogb_f16be,
  1401. ilogb_f32le,
  1402. ilogb_f32be,
  1403. ilogb_f64le,
  1404. ilogb_f64be,
  1405. }
  1406. logb_f16 :: proc "contextless" (val: f16) -> f16 {
  1407. switch {
  1408. case val == 0: return inf_f16(-1)
  1409. case is_inf(val): return inf_f16(+1)
  1410. case is_nan(val): return val
  1411. }
  1412. return f16(ilogb(val))
  1413. }
  1414. logb_f32 :: proc "contextless" (val: f32) -> f32 {
  1415. switch {
  1416. case val == 0: return inf_f32(-1)
  1417. case is_inf(val): return inf_f32(+1)
  1418. case is_nan(val): return val
  1419. }
  1420. return f32(ilogb(val))
  1421. }
  1422. logb_f64 :: proc "contextless" (val: f64) -> f64 {
  1423. switch {
  1424. case val == 0: return inf_f64(-1)
  1425. case is_inf(val): return inf_f64(+1)
  1426. case is_nan(val): return val
  1427. }
  1428. return f64(ilogb(val))
  1429. }
  1430. logb_f16le :: proc "contextless" (value: f16le) -> f16le { return f16le(logb_f16(f16(value))) }
  1431. logb_f16be :: proc "contextless" (value: f16be) -> f16be { return f16be(logb_f16(f16(value))) }
  1432. logb_f32le :: proc "contextless" (value: f32le) -> f32le { return f32le(logb_f32(f32(value))) }
  1433. logb_f32be :: proc "contextless" (value: f32be) -> f32be { return f32be(logb_f32(f32(value))) }
  1434. logb_f64le :: proc "contextless" (value: f64le) -> f64le { return f64le(logb_f64(f64(value))) }
  1435. logb_f64be :: proc "contextless" (value: f64be) -> f64be { return f64be(logb_f64(f64(value))) }
  1436. logb :: proc {
  1437. logb_f16,
  1438. logb_f32,
  1439. logb_f64,
  1440. logb_f16le,
  1441. logb_f16be,
  1442. logb_f32le,
  1443. logb_f32be,
  1444. logb_f64le,
  1445. logb_f64be,
  1446. }
  1447. nextafter_f16 :: proc "contextless" (x, y: f16) -> (r: f16) {
  1448. switch {
  1449. case is_nan(x) || is_nan(y):
  1450. r = nan_f16()
  1451. case x == y:
  1452. r = x
  1453. case x == 0:
  1454. r = copy_sign_f16(1, y)
  1455. case (y > x) == (x > 0):
  1456. r = transmute(f16)(transmute(u16)x + 1)
  1457. case:
  1458. r = transmute(f16)(transmute(u16)x - 1)
  1459. }
  1460. return
  1461. }
  1462. nextafter_f32 :: proc "contextless" (x, y: f32) -> (r: f32) {
  1463. switch {
  1464. case is_nan(x) || is_nan(y):
  1465. r = nan_f32()
  1466. case x == y:
  1467. r = x
  1468. case x == 0:
  1469. r = copy_sign_f32(1, y)
  1470. case (y > x) == (x > 0):
  1471. r = transmute(f32)(transmute(u32)x + 1)
  1472. case:
  1473. r = transmute(f32)(transmute(u32)x - 1)
  1474. }
  1475. return
  1476. }
  1477. nextafter_f64 :: proc "contextless" (x, y: f64) -> (r: f64) {
  1478. switch {
  1479. case is_nan(x) || is_nan(y):
  1480. r = nan_f64()
  1481. case x == y:
  1482. r = x
  1483. case x == 0:
  1484. r = copy_sign_f64(1, y)
  1485. case (y > x) == (x > 0):
  1486. r = transmute(f64)(transmute(u64)x + 1)
  1487. case:
  1488. r = transmute(f64)(transmute(u64)x - 1)
  1489. }
  1490. return
  1491. }
  1492. nextafter_f16le :: proc "contextless" (x, y: f16le) -> (r: f16le) { return f16le(nextafter_f16(f16(x), f16(y))) }
  1493. nextafter_f16be :: proc "contextless" (x, y: f16be) -> (r: f16be) { return f16be(nextafter_f16(f16(x), f16(y))) }
  1494. nextafter_f32le :: proc "contextless" (x, y: f32le) -> (r: f32le) { return f32le(nextafter_f32(f32(x), f32(y))) }
  1495. nextafter_f32be :: proc "contextless" (x, y: f32be) -> (r: f32be) { return f32be(nextafter_f32(f32(x), f32(y))) }
  1496. nextafter_f64le :: proc "contextless" (x, y: f64le) -> (r: f64le) { return f64le(nextafter_f64(f64(x), f64(y))) }
  1497. nextafter_f64be :: proc "contextless" (x, y: f64be) -> (r: f64be) { return f64be(nextafter_f64(f64(x), f64(y))) }
  1498. nextafter :: proc{
  1499. nextafter_f16, nextafter_f16le, nextafter_f16be,
  1500. nextafter_f32, nextafter_f32le, nextafter_f32be,
  1501. nextafter_f64, nextafter_f64le, nextafter_f64be,
  1502. }
  1503. signbit_f16 :: proc "contextless" (x: f16) -> bool {
  1504. return (transmute(u16)x)&(1<<15) != 0
  1505. }
  1506. signbit_f32 :: proc "contextless" (x: f32) -> bool {
  1507. return (transmute(u32)x)&(1<<31) != 0
  1508. }
  1509. signbit_f64 :: proc "contextless" (x: f64) -> bool {
  1510. return (transmute(u64)x)&(1<<63) != 0
  1511. }
  1512. signbit_f16le :: proc "contextless" (x: f16le) -> bool { return signbit_f16(f16(x)) }
  1513. signbit_f32le :: proc "contextless" (x: f32le) -> bool { return signbit_f32(f32(x)) }
  1514. signbit_f64le :: proc "contextless" (x: f64le) -> bool { return signbit_f64(f64(x)) }
  1515. signbit_f16be :: proc "contextless" (x: f16be) -> bool { return signbit_f16(f16(x)) }
  1516. signbit_f32be :: proc "contextless" (x: f32be) -> bool { return signbit_f32(f32(x)) }
  1517. signbit_f64be :: proc "contextless" (x: f64be) -> bool { return signbit_f64(f64(x)) }
  1518. signbit :: proc{
  1519. signbit_f16, signbit_f16le, signbit_f16be,
  1520. signbit_f32, signbit_f32le, signbit_f32be,
  1521. signbit_f64, signbit_f64le, signbit_f64be,
  1522. }
  1523. F16_DIG :: 3
  1524. F16_EPSILON :: 0.00097656
  1525. F16_GUARD :: 0
  1526. F16_MANT_DIG :: 11
  1527. F16_MAX :: 65504.0
  1528. F16_MAX_10_EXP :: 4
  1529. F16_MAX_EXP :: 15
  1530. F16_MIN :: 6.10351562e-5
  1531. F16_MIN_10_EXP :: -4
  1532. F16_MIN_EXP :: -14
  1533. F16_NORMALIZE :: 0
  1534. F16_RADIX :: 2
  1535. F16_ROUNDS :: 1
  1536. F32_DIG :: 6
  1537. F32_EPSILON :: 1.192092896e-07
  1538. F32_GUARD :: 0
  1539. F32_MANT_DIG :: 24
  1540. F32_MAX :: 3.402823466e+38
  1541. F32_MAX_10_EXP :: 38
  1542. F32_MAX_EXP :: 128
  1543. F32_MIN :: 1.175494351e-38
  1544. F32_MIN_10_EXP :: -37
  1545. F32_MIN_EXP :: -125
  1546. F32_NORMALIZE :: 0
  1547. F32_RADIX :: 2
  1548. F32_ROUNDS :: 1
  1549. F64_DIG :: 15 // # of decimal digits of precision
  1550. F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
  1551. F64_MANT_DIG :: 53 // # of bits in mantissa
  1552. F64_MAX :: 1.7976931348623158e+308 // max value
  1553. F64_MAX_10_EXP :: 308 // max decimal exponent
  1554. F64_MAX_EXP :: 1024 // max binary exponent
  1555. F64_MIN :: 2.2250738585072014e-308 // min positive value
  1556. F64_MIN_10_EXP :: -307 // min decimal exponent
  1557. F64_MIN_EXP :: -1021 // min binary exponent
  1558. F64_RADIX :: 2 // exponent radix
  1559. F64_ROUNDS :: 1 // addition rounding: near
  1560. F16_MASK :: 0x1f
  1561. F16_SHIFT :: 16 - 6
  1562. F16_BIAS :: 0xf
  1563. F32_MASK :: 0xff
  1564. F32_SHIFT :: 32 - 9
  1565. F32_BIAS :: 0x7f
  1566. F64_MASK :: 0x7ff
  1567. F64_SHIFT :: 64 - 12
  1568. F64_BIAS :: 0x3ff