diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-26 01:54:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-26 01:54:45 -0400 |
| commit | 68bb3945708c43109c48bda3664176307d45b62c (patch) | |
| tree | afb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/math/fabs.zig | |
| parent | 6128bc728d1e1024a178c16c2149f5b1a167a013 (diff) | |
| parent | 4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff) | |
| download | zig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz zig-68bb3945708c43109c48bda3664176307d45b62c.zip | |
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/math/fabs.zig')
| -rw-r--r-- | lib/std/math/fabs.zig | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/std/math/fabs.zig b/lib/std/math/fabs.zig new file mode 100644 index 0000000000..6469f38835 --- /dev/null +++ b/lib/std/math/fabs.zig @@ -0,0 +1,101 @@ +// Ported from musl, which is licensed under the MIT license: +// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT +// +// https://git.musl-libc.org/cgit/musl/tree/src/math/fabsf.c +// https://git.musl-libc.org/cgit/musl/tree/src/math/fabs.c + +const std = @import("../std.zig"); +const math = std.math; +const expect = std.testing.expect; +const maxInt = std.math.maxInt; + +/// Returns the absolute value of x. +/// +/// Special Cases: +/// - fabs(+-inf) = +inf +/// - fabs(nan) = nan +pub fn fabs(x: var) @typeOf(x) { + const T = @typeOf(x); + return switch (T) { + f16 => fabs16(x), + f32 => fabs32(x), + f64 => fabs64(x), + f128 => fabs128(x), + else => @compileError("fabs not implemented for " ++ @typeName(T)), + }; +} + +fn fabs16(x: f16) f16 { + var u = @bitCast(u16, x); + u &= 0x7FFF; + return @bitCast(f16, u); +} + +fn fabs32(x: f32) f32 { + var u = @bitCast(u32, x); + u &= 0x7FFFFFFF; + return @bitCast(f32, u); +} + +fn fabs64(x: f64) f64 { + var u = @bitCast(u64, x); + u &= maxInt(u64) >> 1; + return @bitCast(f64, u); +} + +fn fabs128(x: f128) f128 { + var u = @bitCast(u128, x); + u &= maxInt(u128) >> 1; + return @bitCast(f128, u); +} + +test "math.fabs" { + expect(fabs(f16(1.0)) == fabs16(1.0)); + expect(fabs(f32(1.0)) == fabs32(1.0)); + expect(fabs(f64(1.0)) == fabs64(1.0)); + expect(fabs(f128(1.0)) == fabs128(1.0)); +} + +test "math.fabs16" { + expect(fabs16(1.0) == 1.0); + expect(fabs16(-1.0) == 1.0); +} + +test "math.fabs32" { + expect(fabs32(1.0) == 1.0); + expect(fabs32(-1.0) == 1.0); +} + +test "math.fabs64" { + expect(fabs64(1.0) == 1.0); + expect(fabs64(-1.0) == 1.0); +} + +test "math.fabs128" { + expect(fabs128(1.0) == 1.0); + expect(fabs128(-1.0) == 1.0); +} + +test "math.fabs16.special" { + expect(math.isPositiveInf(fabs(math.inf(f16)))); + expect(math.isPositiveInf(fabs(-math.inf(f16)))); + expect(math.isNan(fabs(math.nan(f16)))); +} + +test "math.fabs32.special" { + expect(math.isPositiveInf(fabs(math.inf(f32)))); + expect(math.isPositiveInf(fabs(-math.inf(f32)))); + expect(math.isNan(fabs(math.nan(f32)))); +} + +test "math.fabs64.special" { + expect(math.isPositiveInf(fabs(math.inf(f64)))); + expect(math.isPositiveInf(fabs(-math.inf(f64)))); + expect(math.isNan(fabs(math.nan(f64)))); +} + +test "math.fabs128.special" { + expect(math.isPositiveInf(fabs(math.inf(f128)))); + expect(math.isPositiveInf(fabs(-math.inf(f128)))); + expect(math.isNan(fabs(math.nan(f128)))); +} |
