math.odin 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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. // Removes the fractional part of the value, i.e. rounds towards zero.
  420. trunc :: proc{
  421. trunc_f16, trunc_f16le, trunc_f16be,
  422. trunc_f32, trunc_f32le, trunc_f32be,
  423. trunc_f64, trunc_f64le, trunc_f64be,
  424. }
  425. round_f16 :: proc "contextless" (x: f16) -> f16 {
  426. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  427. }
  428. round_f16le :: proc "contextless" (x: f16le) -> f16le {
  429. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  430. }
  431. round_f16be :: proc "contextless" (x: f16be) -> f16be {
  432. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  433. }
  434. round_f32 :: proc "contextless" (x: f32) -> f32 {
  435. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  436. }
  437. round_f32le :: proc "contextless" (x: f32le) -> f32le {
  438. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  439. }
  440. round_f32be :: proc "contextless" (x: f32be) -> f32be {
  441. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  442. }
  443. round_f64 :: proc "contextless" (x: f64) -> f64 {
  444. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  445. }
  446. round_f64le :: proc "contextless" (x: f64le) -> f64le {
  447. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  448. }
  449. round_f64be :: proc "contextless" (x: f64be) -> f64be {
  450. return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
  451. }
  452. round :: proc{
  453. round_f16, round_f16le, round_f16be,
  454. round_f32, round_f32le, round_f32be,
  455. round_f64, round_f64le, round_f64be,
  456. }
  457. ceil_f16 :: proc "contextless" (x: f16) -> f16 { return -floor(-x) }
  458. ceil_f16le :: proc "contextless" (x: f16le) -> f16le { return -floor(-x) }
  459. ceil_f16be :: proc "contextless" (x: f16be) -> f16be { return -floor(-x) }
  460. ceil_f32 :: proc "contextless" (x: f32) -> f32 { return -floor(-x) }
  461. ceil_f32le :: proc "contextless" (x: f32le) -> f32le { return -floor(-x) }
  462. ceil_f32be :: proc "contextless" (x: f32be) -> f32be { return -floor(-x) }
  463. ceil_f64 :: proc "contextless" (x: f64) -> f64 { return -floor(-x) }
  464. ceil_f64le :: proc "contextless" (x: f64le) -> f64le { return -floor(-x) }
  465. ceil_f64be :: proc "contextless" (x: f64be) -> f64be { return -floor(-x) }
  466. ceil :: proc{
  467. ceil_f16, ceil_f16le, ceil_f16be,
  468. ceil_f32, ceil_f32le, ceil_f32be,
  469. ceil_f64, ceil_f64le, ceil_f64be,
  470. }
  471. floor_f16 :: proc "contextless" (x: f16) -> f16 {
  472. if x == 0 || is_nan(x) || is_inf(x) {
  473. return x
  474. }
  475. if x < 0 {
  476. d, fract := modf(-x)
  477. if fract != 0.0 {
  478. d = d + 1
  479. }
  480. return -d
  481. }
  482. d, _ := modf(x)
  483. return d
  484. }
  485. floor_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(floor_f16(f16(x))) }
  486. floor_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(floor_f16(f16(x))) }
  487. floor_f32 :: proc "contextless" (x: f32) -> f32 {
  488. if x == 0 || is_nan(x) || is_inf(x) {
  489. return x
  490. }
  491. if x < 0 {
  492. d, fract := modf(-x)
  493. if fract != 0.0 {
  494. d = d + 1
  495. }
  496. return -d
  497. }
  498. d, _ := modf(x)
  499. return d
  500. }
  501. floor_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(floor_f32(f32(x))) }
  502. floor_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(floor_f32(f32(x))) }
  503. floor_f64 :: proc "contextless" (x: f64) -> f64 {
  504. if x == 0 || is_nan(x) || is_inf(x) {
  505. return x
  506. }
  507. if x < 0 {
  508. d, fract := modf(-x)
  509. if fract != 0.0 {
  510. d = d + 1
  511. }
  512. return -d
  513. }
  514. d, _ := modf(x)
  515. return d
  516. }
  517. floor_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(floor_f64(f64(x))) }
  518. floor_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(floor_f64(f64(x))) }
  519. floor :: proc{
  520. floor_f16, floor_f16le, floor_f16be,
  521. floor_f32, floor_f32le, floor_f32be,
  522. floor_f64, floor_f64le, floor_f64be,
  523. }
  524. floor_div :: proc "contextless" (x, y: $T) -> T
  525. where intrinsics.type_is_integer(T) {
  526. a := x / y
  527. r := x % y
  528. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  529. a -= 1
  530. }
  531. return a
  532. }
  533. floor_mod :: proc "contextless" (x, y: $T) -> T
  534. where intrinsics.type_is_integer(T) {
  535. r := x % y
  536. if (r > 0 && y < 0) || (r < 0 && y > 0) {
  537. r += y
  538. }
  539. return r
  540. }
  541. modf_f16 :: proc "contextless" (x: f16) -> (int: f16, frac: f16) {
  542. shift :: F16_SHIFT
  543. mask :: F16_MASK
  544. bias :: F16_BIAS
  545. if x < 1 {
  546. switch {
  547. case x < 0:
  548. int, frac = modf(-x)
  549. return -int, -frac
  550. case x == 0:
  551. return x, x
  552. }
  553. return 0, x
  554. }
  555. i := transmute(u16)x
  556. e := uint(i>>shift)&mask - bias
  557. if e < shift {
  558. i &~= 1<<(shift-e) - 1
  559. }
  560. int = transmute(f16)i
  561. frac = x - int
  562. return
  563. }
  564. modf_f16le :: proc "contextless" (x: f16le) -> (int: f16le, frac: f16le) {
  565. i, f := #force_inline modf_f16(f16(x))
  566. return f16le(i), f16le(f)
  567. }
  568. modf_f16be :: proc "contextless" (x: f16be) -> (int: f16be, frac: f16be) {
  569. i, f := #force_inline modf_f16(f16(x))
  570. return f16be(i), f16be(f)
  571. }
  572. modf_f32 :: proc "contextless" (x: f32) -> (int: f32, frac: f32) {
  573. shift :: F32_SHIFT
  574. mask :: F32_MASK
  575. bias :: F32_BIAS
  576. if x < 1 {
  577. switch {
  578. case x < 0:
  579. int, frac = modf(-x)
  580. return -int, -frac
  581. case x == 0:
  582. return x, x
  583. }
  584. return 0, x
  585. }
  586. i := transmute(u32)x
  587. e := uint(i>>shift)&mask - bias
  588. if e < shift {
  589. i &~= 1<<(shift-e) - 1
  590. }
  591. int = transmute(f32)i
  592. frac = x - int
  593. return
  594. }
  595. modf_f32le :: proc "contextless" (x: f32le) -> (int: f32le, frac: f32le) {
  596. i, f := #force_inline modf_f32(f32(x))
  597. return f32le(i), f32le(f)
  598. }
  599. modf_f32be :: proc "contextless" (x: f32be) -> (int: f32be, frac: f32be) {
  600. i, f := #force_inline modf_f32(f32(x))
  601. return f32be(i), f32be(f)
  602. }
  603. modf_f64 :: proc "contextless" (x: f64) -> (int: f64, frac: f64) {
  604. shift :: F64_SHIFT
  605. mask :: F64_MASK
  606. bias :: F64_BIAS
  607. if x < 1 {
  608. switch {
  609. case x < 0:
  610. int, frac = modf(-x)
  611. return -int, -frac
  612. case x == 0:
  613. return x, x
  614. }
  615. return 0, x
  616. }
  617. i := transmute(u64)x
  618. e := uint(i>>shift)&mask - bias
  619. if e < shift {
  620. i &~= 1<<(shift-e) - 1
  621. }
  622. int = transmute(f64)i
  623. frac = x - int
  624. return
  625. }
  626. modf_f64le :: proc "contextless" (x: f64le) -> (int: f64le, frac: f64le) {
  627. i, f := #force_inline modf_f64(f64(x))
  628. return f64le(i), f64le(f)
  629. }
  630. modf_f64be :: proc "contextless" (x: f64be) -> (int: f64be, frac: f64be) {
  631. i, f := #force_inline modf_f64(f64(x))
  632. return f64be(i), f64be(f)
  633. }
  634. modf :: proc{
  635. modf_f16, modf_f16le, modf_f16be,
  636. modf_f32, modf_f32le, modf_f32be,
  637. modf_f64, modf_f64le, modf_f64be,
  638. }
  639. split_decimal :: modf
  640. mod_f16 :: proc "contextless" (x, y: f16) -> (n: f16) {
  641. z := abs(y)
  642. n = remainder(abs(x), z)
  643. if sign(n) < 0 {
  644. n += z
  645. }
  646. return copy_sign(n, x)
  647. }
  648. mod_f16le :: proc "contextless" (x, y: f16le) -> (n: f16le) { return #force_inline f16le(mod_f16(f16(x), f16(y))) }
  649. mod_f16be :: proc "contextless" (x, y: f16be) -> (n: f16be) { return #force_inline f16be(mod_f16(f16(x), f16(y))) }
  650. mod_f32 :: proc "contextless" (x, y: f32) -> (n: f32) {
  651. z := abs(y)
  652. n = remainder(abs(x), z)
  653. if sign(n) < 0 {
  654. n += z
  655. }
  656. return copy_sign(n, x)
  657. }
  658. mod_f32le :: proc "contextless" (x, y: f32le) -> (n: f32le) { return #force_inline f32le(mod_f32(f32(x), f32(y))) }
  659. mod_f32be :: proc "contextless" (x, y: f32be) -> (n: f32be) { return #force_inline f32be(mod_f32(f32(x), f32(y))) }
  660. mod_f64 :: proc "contextless" (x, y: f64) -> (n: f64) {
  661. z := abs(y)
  662. n = remainder(abs(x), z)
  663. if sign(n) < 0 {
  664. n += z
  665. }
  666. return copy_sign(n, x)
  667. }
  668. mod_f64le :: proc "contextless" (x, y: f64le) -> (n: f64le) { return #force_inline f64le(mod_f64(f64(x), f64(y))) }
  669. mod_f64be :: proc "contextless" (x, y: f64be) -> (n: f64be) { return #force_inline f64be(mod_f64(f64(x), f64(y))) }
  670. mod :: proc{
  671. mod_f16, mod_f16le, mod_f16be,
  672. mod_f32, mod_f32le, mod_f32be,
  673. mod_f64, mod_f64le, mod_f64be,
  674. }
  675. remainder_f16 :: proc "contextless" (x, y: f16 ) -> f16 { return x - round(x/y) * y }
  676. remainder_f16le :: proc "contextless" (x, y: f16le) -> f16le { return x - round(x/y) * y }
  677. remainder_f16be :: proc "contextless" (x, y: f16be) -> f16be { return x - round(x/y) * y }
  678. remainder_f32 :: proc "contextless" (x, y: f32 ) -> f32 { return x - round(x/y) * y }
  679. remainder_f32le :: proc "contextless" (x, y: f32le) -> f32le { return x - round(x/y) * y }
  680. remainder_f32be :: proc "contextless" (x, y: f32be) -> f32be { return x - round(x/y) * y }
  681. remainder_f64 :: proc "contextless" (x, y: f64 ) -> f64 { return x - round(x/y) * y }
  682. remainder_f64le :: proc "contextless" (x, y: f64le) -> f64le { return x - round(x/y) * y }
  683. remainder_f64be :: proc "contextless" (x, y: f64be) -> f64be { return x - round(x/y) * y }
  684. remainder :: proc{
  685. remainder_f16, remainder_f16le, remainder_f16be,
  686. remainder_f32, remainder_f32le, remainder_f32be,
  687. remainder_f64, remainder_f64le, remainder_f64be,
  688. }
  689. gcd :: proc "contextless" (x, y: $T) -> T
  690. where intrinsics.type_is_ordered_numeric(T) {
  691. x, y := x, y
  692. for y != 0 {
  693. x %= y
  694. x, y = y, x
  695. }
  696. return abs(x)
  697. }
  698. lcm :: proc "contextless" (x, y: $T) -> T
  699. where intrinsics.type_is_ordered_numeric(T) {
  700. return x / gcd(x, y) * y
  701. }
  702. normalize_f16 :: proc "contextless" (x: f16) -> (y: f16, exponent: int) {
  703. if abs(x) < F16_MIN {
  704. return x * (1<<F16_SHIFT), -F16_SHIFT
  705. }
  706. return x, 0
  707. }
  708. normalize_f32 :: proc "contextless" (x: f32) -> (y: f32, exponent: int) {
  709. if abs(x) < F32_MIN {
  710. return x * (1<<F32_SHIFT), -F32_SHIFT
  711. }
  712. return x, 0
  713. }
  714. normalize_f64 :: proc "contextless" (x: f64) -> (y: f64, exponent: int) {
  715. if abs(x) < F64_MIN {
  716. return x * (1<<F64_SHIFT), -F64_SHIFT
  717. }
  718. return x, 0
  719. }
  720. normalize_f16le :: proc "contextless" (x: f16le) -> (y: f16le, exponent: int) { y0, e := normalize_f16(f16(x)); return f16le(y0), e }
  721. normalize_f16be :: proc "contextless" (x: f16be) -> (y: f16be, exponent: int) { y0, e := normalize_f16(f16(x)); return f16be(y0), e }
  722. normalize_f32le :: proc "contextless" (x: f32le) -> (y: f32le, exponent: int) { y0, e := normalize_f32(f32(x)); return f32le(y0), e }
  723. normalize_f32be :: proc "contextless" (x: f32be) -> (y: f32be, exponent: int) { y0, e := normalize_f32(f32(x)); return f32be(y0), e }
  724. normalize_f64le :: proc "contextless" (x: f64le) -> (y: f64le, exponent: int) { y0, e := normalize_f64(f64(x)); return f64le(y0), e }
  725. normalize_f64be :: proc "contextless" (x: f64be) -> (y: f64be, exponent: int) { y0, e := normalize_f64(f64(x)); return f64be(y0), e }
  726. normalize :: proc{
  727. normalize_f16,
  728. normalize_f32,
  729. normalize_f64,
  730. normalize_f16le,
  731. normalize_f16be,
  732. normalize_f32le,
  733. normalize_f32be,
  734. normalize_f64le,
  735. normalize_f64be,
  736. }
  737. frexp_f16 :: proc "contextless" (x: f16) -> (significand: f16, exponent: int) {
  738. f, e := frexp_f64(f64(x))
  739. return f16(f), e
  740. }
  741. frexp_f16le :: proc "contextless" (x: f16le) -> (significand: f16le, exponent: int) {
  742. f, e := frexp_f64(f64(x))
  743. return f16le(f), e
  744. }
  745. frexp_f16be :: proc "contextless" (x: f16be) -> (significand: f16be, exponent: int) {
  746. f, e := frexp_f64(f64(x))
  747. return f16be(f), e
  748. }
  749. frexp_f32 :: proc "contextless" (x: f32) -> (significand: f32, exponent: int) {
  750. f, e := frexp_f64(f64(x))
  751. return f32(f), e
  752. }
  753. frexp_f32le :: proc "contextless" (x: f32le) -> (significand: f32le, exponent: int) {
  754. f, e := frexp_f64(f64(x))
  755. return f32le(f), e
  756. }
  757. frexp_f32be :: proc "contextless" (x: f32be) -> (significand: f32be, exponent: int) {
  758. f, e := frexp_f64(f64(x))
  759. return f32be(f), e
  760. }
  761. frexp_f64 :: proc "contextless" (f: f64) -> (significand: f64, exponent: int) {
  762. mask :: F64_MASK
  763. shift :: F64_SHIFT
  764. bias :: F64_BIAS
  765. switch {
  766. case f == 0:
  767. return 0, 0
  768. case is_inf(f) || is_nan(f):
  769. return f, 0
  770. }
  771. f := f
  772. f, exponent = normalize_f64(f)
  773. x := transmute(u64)f
  774. exponent += int((x>>shift)&mask) - bias + 1
  775. x &~= mask << shift
  776. x |= (-1 + bias) << shift
  777. significand = transmute(f64)x
  778. return
  779. }
  780. frexp_f64le :: proc "contextless" (x: f64le) -> (significand: f64le, exponent: int) {
  781. f, e := frexp_f64(f64(x))
  782. return f64le(f), e
  783. }
  784. frexp_f64be :: proc "contextless" (x: f64be) -> (significand: f64be, exponent: int) {
  785. f, e := frexp_f64(f64(x))
  786. return f64be(f), e
  787. }
  788. // frexp breaks the value into a normalized fraction, and an integral power of two
  789. // It returns a significand and exponent satisfying x == significand * 2**exponent
  790. // with the absolute value of significand in the intervalue of [0.5, 1).
  791. //
  792. // Special cases:
  793. // frexp(+0) = +0, 0
  794. // frexp(-0) = -0, 0
  795. // frexp(+inf) = +inf, 0
  796. // frexp(-inf) = -inf, 0
  797. // frexp(NaN) = NaN, 0
  798. frexp :: proc{
  799. frexp_f16, frexp_f16le, frexp_f16be,
  800. frexp_f32, frexp_f32le, frexp_f32be,
  801. frexp_f64, frexp_f64le, frexp_f64be,
  802. }
  803. binomial :: proc "contextless" (n, k: int) -> int {
  804. switch {
  805. case k <= 0: return 1
  806. case 2*k > n: return binomial(n, n-k)
  807. }
  808. b := n
  809. for i in 2..<k {
  810. b = (b * (n+1-i))/i
  811. }
  812. return b
  813. }
  814. factorial :: proc "contextless" (n: int) -> int {
  815. when size_of(int) == size_of(i64) {
  816. @static table := [21]int{
  817. 1,
  818. 1,
  819. 2,
  820. 6,
  821. 24,
  822. 120,
  823. 720,
  824. 5_040,
  825. 40_320,
  826. 362_880,
  827. 3_628_800,
  828. 39_916_800,
  829. 479_001_600,
  830. 6_227_020_800,
  831. 87_178_291_200,
  832. 1_307_674_368_000,
  833. 20_922_789_888_000,
  834. 355_687_428_096_000,
  835. 6_402_373_705_728_000,
  836. 121_645_100_408_832_000,
  837. 2_432_902_008_176_640_000,
  838. }
  839. } else {
  840. @static table := [13]int{
  841. 1,
  842. 1,
  843. 2,
  844. 6,
  845. 24,
  846. 120,
  847. 720,
  848. 5_040,
  849. 40_320,
  850. 362_880,
  851. 3_628_800,
  852. 39_916_800,
  853. 479_001_600,
  854. }
  855. }
  856. return table[n]
  857. }
  858. classify_f16 :: proc "contextless" (x: f16) -> Float_Class {
  859. switch {
  860. case x == 0:
  861. i := transmute(i16)x
  862. if i < 0 {
  863. return .Neg_Zero
  864. }
  865. return .Zero
  866. case x*0.25 == x:
  867. if x < 0 {
  868. return .Neg_Inf
  869. }
  870. return .Inf
  871. case !(x == x):
  872. return .NaN
  873. }
  874. u := transmute(u16)x
  875. exp := int(u>>10) & (1<<5 - 1)
  876. if exp == 0 {
  877. return .Subnormal
  878. }
  879. return .Normal
  880. }
  881. classify_f16le :: proc "contextless" (x: f16le) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  882. classify_f16be :: proc "contextless" (x: f16be) -> Float_Class { return #force_inline classify_f16(f16(x)) }
  883. classify_f32 :: proc "contextless" (x: f32) -> Float_Class {
  884. switch {
  885. case x == 0:
  886. i := transmute(i32)x
  887. if i < 0 {
  888. return .Neg_Zero
  889. }
  890. return .Zero
  891. case x*0.5 == x:
  892. if x < 0 {
  893. return .Neg_Inf
  894. }
  895. return .Inf
  896. case !(x == x):
  897. return .NaN
  898. }
  899. u := transmute(u32)x
  900. exp := int(u>>23) & (1<<8 - 1)
  901. if exp == 0 {
  902. return .Subnormal
  903. }
  904. return .Normal
  905. }
  906. classify_f32le :: proc "contextless" (x: f32le) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  907. classify_f32be :: proc "contextless" (x: f32be) -> Float_Class { return #force_inline classify_f32(f32(x)) }
  908. classify_f64 :: proc "contextless" (x: f64) -> Float_Class {
  909. switch {
  910. case x == 0:
  911. i := transmute(i64)x
  912. if i < 0 {
  913. return .Neg_Zero
  914. }
  915. return .Zero
  916. case x*0.5 == x:
  917. if x < 0 {
  918. return .Neg_Inf
  919. }
  920. return .Inf
  921. case !(x == x):
  922. return .NaN
  923. }
  924. u := transmute(u64)x
  925. exp := int(u>>52) & (1<<11 - 1)
  926. if exp == 0 {
  927. return .Subnormal
  928. }
  929. return .Normal
  930. }
  931. classify_f64le :: proc "contextless" (x: f64le) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  932. classify_f64be :: proc "contextless" (x: f64be) -> Float_Class { return #force_inline classify_f64(f64(x)) }
  933. // Returns the `Float_Class` of the value, i.e. whether normal, subnormal, zero, negative zero, NaN, infinity or
  934. // negative infinity.
  935. classify :: proc{
  936. classify_f16, classify_f16le, classify_f16be,
  937. classify_f32, classify_f32le, classify_f32be,
  938. classify_f64, classify_f64le, classify_f64be,
  939. }
  940. is_nan_f16 :: proc "contextless" (x: f16) -> bool { return classify(x) == .NaN }
  941. is_nan_f16le :: proc "contextless" (x: f16le) -> bool { return classify(x) == .NaN }
  942. is_nan_f16be :: proc "contextless" (x: f16be) -> bool { return classify(x) == .NaN }
  943. is_nan_f32 :: proc "contextless" (x: f32) -> bool { return classify(x) == .NaN }
  944. is_nan_f32le :: proc "contextless" (x: f32le) -> bool { return classify(x) == .NaN }
  945. is_nan_f32be :: proc "contextless" (x: f32be) -> bool { return classify(x) == .NaN }
  946. is_nan_f64 :: proc "contextless" (x: f64) -> bool { return classify(x) == .NaN }
  947. is_nan_f64le :: proc "contextless" (x: f64le) -> bool { return classify(x) == .NaN }
  948. is_nan_f64be :: proc "contextless" (x: f64be) -> bool { return classify(x) == .NaN }
  949. is_nan :: proc{
  950. is_nan_f16, is_nan_f16le, is_nan_f16be,
  951. is_nan_f32, is_nan_f32le, is_nan_f32be,
  952. is_nan_f64, is_nan_f64le, is_nan_f64be,
  953. }
  954. // is_inf reports whether f is an infinity, according to sign.
  955. // If sign > 0, is_inf reports whether f is positive infinity.
  956. // If sign < 0, is_inf reports whether f is negative infinity.
  957. // If sign == 0, is_inf reports whether f is either infinity.
  958. is_inf_f16 :: proc "contextless" (x: f16, sign: int = 0) -> bool {
  959. class := classify(abs(x))
  960. switch {
  961. case sign > 0:
  962. return class == .Inf
  963. case sign < 0:
  964. return class == .Neg_Inf
  965. }
  966. return class == .Inf || class == .Neg_Inf
  967. }
  968. is_inf_f16le :: proc "contextless" (x: f16le, sign: int = 0) -> bool {
  969. return #force_inline is_inf_f16(f16(x), sign)
  970. }
  971. is_inf_f16be :: proc "contextless" (x: f16be, sign: int = 0) -> bool {
  972. return #force_inline is_inf_f16(f16(x), sign)
  973. }
  974. is_inf_f32 :: proc "contextless" (x: f32, sign: int = 0) -> bool {
  975. class := classify(abs(x))
  976. switch {
  977. case sign > 0:
  978. return class == .Inf
  979. case sign < 0:
  980. return class == .Neg_Inf
  981. }
  982. return class == .Inf || class == .Neg_Inf
  983. }
  984. is_inf_f32le :: proc "contextless" (x: f32le, sign: int = 0) -> bool {
  985. return #force_inline is_inf_f32(f32(x), sign)
  986. }
  987. is_inf_f32be :: proc "contextless" (x: f32be, sign: int = 0) -> bool {
  988. return #force_inline is_inf_f32(f32(x), sign)
  989. }
  990. is_inf_f64 :: proc "contextless" (x: f64, sign: int = 0) -> bool {
  991. class := classify(abs(x))
  992. switch {
  993. case sign > 0:
  994. return class == .Inf
  995. case sign < 0:
  996. return class == .Neg_Inf
  997. }
  998. return class == .Inf || class == .Neg_Inf
  999. }
  1000. is_inf_f64le :: proc "contextless" (x: f64le, sign: int = 0) -> bool {
  1001. return #force_inline is_inf_f64(f64(x), sign)
  1002. }
  1003. is_inf_f64be :: proc "contextless" (x: f64be, sign: int = 0) -> bool {
  1004. return #force_inline is_inf_f64(f64(x), sign)
  1005. }
  1006. is_inf :: proc{
  1007. is_inf_f16, is_inf_f16le, is_inf_f16be,
  1008. is_inf_f32, is_inf_f32le, is_inf_f32be,
  1009. is_inf_f64, is_inf_f64le, is_inf_f64be,
  1010. }
  1011. inf_f16 :: proc "contextless" (sign: int) -> f16 {
  1012. return f16(inf_f64(sign))
  1013. }
  1014. inf_f16le :: proc "contextless" (sign: int) -> f16le {
  1015. return f16le(inf_f64(sign))
  1016. }
  1017. inf_f16be :: proc "contextless" (sign: int) -> f16be {
  1018. return f16be(inf_f64(sign))
  1019. }
  1020. inf_f32 :: proc "contextless" (sign: int) -> f32 {
  1021. return f32(inf_f64(sign))
  1022. }
  1023. inf_f32le :: proc "contextless" (sign: int) -> f32le {
  1024. return f32le(inf_f64(sign))
  1025. }
  1026. inf_f32be :: proc "contextless" (sign: int) -> f32be {
  1027. return f32be(inf_f64(sign))
  1028. }
  1029. inf_f64 :: proc "contextless" (sign: int) -> f64 {
  1030. if sign >= 0 {
  1031. return 0h7ff00000_00000000
  1032. } else {
  1033. return 0hfff00000_00000000
  1034. }
  1035. }
  1036. inf_f64le :: proc "contextless" (sign: int) -> f64le {
  1037. return f64le(inf_f64(sign))
  1038. }
  1039. inf_f64be :: proc "contextless" (sign: int) -> f64be {
  1040. return f64be(inf_f64(sign))
  1041. }
  1042. nan_f16 :: proc "contextless" () -> f16 {
  1043. return f16(nan_f64())
  1044. }
  1045. nan_f16le :: proc "contextless" () -> f16le {
  1046. return f16le(nan_f64())
  1047. }
  1048. nan_f16be :: proc "contextless" () -> f16be {
  1049. return f16be(nan_f64())
  1050. }
  1051. nan_f32 :: proc "contextless" () -> f32 {
  1052. return f32(nan_f64())
  1053. }
  1054. nan_f32le :: proc "contextless" () -> f32le {
  1055. return f32le(nan_f64())
  1056. }
  1057. nan_f32be :: proc "contextless" () -> f32be {
  1058. return f32be(nan_f64())
  1059. }
  1060. nan_f64 :: proc "contextless" () -> f64 {
  1061. return 0h7ff80000_00000001
  1062. }
  1063. nan_f64le :: proc "contextless" () -> f64le {
  1064. return f64le(nan_f64())
  1065. }
  1066. nan_f64be :: proc "contextless" () -> f64be {
  1067. return f64be(nan_f64())
  1068. }
  1069. is_power_of_two :: proc "contextless" (x: int) -> bool {
  1070. return x > 0 && (x & (x-1)) == 0
  1071. }
  1072. next_power_of_two :: proc "contextless" (x: int) -> int {
  1073. k := x -1
  1074. when size_of(int) == 8 {
  1075. k = k | (k >> 32)
  1076. }
  1077. k = k | (k >> 16)
  1078. k = k | (k >> 8)
  1079. k = k | (k >> 4)
  1080. k = k | (k >> 2)
  1081. k = k | (k >> 1)
  1082. k += 1 + int(x <= 0)
  1083. return k
  1084. }
  1085. sum :: proc "contextless" (x: $T/[]$E) -> (res: E)
  1086. where intrinsics.type_is_numeric(E) {
  1087. for i in x {
  1088. res += i
  1089. }
  1090. return
  1091. }
  1092. prod :: proc "contextless" (x: $T/[]$E) -> (res: E)
  1093. where intrinsics.type_is_numeric(E) {
  1094. res = 1
  1095. for i in x {
  1096. res *= i
  1097. }
  1098. return
  1099. }
  1100. cumsum_inplace :: proc "contextless" (x: $T/[]$E)
  1101. where intrinsics.type_is_numeric(E) {
  1102. for i in 1..<len(x) {
  1103. x[i] = x[i-1] + x[i]
  1104. }
  1105. }
  1106. cumsum :: proc "contextless" (dst, src: $T/[]$E) -> T
  1107. where intrinsics.type_is_numeric(E) {
  1108. N := min(len(dst), len(src))
  1109. if N > 0 {
  1110. dst[0] = src[0]
  1111. for i in 1..<N {
  1112. dst[i] = dst[i-1] + src[i]
  1113. }
  1114. }
  1115. return dst[:N]
  1116. }
  1117. atan2_f16 :: proc "contextless" (y, x: f16) -> f16 {
  1118. // TODO(bill): Better atan2_f16
  1119. return f16(atan2_f64(f64(y), f64(x)))
  1120. }
  1121. atan2_f16le :: proc "contextless" (y, x: f16le) -> f16le {
  1122. // TODO(bill): Better atan2_f16
  1123. return f16le(atan2_f64(f64(y), f64(x)))
  1124. }
  1125. atan2_f16be :: proc "contextless" (y, x: f16be) -> f16be {
  1126. // TODO(bill): Better atan2_f16
  1127. return f16be(atan2_f64(f64(y), f64(x)))
  1128. }
  1129. atan2_f32 :: proc "contextless" (y, x: f32) -> f32 {
  1130. // TODO(bill): Better atan2_f32
  1131. return f32(atan2_f64(f64(y), f64(x)))
  1132. }
  1133. atan2_f32le :: proc "contextless" (y, x: f32le) -> f32le {
  1134. // TODO(bill): Better atan2_f32
  1135. return f32le(atan2_f64(f64(y), f64(x)))
  1136. }
  1137. atan2_f32be :: proc "contextless" (y, x: f32be) -> f32be {
  1138. // TODO(bill): Better atan2_f32
  1139. return f32be(atan2_f64(f64(y), f64(x)))
  1140. }
  1141. atan2_f64 :: proc "contextless" (y, x: f64) -> f64 {
  1142. // TODO(bill): Faster atan2_f64 if possible
  1143. // The original C code:
  1144. // Stephen L. Moshier
  1145. // [email protected]
  1146. NAN :: 0h7fff_ffff_ffff_ffff
  1147. INF :: 0h7FF0_0000_0000_0000
  1148. PI :: 0h4009_21fb_5444_2d18
  1149. atan :: proc "contextless" (x: f64) -> f64 {
  1150. if x == 0 {
  1151. return x
  1152. }
  1153. if x > 0 {
  1154. return s_atan(x)
  1155. }
  1156. return -s_atan(-x)
  1157. }
  1158. // s_atan reduces its argument (known to be positive) to the range [0, 0.66] and calls x_atan.
  1159. s_atan :: proc "contextless" (x: f64) -> f64 {
  1160. MORE_BITS :: 6.123233995736765886130e-17 // pi/2 = PIO2 + MORE_BITS
  1161. TAN3PI08 :: 2.41421356237309504880 // tan(3*pi/8)
  1162. if x <= 0.66 {
  1163. return x_atan(x)
  1164. }
  1165. if x > TAN3PI08 {
  1166. return PI/2 - x_atan(1/x) + MORE_BITS
  1167. }
  1168. return PI/4 + x_atan((x-1)/(x+1)) + 0.5*MORE_BITS
  1169. }
  1170. // x_atan evaluates a series valid in the range [0, 0.66].
  1171. x_atan :: proc "contextless" (x: f64) -> f64 {
  1172. P0 :: -8.750608600031904122785e-01
  1173. P1 :: -1.615753718733365076637e+01
  1174. P2 :: -7.500855792314704667340e+01
  1175. P3 :: -1.228866684490136173410e+02
  1176. P4 :: -6.485021904942025371773e+01
  1177. Q0 :: +2.485846490142306297962e+01
  1178. Q1 :: +1.650270098316988542046e+02
  1179. Q2 :: +4.328810604912902668951e+02
  1180. Q3 :: +4.853903996359136964868e+02
  1181. Q4 :: +1.945506571482613964425e+02
  1182. z := x * x
  1183. z = z * ((((P0*z+P1)*z+P2)*z+P3)*z + P4) / (((((z+Q0)*z+Q1)*z+Q2)*z+Q3)*z + Q4)
  1184. z = x*z + x
  1185. return z
  1186. }
  1187. switch {
  1188. case is_nan(y) || is_nan(x):
  1189. return NAN
  1190. case y == 0:
  1191. if x >= 0 && !sign_bit(x) {
  1192. return copy_sign(0.0, y)
  1193. }
  1194. return copy_sign(PI, y)
  1195. case x == 0:
  1196. return copy_sign(PI*0.5, y)
  1197. case is_inf(x, 0):
  1198. if is_inf(x, 1) {
  1199. if is_inf(y, 0) {
  1200. return copy_sign(PI*0.25, y)
  1201. }
  1202. return copy_sign(0, y)
  1203. }
  1204. if is_inf(y, 0) {
  1205. return copy_sign(PI*0.75, y)
  1206. }
  1207. return copy_sign(PI, y)
  1208. case is_inf(y, 0):
  1209. return copy_sign(PI*0.5, y)
  1210. }
  1211. q := atan(y / x)
  1212. if x < 0 {
  1213. if q <= 0 {
  1214. return q + PI
  1215. }
  1216. return q - PI
  1217. }
  1218. return q
  1219. }
  1220. atan2_f64le :: proc "contextless" (y, x: f64le) -> f64le {
  1221. // TODO(bill): Better atan2_f32
  1222. return f64le(atan2_f64(f64(y), f64(x)))
  1223. }
  1224. atan2_f64be :: proc "contextless" (y, x: f64be) -> f64be {
  1225. // TODO(bill): Better atan2_f32
  1226. return f64be(atan2_f64(f64(y), f64(x)))
  1227. }
  1228. atan2 :: proc{
  1229. atan2_f16, atan2_f16le, atan2_f16be,
  1230. atan2_f32, atan2_f32le, atan2_f32be,
  1231. atan2_f64, atan2_f64le, atan2_f64be,
  1232. }
  1233. atan :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1234. return atan2(x, 1)
  1235. }
  1236. asin :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1237. return atan2(x, sqrt(1 - x*x))
  1238. }
  1239. acos :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1240. return 2 * atan2(sqrt(1 - x), sqrt(1 + x))
  1241. }
  1242. sinh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1243. return (exp(x) - exp(-x))*0.5
  1244. }
  1245. cosh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1246. return (exp(x) + exp(-x))*0.5
  1247. }
  1248. tanh :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) {
  1249. t := exp(2*x)
  1250. return (t - 1) / (t + 1)
  1251. }
  1252. asinh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1253. // The original C code, the long comment, and the constants
  1254. // below are from FreeBSD's /usr/src/lib/msun/src/s_asinh.c
  1255. // and came with this notice.
  1256. //
  1257. // ====================================================
  1258. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1259. //
  1260. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1261. // Permission to use, copy, modify, and distribute this
  1262. // software is freely granted, provided that this notice
  1263. // is preserved.
  1264. // ====================================================
  1265. LN2 :: 0h3FE62E42FEFA39EF
  1266. NEAR_ZERO :: 1.0 / (1 << 28)
  1267. LARGE :: 1 << 28
  1268. x := f64(y)
  1269. if is_nan(x) || is_inf(x) {
  1270. return T(x)
  1271. }
  1272. sign := false
  1273. if x < 0 {
  1274. x = -x
  1275. sign = true
  1276. }
  1277. temp: f64
  1278. switch {
  1279. case x > LARGE:
  1280. temp = ln(x) + LN2
  1281. case x > 2:
  1282. temp = ln(2*x + 1/(sqrt(x*x + 1) + x))
  1283. case x < NEAR_ZERO:
  1284. temp = x
  1285. case:
  1286. temp = log1p(x + x*x/(1 + sqrt(1 + x*x)))
  1287. }
  1288. if sign {
  1289. temp = -temp
  1290. }
  1291. return T(temp)
  1292. }
  1293. acosh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1294. // The original C code, the long comment, and the constants
  1295. // below are from FreeBSD's /usr/src/lib/msun/src/e_acosh.c
  1296. // and came with this notice.
  1297. //
  1298. // ====================================================
  1299. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1300. //
  1301. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1302. // Permission to use, copy, modify, and distribute this
  1303. // software is freely granted, provided that this notice
  1304. // is preserved.
  1305. // ====================================================
  1306. LARGE :: 1<<28
  1307. LN2 :: 0h3FE62E42FEFA39EF
  1308. x := f64(y)
  1309. switch {
  1310. case x < 1 || is_nan(x):
  1311. return T(nan_f64())
  1312. case x == 1:
  1313. return 0
  1314. case x >= LARGE:
  1315. return T(ln(x) + LN2)
  1316. case x > 2:
  1317. return T(ln(2*x - 1/(x+sqrt(x*x-1))))
  1318. }
  1319. t := x-1
  1320. return T(log1p(t + sqrt(2*t + t*t)))
  1321. }
  1322. atanh :: proc "contextless" (y: $T) -> T where intrinsics.type_is_float(T) {
  1323. // The original C code, the long comment, and the constants
  1324. // below are from FreeBSD's /usr/src/lib/msun/src/e_atanh.c
  1325. // and came with this notice.
  1326. //
  1327. // ====================================================
  1328. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1329. //
  1330. // Developed at SunPro, a Sun Microsystems, Inc. business.
  1331. // Permission to use, copy, modify, and distribute this
  1332. // software is freely granted, provided that this notice
  1333. // is preserved.
  1334. // ====================================================
  1335. NEAR_ZERO :: 1.0 / (1 << 28)
  1336. x := f64(y)
  1337. switch {
  1338. case x < -1 || x > 1 || is_nan(x):
  1339. return T(nan_f64())
  1340. case x == 1:
  1341. return T(inf_f64(1))
  1342. case x == -1:
  1343. return T(inf_f64(-1))
  1344. }
  1345. sign := false
  1346. if x < 0 {
  1347. x = -x
  1348. sign = true
  1349. }
  1350. temp: f64
  1351. switch {
  1352. case x < NEAR_ZERO:
  1353. temp = x
  1354. case x < 0.5:
  1355. temp = x + x
  1356. temp = 0.5 * log1p(temp + temp*x/(1-x))
  1357. case:
  1358. temp = 0.5 * log1p((x+x)/(1-x))
  1359. }
  1360. if sign {
  1361. temp = -temp
  1362. }
  1363. return T(temp)
  1364. }
  1365. ilogb_f16 :: proc "contextless" (val: f16) -> int {
  1366. switch {
  1367. case val == 0: return int(min(i32))
  1368. case is_nan(val): return int(max(i32))
  1369. case is_inf(val): return int(max(i32))
  1370. }
  1371. x, exp := normalize_f16(val)
  1372. return int(((transmute(u16)x)>>F16_SHIFT)&F16_MASK) - F16_BIAS + exp
  1373. }
  1374. ilogb_f32 :: proc "contextless" (val: f32) -> int {
  1375. switch {
  1376. case val == 0: return int(min(i32))
  1377. case is_nan(val): return int(max(i32))
  1378. case is_inf(val): return int(max(i32))
  1379. }
  1380. x, exp := normalize_f32(val)
  1381. return int(((transmute(u32)x)>>F32_SHIFT)&F32_MASK) - F32_BIAS + exp
  1382. }
  1383. ilogb_f64 :: proc "contextless" (val: f64) -> int {
  1384. switch {
  1385. case val == 0: return int(min(i32))
  1386. case is_nan(val): return int(max(i32))
  1387. case is_inf(val): return int(max(i32))
  1388. }
  1389. x, exp := normalize_f64(val)
  1390. return int(((transmute(u64)x)>>F64_SHIFT)&F64_MASK) - F64_BIAS + exp
  1391. }
  1392. ilogb_f16le :: proc "contextless" (value: f16le) -> int { return ilogb_f16(f16(value)) }
  1393. ilogb_f16be :: proc "contextless" (value: f16be) -> int { return ilogb_f16(f16(value)) }
  1394. ilogb_f32le :: proc "contextless" (value: f32le) -> int { return ilogb_f32(f32(value)) }
  1395. ilogb_f32be :: proc "contextless" (value: f32be) -> int { return ilogb_f32(f32(value)) }
  1396. ilogb_f64le :: proc "contextless" (value: f64le) -> int { return ilogb_f64(f64(value)) }
  1397. ilogb_f64be :: proc "contextless" (value: f64be) -> int { return ilogb_f64(f64(value)) }
  1398. ilogb :: proc {
  1399. ilogb_f16,
  1400. ilogb_f32,
  1401. ilogb_f64,
  1402. ilogb_f16le,
  1403. ilogb_f16be,
  1404. ilogb_f32le,
  1405. ilogb_f32be,
  1406. ilogb_f64le,
  1407. ilogb_f64be,
  1408. }
  1409. logb_f16 :: proc "contextless" (val: f16) -> f16 {
  1410. switch {
  1411. case val == 0: return inf_f16(-1)
  1412. case is_inf(val): return inf_f16(+1)
  1413. case is_nan(val): return val
  1414. }
  1415. return f16(ilogb(val))
  1416. }
  1417. logb_f32 :: proc "contextless" (val: f32) -> f32 {
  1418. switch {
  1419. case val == 0: return inf_f32(-1)
  1420. case is_inf(val): return inf_f32(+1)
  1421. case is_nan(val): return val
  1422. }
  1423. return f32(ilogb(val))
  1424. }
  1425. logb_f64 :: proc "contextless" (val: f64) -> f64 {
  1426. switch {
  1427. case val == 0: return inf_f64(-1)
  1428. case is_inf(val): return inf_f64(+1)
  1429. case is_nan(val): return val
  1430. }
  1431. return f64(ilogb(val))
  1432. }
  1433. logb_f16le :: proc "contextless" (value: f16le) -> f16le { return f16le(logb_f16(f16(value))) }
  1434. logb_f16be :: proc "contextless" (value: f16be) -> f16be { return f16be(logb_f16(f16(value))) }
  1435. logb_f32le :: proc "contextless" (value: f32le) -> f32le { return f32le(logb_f32(f32(value))) }
  1436. logb_f32be :: proc "contextless" (value: f32be) -> f32be { return f32be(logb_f32(f32(value))) }
  1437. logb_f64le :: proc "contextless" (value: f64le) -> f64le { return f64le(logb_f64(f64(value))) }
  1438. logb_f64be :: proc "contextless" (value: f64be) -> f64be { return f64be(logb_f64(f64(value))) }
  1439. logb :: proc {
  1440. logb_f16,
  1441. logb_f32,
  1442. logb_f64,
  1443. logb_f16le,
  1444. logb_f16be,
  1445. logb_f32le,
  1446. logb_f32be,
  1447. logb_f64le,
  1448. logb_f64be,
  1449. }
  1450. nextafter_f16 :: proc "contextless" (x, y: f16) -> (r: f16) {
  1451. switch {
  1452. case is_nan(x) || is_nan(y):
  1453. r = nan_f16()
  1454. case x == y:
  1455. r = x
  1456. case x == 0:
  1457. r = copy_sign_f16(1, y)
  1458. case (y > x) == (x > 0):
  1459. r = transmute(f16)(transmute(u16)x + 1)
  1460. case:
  1461. r = transmute(f16)(transmute(u16)x - 1)
  1462. }
  1463. return
  1464. }
  1465. nextafter_f32 :: proc "contextless" (x, y: f32) -> (r: f32) {
  1466. switch {
  1467. case is_nan(x) || is_nan(y):
  1468. r = nan_f32()
  1469. case x == y:
  1470. r = x
  1471. case x == 0:
  1472. r = copy_sign_f32(1, y)
  1473. case (y > x) == (x > 0):
  1474. r = transmute(f32)(transmute(u32)x + 1)
  1475. case:
  1476. r = transmute(f32)(transmute(u32)x - 1)
  1477. }
  1478. return
  1479. }
  1480. nextafter_f64 :: proc "contextless" (x, y: f64) -> (r: f64) {
  1481. switch {
  1482. case is_nan(x) || is_nan(y):
  1483. r = nan_f64()
  1484. case x == y:
  1485. r = x
  1486. case x == 0:
  1487. r = copy_sign_f64(1, y)
  1488. case (y > x) == (x > 0):
  1489. r = transmute(f64)(transmute(u64)x + 1)
  1490. case:
  1491. r = transmute(f64)(transmute(u64)x - 1)
  1492. }
  1493. return
  1494. }
  1495. nextafter_f16le :: proc "contextless" (x, y: f16le) -> (r: f16le) { return f16le(nextafter_f16(f16(x), f16(y))) }
  1496. nextafter_f16be :: proc "contextless" (x, y: f16be) -> (r: f16be) { return f16be(nextafter_f16(f16(x), f16(y))) }
  1497. nextafter_f32le :: proc "contextless" (x, y: f32le) -> (r: f32le) { return f32le(nextafter_f32(f32(x), f32(y))) }
  1498. nextafter_f32be :: proc "contextless" (x, y: f32be) -> (r: f32be) { return f32be(nextafter_f32(f32(x), f32(y))) }
  1499. nextafter_f64le :: proc "contextless" (x, y: f64le) -> (r: f64le) { return f64le(nextafter_f64(f64(x), f64(y))) }
  1500. nextafter_f64be :: proc "contextless" (x, y: f64be) -> (r: f64be) { return f64be(nextafter_f64(f64(x), f64(y))) }
  1501. nextafter :: proc{
  1502. nextafter_f16, nextafter_f16le, nextafter_f16be,
  1503. nextafter_f32, nextafter_f32le, nextafter_f32be,
  1504. nextafter_f64, nextafter_f64le, nextafter_f64be,
  1505. }
  1506. signbit_f16 :: proc "contextless" (x: f16) -> bool {
  1507. return (transmute(u16)x)&(1<<15) != 0
  1508. }
  1509. signbit_f32 :: proc "contextless" (x: f32) -> bool {
  1510. return (transmute(u32)x)&(1<<31) != 0
  1511. }
  1512. signbit_f64 :: proc "contextless" (x: f64) -> bool {
  1513. return (transmute(u64)x)&(1<<63) != 0
  1514. }
  1515. signbit_f16le :: proc "contextless" (x: f16le) -> bool { return signbit_f16(f16(x)) }
  1516. signbit_f32le :: proc "contextless" (x: f32le) -> bool { return signbit_f32(f32(x)) }
  1517. signbit_f64le :: proc "contextless" (x: f64le) -> bool { return signbit_f64(f64(x)) }
  1518. signbit_f16be :: proc "contextless" (x: f16be) -> bool { return signbit_f16(f16(x)) }
  1519. signbit_f32be :: proc "contextless" (x: f32be) -> bool { return signbit_f32(f32(x)) }
  1520. signbit_f64be :: proc "contextless" (x: f64be) -> bool { return signbit_f64(f64(x)) }
  1521. signbit :: proc{
  1522. signbit_f16, signbit_f16le, signbit_f16be,
  1523. signbit_f32, signbit_f32le, signbit_f32be,
  1524. signbit_f64, signbit_f64le, signbit_f64be,
  1525. }
  1526. F16_DIG :: 3
  1527. F16_EPSILON :: 0.00097656
  1528. F16_GUARD :: 0
  1529. F16_MANT_DIG :: 11
  1530. F16_MAX :: 65504.0
  1531. F16_MAX_10_EXP :: 4
  1532. F16_MAX_EXP :: 15
  1533. F16_MIN :: 6.10351562e-5
  1534. F16_MIN_10_EXP :: -4
  1535. F16_MIN_EXP :: -14
  1536. F16_NORMALIZE :: 0
  1537. F16_RADIX :: 2
  1538. F16_ROUNDS :: 1
  1539. F32_DIG :: 6
  1540. F32_EPSILON :: 1.192092896e-07
  1541. F32_GUARD :: 0
  1542. F32_MANT_DIG :: 24
  1543. F32_MAX :: 3.402823466e+38
  1544. F32_MAX_10_EXP :: 38
  1545. F32_MAX_EXP :: 128
  1546. F32_MIN :: 1.175494351e-38
  1547. F32_MIN_10_EXP :: -37
  1548. F32_MIN_EXP :: -125
  1549. F32_NORMALIZE :: 0
  1550. F32_RADIX :: 2
  1551. F32_ROUNDS :: 1
  1552. F64_DIG :: 15 // # of decimal digits of precision
  1553. F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
  1554. F64_MANT_DIG :: 53 // # of bits in mantissa
  1555. F64_MAX :: 1.7976931348623158e+308 // max value
  1556. F64_MAX_10_EXP :: 308 // max decimal exponent
  1557. F64_MAX_EXP :: 1024 // max binary exponent
  1558. F64_MIN :: 2.2250738585072014e-308 // min positive value
  1559. F64_MIN_10_EXP :: -307 // min decimal exponent
  1560. F64_MIN_EXP :: -1021 // min binary exponent
  1561. F64_RADIX :: 2 // exponent radix
  1562. F64_ROUNDS :: 1 // addition rounding: near
  1563. F16_MASK :: 0x1f
  1564. F16_SHIFT :: 16 - 6
  1565. F16_BIAS :: 0xf
  1566. F32_MASK :: 0xff
  1567. F32_SHIFT :: 32 - 9
  1568. F32_BIAS :: 0x7f
  1569. F64_MASK :: 0x7ff
  1570. F64_SHIFT :: 64 - 12
  1571. F64_BIAS :: 0x3ff
  1572. INF_F16 :f16: 0h7C00
  1573. NEG_INF_F16 :f16: 0hFC00
  1574. SNAN_F16 :f16: 0h7C01
  1575. QNAN_F16 :f16: 0h7E01
  1576. INF_F32 :f32: 0h7F80_0000
  1577. NEG_INF_F32 :f32: 0hFF80_0000
  1578. SNAN_F32 :f32: 0hFF80_0001
  1579. QNAN_F32 :f32: 0hFFC0_0001
  1580. INF_F64 :f64: 0h7FF0_0000_0000_0000
  1581. NEG_INF_F64 :f64: 0hFFF0_0000_0000_0000
  1582. SNAN_F64 :f64: 0h7FF0_0000_0000_0001
  1583. QNAN_F64 :f64: 0h7FF8_0000_0000_0001