2017-06-20 12:01:04 +01:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - ilogb(+-inf) = @maxValue(i32)
|
|
|
|
// - ilogb(0) = @maxValue(i32)
|
|
|
|
// - ilogb(nan) = @maxValue(i32)
|
|
|
|
|
2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-16 09:26:10 +01:00
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn ilogb(x: var) i32 {
|
2017-06-16 09:26:10 +01:00
|
|
|
const T = @typeOf(x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return switch (T) {
|
2017-12-22 18:14:07 +00:00
|
|
|
f32 => ilogb32(x),
|
|
|
|
f64 => ilogb64(x),
|
2017-06-16 09:26:10 +01:00
|
|
|
else => @compileError("ilogb not implemented for " ++ @typeName(T)),
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Should these be exposed publically?
|
|
|
|
const fp_ilogbnan = -1 - i32(@maxValue(u32) >> 1);
|
|
|
|
const fp_ilogb0 = fp_ilogbnan;
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn ilogb32(x: f32) i32 {
|
2017-06-16 09:26:10 +01:00
|
|
|
var u = @bitCast(u32, x);
|
|
|
|
var e = i32((u >> 23) & 0xFF);
|
|
|
|
|
2017-06-20 12:01:04 +01:00
|
|
|
// TODO: We should be able to merge this with the lower check.
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return @maxValue(i32);
|
|
|
|
}
|
|
|
|
|
2017-06-16 09:26:10 +01: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 15:09:38 +01:00
|
|
|
if (u << 9 != 0) {
|
2017-06-16 09:26:10 +01:00
|
|
|
return fp_ilogbnan;
|
|
|
|
} else {
|
|
|
|
return @maxValue(i32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:50:30 +00:00
|
|
|
return e - 0x7F;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn ilogb64(x: f64) i32 {
|
2017-06-16 09:26:10 +01:00
|
|
|
var u = @bitCast(u64, x);
|
|
|
|
var e = i32((u >> 52) & 0x7FF);
|
|
|
|
|
2017-06-20 12:01:04 +01:00
|
|
|
if (math.isNan(x)) {
|
|
|
|
return @maxValue(i32);
|
|
|
|
}
|
|
|
|
|
2017-06-16 09:26:10 +01: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 15:09:38 +01:00
|
|
|
if (u << 12 != 0) {
|
2017-06-16 09:26:10 +01:00
|
|
|
return fp_ilogbnan;
|
|
|
|
} else {
|
|
|
|
return @maxValue(i32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:50:30 +00:00
|
|
|
return e - 0x3FF;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ilogb" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ilogb(f32(0.2)) == ilogb32(0.2));
|
|
|
|
assert(ilogb(f64(0.2)) == ilogb64(0.2));
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ilogb32" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ilogb32(0.0) == fp_ilogb0);
|
|
|
|
assert(ilogb32(0.5) == -1);
|
|
|
|
assert(ilogb32(0.8923) == -1);
|
|
|
|
assert(ilogb32(10.0) == 3);
|
|
|
|
assert(ilogb32(-123984) == 16);
|
|
|
|
assert(ilogb32(2398.23) == 11);
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ilogb64" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ilogb64(0.0) == fp_ilogb0);
|
|
|
|
assert(ilogb64(0.5) == -1);
|
|
|
|
assert(ilogb64(0.8923) == -1);
|
|
|
|
assert(ilogb64(10.0) == 3);
|
|
|
|
assert(ilogb64(-123984) == 16);
|
|
|
|
assert(ilogb64(2398.23) == 11);
|
|
|
|
}
|
2017-06-20 12:01:04 +01:00
|
|
|
|
|
|
|
test "math.ilogb32.special" {
|
|
|
|
assert(ilogb32(math.inf(f32)) == @maxValue(i32));
|
|
|
|
assert(ilogb32(-math.inf(f32)) == @maxValue(i32));
|
|
|
|
assert(ilogb32(0.0) == @minValue(i32));
|
|
|
|
assert(ilogb32(math.nan(f32)) == @maxValue(i32));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.ilogb64.special" {
|
|
|
|
assert(ilogb64(math.inf(f64)) == @maxValue(i32));
|
|
|
|
assert(ilogb64(-math.inf(f64)) == @maxValue(i32));
|
|
|
|
assert(ilogb64(0.0) == @minValue(i32));
|
|
|
|
assert(ilogb64(math.nan(f64)) == @maxValue(i32));
|
|
|
|
}
|