2017-06-16 01:26:10 -07:00
|
|
|
const math = @import("index.zig");
|
|
|
|
const assert = @import("../debug.zig").assert;
|
|
|
|
|
2017-08-28 01:28:42 -07:00
|
|
|
pub fn signbit(x: var) -> bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
const T = @typeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => signbit32(x),
|
|
|
|
f64 => signbit64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("signbit not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signbit32(x: f32) -> bool {
|
|
|
|
const bits = @bitCast(u32, x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return bits >> 31 != 0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signbit64(x: f64) -> bool {
|
|
|
|
const bits = @bitCast(u64, x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return bits >> 63 != 0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(signbit(f32(4.0)) == signbit32(4.0));
|
|
|
|
assert(signbit(f64(4.0)) == signbit64(4.0));
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(!signbit32(4.0));
|
|
|
|
assert(signbit32(-3.0));
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(!signbit64(4.0));
|
|
|
|
assert(signbit64(-3.0));
|
|
|
|
}
|