Skip to content

Commit

Permalink
Merge pull request #681 from zig-lang/hw-math
Browse files Browse the repository at this point in the history
Add hw sqrt for x86_64
  • Loading branch information
andrewrk committed Jan 10, 2018
2 parents d4f791c + 24cd991 commit 891c93c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -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"
Expand Down
18 changes: 14 additions & 4 deletions std/math/sqrt.zig
Expand Up @@ -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)) {
Expand Down
15 changes: 15 additions & 0 deletions 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)
);
}

0 comments on commit 891c93c

Please sign in to comment.