2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const expect = std.testing.expect;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2018-01-25 01:10:11 -08: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) {
|
2018-06-29 16:44:54 -07:00
|
|
|
f16 => signbit16(x),
|
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
|
|
|
}
|
|
|
|
|
2018-06-29 16:44:54 -07:00
|
|
|
fn signbit16(x: f16) bool {
|
|
|
|
const bits = @bitCast(u16, x);
|
|
|
|
return bits >> 15 != 0;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn signbit32(x: f32) bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn signbit64(x: f64) bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
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" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(signbit(f16(4.0)) == signbit16(4.0));
|
|
|
|
expect(signbit(f32(4.0)) == signbit32(4.0));
|
|
|
|
expect(signbit(f64(4.0)) == signbit64(4.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:44:54 -07:00
|
|
|
test "math.signbit16" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(!signbit16(4.0));
|
|
|
|
expect(signbit16(-3.0));
|
2018-06-29 16:44:54 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit32" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(!signbit32(4.0));
|
|
|
|
expect(signbit32(-3.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit64" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(!signbit64(4.0));
|
|
|
|
expect(signbit64(-3.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|