Skip to content

Add hw sqrt for x86_64 #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
);
}