2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - fabs(+-inf) = +inf
|
|
|
|
// - fabs(nan) = nan
|
|
|
|
|
2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-07 19:56:57 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn fabs(x: var) @typeOf(x) {
|
2017-06-07 19:56:57 -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 => fabs16(x),
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => fabs32(x),
|
|
|
|
f64 => fabs64(x),
|
2017-06-07 19:56:57 -07:00
|
|
|
else => @compileError("fabs not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-07 19:56:57 -07:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:44:54 -07:00
|
|
|
fn fabs16(x: f16) f16 {
|
|
|
|
var u = @bitCast(u16, x);
|
|
|
|
u &= 0x7FFF;
|
|
|
|
return @bitCast(f16, u);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn fabs32(x: f32) f32 {
|
2017-06-07 19:56:57 -07:00
|
|
|
var u = @bitCast(u32, x);
|
|
|
|
u &= 0x7FFFFFFF;
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f32, u);
|
2017-06-07 19:56:57 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn fabs64(x: f64) f64 {
|
2017-06-07 19:56:57 -07:00
|
|
|
var u = @bitCast(u64, x);
|
|
|
|
u &= @maxValue(u64) >> 1;
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f64, u);
|
2017-06-07 19:56:57 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.fabs" {
|
2018-06-29 16:44:54 -07:00
|
|
|
assert(fabs(f16(1.0)) == fabs16(1.0));
|
2017-06-07 19:56:57 -07:00
|
|
|
assert(fabs(f32(1.0)) == fabs32(1.0));
|
|
|
|
assert(fabs(f64(1.0)) == fabs64(1.0));
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:44:54 -07:00
|
|
|
test "math.fabs16" {
|
|
|
|
assert(fabs16(1.0) == 1.0);
|
|
|
|
assert(fabs16(-1.0) == 1.0);
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.fabs32" {
|
2017-06-26 11:41:47 -07:00
|
|
|
assert(fabs32(1.0) == 1.0);
|
|
|
|
assert(fabs32(-1.0) == 1.0);
|
2017-06-07 19:56:57 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.fabs64" {
|
2017-06-07 19:56:57 -07:00
|
|
|
assert(fabs64(1.0) == 1.0);
|
|
|
|
assert(fabs64(-1.0) == 1.0);
|
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
2018-06-29 16:44:54 -07:00
|
|
|
test "math.fabs16.special" {
|
|
|
|
assert(math.isPositiveInf(fabs(math.inf(f16))));
|
|
|
|
assert(math.isPositiveInf(fabs(-math.inf(f16))));
|
|
|
|
assert(math.isNan(fabs(math.nan(f16))));
|
|
|
|
}
|
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
test "math.fabs32.special" {
|
|
|
|
assert(math.isPositiveInf(fabs(math.inf(f32))));
|
|
|
|
assert(math.isPositiveInf(fabs(-math.inf(f32))));
|
|
|
|
assert(math.isNan(fabs(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.fabs64.special" {
|
|
|
|
assert(math.isPositiveInf(fabs(math.inf(f64))));
|
|
|
|
assert(math.isPositiveInf(fabs(-math.inf(f64))));
|
|
|
|
assert(math.isNan(fabs(math.nan(f64))));
|
|
|
|
}
|