math_basic.odin 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //+build !js
  2. package math
  3. import "core:intrinsics"
  4. @(default_calling_convention="none")
  5. foreign _ {
  6. @(link_name="llvm.sin.f16")
  7. sin_f16 :: proc(θ: f16) -> f16 ---
  8. @(link_name="llvm.sin.f32")
  9. sin_f32 :: proc(θ: f32) -> f32 ---
  10. @(link_name="llvm.sin.f64")
  11. sin_f64 :: proc(θ: f64) -> f64 ---
  12. @(link_name="llvm.cos.f16")
  13. cos_f16 :: proc(θ: f16) -> f16 ---
  14. @(link_name="llvm.cos.f32")
  15. cos_f32 :: proc(θ: f32) -> f32 ---
  16. @(link_name="llvm.cos.f64")
  17. cos_f64 :: proc(θ: f64) -> f64 ---
  18. @(link_name="llvm.pow.f16")
  19. pow_f16 :: proc(x, power: f16) -> f16 ---
  20. @(link_name="llvm.pow.f32")
  21. pow_f32 :: proc(x, power: f32) -> f32 ---
  22. @(link_name="llvm.pow.f64")
  23. pow_f64 :: proc(x, power: f64) -> f64 ---
  24. @(link_name="llvm.fmuladd.f16")
  25. fmuladd_f16 :: proc(a, b, c: f16) -> f16 ---
  26. @(link_name="llvm.fmuladd.f32")
  27. fmuladd_f32 :: proc(a, b, c: f32) -> f32 ---
  28. @(link_name="llvm.fmuladd.f64")
  29. fmuladd_f64 :: proc(a, b, c: f64) -> f64 ---
  30. @(link_name="llvm.exp.f16")
  31. exp_f16 :: proc(x: f16) -> f16 ---
  32. @(link_name="llvm.exp.f32")
  33. exp_f32 :: proc(x: f32) -> f32 ---
  34. @(link_name="llvm.exp.f64")
  35. exp_f64 :: proc(x: f64) -> f64 ---
  36. }
  37. sqrt_f16 :: proc "contextless" (x: f16) -> f16 {
  38. return intrinsics.sqrt(x)
  39. }
  40. sqrt_f32 :: proc "contextless" (x: f32) -> f32 {
  41. return intrinsics.sqrt(x)
  42. }
  43. sqrt_f64 :: proc "contextless" (x: f64) -> f64 {
  44. return intrinsics.sqrt(x)
  45. }
  46. ln_f64 :: proc "contextless" (x: f64) -> f64 {
  47. // The original C code, the long comment, and the constants
  48. // below are from FreeBSD's /usr/src/lib/msun/src/e_log.c
  49. // and came with this notice.
  50. //
  51. // ====================================================
  52. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  53. //
  54. // Developed at SunPro, a Sun Microsystems, Inc. business.
  55. // Permission to use, copy, modify, and distribute this
  56. // software is freely granted, provided that this notice
  57. // is preserved.
  58. // ====================================================
  59. //
  60. // __ieee754_log(x)
  61. // Return the logarithm of x
  62. //
  63. // Method :
  64. // 1. Argument Reduction: find k and f such that
  65. // x = 2**k * (1+f),
  66. // where sqrt(2)/2 < 1+f < sqrt(2) .
  67. //
  68. // 2. Approximation of log(1+f).
  69. // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  70. // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  71. // = 2s + s*R
  72. // We use a special Reme algorithm on [0,0.1716] to generate
  73. // a polynomial of degree 14 to approximate R. The maximum error
  74. // of this polynomial approximation is bounded by 2**-58.45. In
  75. // other words,
  76. // 2 4 6 8 10 12 14
  77. // R(z) ~ L1*s +L2*s +L3*s +L4*s +L5*s +L6*s +L7*s
  78. // (the values of L1 to L7 are listed in the program) and
  79. // | 2 14 | -58.45
  80. // | L1*s +...+L7*s - R(z) | <= 2
  81. // | |
  82. // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  83. // In order to guarantee error in log below 1ulp, we compute log by
  84. // log(1+f) = f - s*(f - R) (if f is not too large)
  85. // log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
  86. //
  87. // 3. Finally, log(x) = k*Ln2 + log(1+f).
  88. // = k*Ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*Ln2_lo)))
  89. // Here Ln2 is split into two floating point number:
  90. // Ln2_hi + Ln2_lo,
  91. // where n*Ln2_hi is always exact for |n| < 2000.
  92. //
  93. // Special cases:
  94. // log(x) is NaN with signal if x < 0 (including -INF) ;
  95. // log(+INF) is +INF; log(0) is -INF with signal;
  96. // log(NaN) is that NaN with no signal.
  97. //
  98. // Accuracy:
  99. // according to an error analysis, the error is always less than
  100. // 1 ulp (unit in the last place).
  101. //
  102. // Constants:
  103. // The hexadecimal values are the intended ones for the following
  104. // constants. The decimal values may be used, provided that the
  105. // compiler will convert from decimal to binary accurately enough
  106. // to produce the hexadecimal values shown.
  107. LN2_HI :: 0h3fe62e42_fee00000 // 6.93147180369123816490e-01
  108. LN2_LO :: 0h3dea39ef_35793c76 // 1.90821492927058770002e-10
  109. L1 :: 0h3fe55555_55555593 // 6.666666666666735130e-01
  110. L2 :: 0h3fd99999_9997fa04 // 3.999999999940941908e-01
  111. L3 :: 0h3fd24924_94229359 // 2.857142874366239149e-01
  112. L4 :: 0h3fcc71c5_1d8e78af // 2.222219843214978396e-01
  113. L5 :: 0h3fc74664_96cb03de // 1.818357216161805012e-01
  114. L6 :: 0h3fc39a09_d078c69f // 1.531383769920937332e-01
  115. L7 :: 0h3fc2f112_df3e5244 // 1.479819860511658591e-01
  116. switch {
  117. case is_nan(x) || is_inf(x, 1):
  118. return x
  119. case x < 0:
  120. return nan_f64()
  121. case x == 0:
  122. return inf_f64(-1)
  123. }
  124. // reduce
  125. f1, ki := frexp(x)
  126. if f1 < SQRT_TWO/2 {
  127. f1 *= 2
  128. ki -= 1
  129. }
  130. f := f1 - 1
  131. k := f64(ki)
  132. // compute
  133. s := f / (2 + f)
  134. s2 := s * s
  135. s4 := s2 * s2
  136. t1 := s2 * (L1 + s4*(L3+s4*(L5+s4*L7)))
  137. t2 := s4 * (L2 + s4*(L4+s4*L6))
  138. R := t1 + t2
  139. hfsq := 0.5 * f * f
  140. return k*LN2_HI - ((hfsq - (s*(hfsq+R) + k*LN2_LO)) - f)
  141. }
  142. ln_f16 :: proc "contextless" (x: f16) -> f16 { return #force_inline f16(ln_f64(f64(x))) }
  143. ln_f32 :: proc "contextless" (x: f32) -> f32 { return #force_inline f32(ln_f64(f64(x))) }
  144. ln_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(ln_f64(f64(x))) }
  145. ln_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(ln_f64(f64(x))) }
  146. ln_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(ln_f64(f64(x))) }
  147. ln_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(ln_f64(f64(x))) }
  148. ln_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(ln_f64(f64(x))) }
  149. ln_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(ln_f64(f64(x))) }
  150. ln :: proc{
  151. ln_f16, ln_f16le, ln_f16be,
  152. ln_f32, ln_f32le, ln_f32be,
  153. ln_f64, ln_f64le, ln_f64be,
  154. }