2019-04-30 23:15:57 -07:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
2017-06-20 04:01:04 -07:00
|
|
|
//
|
2019-04-30 23:15:57 -07:00
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/ilogbf.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/ilogb.c
|
2017-06-20 04:01:04 -07:00
|
|
|
|
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;
|
2018-10-26 11:59:58 -07:00
|
|
|
const maxInt = std.math.maxInt;
|
|
|
|
const minInt = std.math.minInt;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the binary exponent of x as an integer.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - ilogb(+-inf) = maxInt(i32)
|
|
|
|
/// - ilogb(0) = maxInt(i32)
|
|
|
|
/// - ilogb(nan) = maxInt(i32)
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn ilogb(x: var) i32 {
|
2019-12-09 12:56:19 -08: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 => ilogb32(x),
|
|
|
|
f64 => ilogb64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("ilogb not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 10:33:58 -08:00
|
|
|
// NOTE: Should these be exposed publicly?
|
2019-11-06 20:25:57 -08:00
|
|
|
const fp_ilogbnan = -1 - @as(i32, maxInt(u32) >> 1);
|
2017-06-16 01:26:10 -07:00
|
|
|
const fp_ilogb0 = fp_ilogbnan;
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn ilogb32(x: f32) i32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var u = @bitCast(u32, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
var e = @intCast(i32, (u >> 23) & 0xFF);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
// TODO: We should be able to merge this with the lower check.
|
|
|
|
if (math.isNan(x)) {
|
2018-10-26 11:59:58 -07:00
|
|
|
return maxInt(i32);
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
if (e == 0) {
|
|
|
|
u <<= 9;
|
|
|
|
if (u == 0) {
|
|
|
|
math.raiseInvalid();
|
|
|
|
return fp_ilogb0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// subnormal
|
|
|
|
e = -0x7F;
|
|
|
|
while (u >> 31 == 0) : (u <<= 1) {
|
|
|
|
e -= 1;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e == 0xFF) {
|
|
|
|
math.raiseInvalid();
|
2017-08-09 07:09:38 -07:00
|
|
|
if (u << 9 != 0) {
|
2017-06-16 01:26:10 -07:00
|
|
|
return fp_ilogbnan;
|
|
|
|
} else {
|
2018-10-26 11:59:58 -07:00
|
|
|
return maxInt(i32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return e - 0x7F;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn ilogb64(x: f64) i32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var u = @bitCast(u64, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
var e = @intCast(i32, (u >> 52) & 0x7FF);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
if (math.isNan(x)) {
|
2018-10-26 11:59:58 -07:00
|
|
|
return maxInt(i32);
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
if (e == 0) {
|
|
|
|
u <<= 12;
|
|
|
|
if (u == 0) {
|
|
|
|
math.raiseInvalid();
|
|
|
|
return fp_ilogb0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// subnormal
|
|
|
|
e = -0x3FF;
|
|
|
|
while (u >> 63 == 0) : (u <<= 1) {
|
|
|
|
e -= 1;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e == 0x7FF) {
|
|
|
|
math.raiseInvalid();
|
2017-08-09 07:09:38 -07:00
|
|
|
if (u << 12 != 0) {
|
2017-06-16 01:26:10 -07:00
|
|
|
return fp_ilogbnan;
|
|
|
|
} else {
|
2018-10-26 11:59:58 -07:00
|
|
|
return maxInt(i32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return e - 0x3FF;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ilogb" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(ilogb(@as(f32, 0.2)) == ilogb32(0.2));
|
|
|
|
expect(ilogb(@as(f64, 0.2)) == ilogb64(0.2));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ilogb32" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ilogb32(0.0) == fp_ilogb0);
|
|
|
|
expect(ilogb32(0.5) == -1);
|
|
|
|
expect(ilogb32(0.8923) == -1);
|
|
|
|
expect(ilogb32(10.0) == 3);
|
|
|
|
expect(ilogb32(-123984) == 16);
|
|
|
|
expect(ilogb32(2398.23) == 11);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ilogb64" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ilogb64(0.0) == fp_ilogb0);
|
|
|
|
expect(ilogb64(0.5) == -1);
|
|
|
|
expect(ilogb64(0.8923) == -1);
|
|
|
|
expect(ilogb64(10.0) == 3);
|
|
|
|
expect(ilogb64(-123984) == 16);
|
|
|
|
expect(ilogb64(2398.23) == 11);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.ilogb32.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ilogb32(math.inf(f32)) == maxInt(i32));
|
|
|
|
expect(ilogb32(-math.inf(f32)) == maxInt(i32));
|
|
|
|
expect(ilogb32(0.0) == minInt(i32));
|
|
|
|
expect(ilogb32(math.nan(f32)) == maxInt(i32));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.ilogb64.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ilogb64(math.inf(f64)) == maxInt(i32));
|
|
|
|
expect(ilogb64(-math.inf(f64)) == maxInt(i32));
|
|
|
|
expect(ilogb64(0.0) == minInt(i32));
|
|
|
|
expect(ilogb64(math.nan(f64)) == maxInt(i32));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|