Browse Source

Implement `math.sqrt` with `intrinsics.sqrt`

gingerBill 3 years ago
parent
commit
91949b0992
2 changed files with 17 additions and 9 deletions
  1. 12 7
      core/math/math_basic.odin
  2. 5 2
      core/math/math_js.odin

+ 12 - 7
core/math/math_basic.odin

@@ -1,15 +1,10 @@
 //+build !js
 //+build !js
 package math
 package math
 
 
+import "core:intrinsics"
+
 @(default_calling_convention="none")
 @(default_calling_convention="none")
 foreign _ {
 foreign _ {
-	@(link_name="llvm.sqrt.f16")
-	sqrt_f16 :: proc(x: f16) -> f16 ---
-	@(link_name="llvm.sqrt.f32")
-	sqrt_f32 :: proc(x: f32) -> f32 ---
-	@(link_name="llvm.sqrt.f64")
-	sqrt_f64 :: proc(x: f64) -> f64 ---
-
 	@(link_name="llvm.sin.f16")
 	@(link_name="llvm.sin.f16")
 	sin_f16 :: proc(θ: f16) -> f16 ---
 	sin_f16 :: proc(θ: f16) -> f16 ---
 	@(link_name="llvm.sin.f32")
 	@(link_name="llvm.sin.f32")
@@ -52,3 +47,13 @@ foreign _ {
 	@(link_name="llvm.exp.f64")
 	@(link_name="llvm.exp.f64")
 	exp_f64 :: proc(x: f64) -> f64 ---
 	exp_f64 :: proc(x: f64) -> f64 ---
 }
 }
+
+sqrt_f16 :: proc "contextless" (x: f16) -> f16 {
+	return intrinsics.sqrt(x)
+}
+sqrt_f32 :: proc "contextless" (x: f32) -> f32 {
+	return intrinsics.sqrt(x)
+}
+sqrt_f64 :: proc "contextless" (x: f64) -> f64 {
+	return intrinsics.sqrt(x)
+}

+ 5 - 2
core/math/math_js.odin

@@ -1,12 +1,12 @@
 //+build js
 //+build js
 package math
 package math
 
 
+import "core:intrinsics"
+
 foreign import "odin_env"
 foreign import "odin_env"
 
 
 @(default_calling_convention="c")
 @(default_calling_convention="c")
 foreign odin_env {
 foreign odin_env {
-	@(link_name="sqrt")
-	sqrt_f64 :: proc(x: f64) -> f64 ---
 	@(link_name="sin")
 	@(link_name="sin")
 	sin_f64 :: proc(θ: f64) -> f64 ---
 	sin_f64 :: proc(θ: f64) -> f64 ---
 	@(link_name="cos")
 	@(link_name="cos")
@@ -21,6 +21,9 @@ foreign odin_env {
 	exp_f64 :: proc(x: f64) -> f64 ---
 	exp_f64 :: proc(x: f64) -> f64 ---
 }
 }
 
 
+sqrt_f64 :: proc "contextless" (x: f64) -> f64 {
+	return intrinsics.sqrt(x)
+}
 
 
 sqrt_f16    :: proc "c" (x: f16) -> f16             { return f16(sqrt_f64(f64(x)))                    }
 sqrt_f16    :: proc "c" (x: f16) -> f16             { return f16(sqrt_f64(f64(x)))                    }
 sin_f16     :: proc "c" (θ: f16) -> f16             { return f16(sin_f64(f64(θ)))                     }
 sin_f16     :: proc "c" (θ: f16) -> f16             { return f16(sin_f64(f64(θ)))                     }