diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-01-10 10:22:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-10 10:22:40 -0500 |
| commit | 891c93c118a85ce16225ed2e97676cdae2abe657 (patch) | |
| tree | 8fb47079465fbcd5d58f7d477678987ade56c032 | |
| parent | d4f791cf6c560da25a0b84b4eb2ad68f8e7d27fd (diff) | |
| parent | 24cd99160c2eeea34088ef3e3d631233d8913dc1 (diff) | |
| download | zig-891c93c118a85ce16225ed2e97676cdae2abe657.tar.gz zig-891c93c118a85ce16225ed2e97676cdae2abe657.zip | |
Merge pull request #681 from zig-lang/hw-math
Add hw sqrt for x86_64
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | std/math/sqrt.zig | 18 | ||||
| -rw-r--r-- | std/math/x86_64/sqrt.zig | 15 |
3 files changed, 30 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e1bfcf72b6..252b07e6dd 100644 --- a/CMakeLists.txt +++ b/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" diff --git a/std/math/sqrt.zig b/std/math/sqrt.zig index 4d84756b4a..18cee6e248 100644 --- a/std/math/sqrt.zig +++ b/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)) { diff --git a/std/math/x86_64/sqrt.zig b/std/math/x86_64/sqrt.zig new file mode 100644 index 0000000000..fbb898c297 --- /dev/null +++ b/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) + ); +} |
