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/signbit.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/signbit.zig')
| -rw-r--r-- | lib/std/math/signbit.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/std/math/signbit.zig b/lib/std/math/signbit.zig new file mode 100644 index 0000000000..e5c5909292 --- /dev/null +++ b/lib/std/math/signbit.zig @@ -0,0 +1,50 @@ +const std = @import("../std.zig"); +const math = std.math; +const expect = std.testing.expect; + +/// Returns whether x is negative or negative 0. +pub fn signbit(x: var) bool { + const T = @typeOf(x); + return switch (T) { + f16 => signbit16(x), + f32 => signbit32(x), + f64 => signbit64(x), + else => @compileError("signbit not implemented for " ++ @typeName(T)), + }; +} + +fn signbit16(x: f16) bool { + const bits = @bitCast(u16, x); + return bits >> 15 != 0; +} + +fn signbit32(x: f32) bool { + const bits = @bitCast(u32, x); + return bits >> 31 != 0; +} + +fn signbit64(x: f64) bool { + const bits = @bitCast(u64, x); + return bits >> 63 != 0; +} + +test "math.signbit" { + expect(signbit(f16(4.0)) == signbit16(4.0)); + expect(signbit(f32(4.0)) == signbit32(4.0)); + expect(signbit(f64(4.0)) == signbit64(4.0)); +} + +test "math.signbit16" { + expect(!signbit16(4.0)); + expect(signbit16(-3.0)); +} + +test "math.signbit32" { + expect(!signbit32(4.0)); + expect(signbit32(-3.0)); +} + +test "math.signbit64" { + expect(!signbit64(4.0)); + expect(signbit64(-3.0)); +} |
