math_basic_js.odin 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //+build js
  2. package math
  3. import "core:intrinsics"
  4. foreign import "odin_env"
  5. @(default_calling_convention="c")
  6. foreign odin_env {
  7. @(link_name="sin")
  8. sin_f64 :: proc(θ: f64) -> f64 ---
  9. @(link_name="cos")
  10. cos_f64 :: proc(θ: f64) -> f64 ---
  11. @(link_name="pow")
  12. pow_f64 :: proc(x, power: f64) -> f64 ---
  13. @(link_name="fmuladd")
  14. fmuladd_f64 :: proc(a, b, c: f64) -> f64 ---
  15. @(link_name="ln")
  16. ln_f64 :: proc(x: f64) -> f64 ---
  17. @(link_name="exp")
  18. exp_f64 :: proc(x: f64) -> f64 ---
  19. }
  20. sqrt_f64 :: proc "contextless" (x: f64) -> f64 {
  21. return intrinsics.sqrt(x)
  22. }
  23. sqrt_f16 :: proc "c" (x: f16) -> f16 { return f16(sqrt_f64(f64(x))) }
  24. sin_f16 :: proc "c" (θ: f16) -> f16 { return f16(sin_f64(f64(θ))) }
  25. cos_f16 :: proc "c" (θ: f16) -> f16 { return f16(cos_f64(f64(θ))) }
  26. pow_f16 :: proc "c" (x, power: f16) -> f16 { return f16(pow_f64(f64(x), f64(power))) }
  27. fmuladd_f16 :: proc "c" (a, b, c: f16) -> f16 { return f16(fmuladd_f64(f64(a), f64(a), f64(c))) }
  28. ln_f16 :: proc "c" (x: f16) -> f16 { return f16(ln_f64(f64(x))) }
  29. exp_f16 :: proc "c" (x: f16) -> f16 { return f16(exp_f64(f64(x))) }
  30. sqrt_f32 :: proc "c" (x: f32) -> f32 { return f32(sqrt_f64(f64(x))) }
  31. sin_f32 :: proc "c" (θ: f32) -> f32 { return f32(sin_f64(f64(θ))) }
  32. cos_f32 :: proc "c" (θ: f32) -> f32 { return f32(cos_f64(f64(θ))) }
  33. pow_f32 :: proc "c" (x, power: f32) -> f32 { return f32(pow_f64(f64(x), f64(power))) }
  34. fmuladd_f32 :: proc "c" (a, b, c: f32) -> f32 { return f32(fmuladd_f64(f64(a), f64(a), f64(c))) }
  35. ln_f32 :: proc "c" (x: f32) -> f32 { return f32(ln_f64(f64(x))) }
  36. exp_f32 :: proc "c" (x: f32) -> f32 { return f32(exp_f64(f64(x))) }
  37. ln_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(ln_f64(f64(x))) }
  38. ln_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(ln_f64(f64(x))) }
  39. ln_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(ln_f64(f64(x))) }
  40. ln_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(ln_f64(f64(x))) }
  41. ln_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(ln_f64(f64(x))) }
  42. ln_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(ln_f64(f64(x))) }
  43. ln :: proc{
  44. ln_f16, ln_f16le, ln_f16be,
  45. ln_f32, ln_f32le, ln_f32be,
  46. ln_f64, ln_f64le, ln_f64be,
  47. }