mirror of
https://github.com/ziglang/zig.git
synced 2026-02-18 15:31:48 +00:00
Add hw sqrt for x86_64
This commit is contained in:
parent
3c094116aa
commit
24cd99160c
@ -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"
|
||||
|
||||
@ -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)) {
|
||||
|
||||
15
std/math/x86_64/sqrt.zig
Normal file
15
std/math/x86_64/sqrt.zig
Normal file
@ -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)
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user