Commit 24cd99160c

Marc Tiehuis <marctiehuis@gmail.com>
2018-01-10 07:53:36
Add hw sqrt for x86_64
1 parent 3c09411
Changed files (3)
std/math/x86_64/sqrt.zig
@@ -0,0 +1,15 @@
+pub fn sqrt32(x: f32) -> f32 {
+    return asm (
+        \\sqrtss %%xmm0, %%xmm0
+        : [ret] "={xmm0}" (-> f32)
+        : [x] "{xmm0}" (x)
+    );
+}
+
+pub fn sqrt64(x: f64) -> f64 {
+    return asm (
+        \\sqrtsd %%xmm0, %%xmm0
+        : [ret] "={xmm0}" (-> f64)
+        : [x] "{xmm0}" (x)
+    );
+}
std/math/sqrt.zig
@@ -18,11 +18,21 @@ pub fn sqrt(x: var) -> (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @
             return T(sqrt64(x));
         },
         TypeId.Float => {
-            return switch (T) {
-                f32 => sqrt32(x),
-                f64 => sqrt64(x),
+            switch (T) {
+                f32 => {
+                    switch (builtin.arch) {
+                        builtin.Arch.x86_64 => return @import("x86_64/sqrt.zig").sqrt32(x),
+                        else => return sqrt32(x),
+                    }
+                },
+                f64 => {
+                    switch (builtin.arch) {
+                        builtin.Arch.x86_64 => return @import("x86_64/sqrt.zig").sqrt64(x),
+                        else => return sqrt64(x),
+                    }
+                },
                 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
-            };
+            }
         },
         TypeId.IntLiteral => comptime {
             if (x > @maxValue(u128)) {
CMakeLists.txt
@@ -425,6 +425,7 @@ set(ZIG_STD_FILES
     "math/tan.zig"
     "math/tanh.zig"
     "math/trunc.zig"
+    "math/x86_64/sqrt.zig"
     "mem.zig"
     "net.zig"
     "os/child_process.zig"