zig/std/math/ilogb.zig
Marc Tiehuis 4c16f9a3c3 Add math library
This covers the majority of the functions as covered by the C99
specification for a math library.

Code is adapted primarily from musl libc, with the pow and standard
trigonometric functions adapted from the Go stdlib.

Changes:

 - Remove assert expose in index and import as needed.
 - Add float log function and merge with existing base 2 integer
   implementation.

See https://github.com/tiehuis/zig-fmath.
See #374.
2017-06-16 20:32:31 +12:00

101 lines
2.1 KiB
Zig

const math = @import("index.zig");
const assert = @import("../debug.zig").assert;
pub fn ilogb(x: var) -> i32 {
const T = @typeOf(x);
switch (T) {
f32 => @inlineCall(ilogb32, x),
f64 => @inlineCall(ilogb64, x),
else => @compileError("ilogb not implemented for " ++ @typeName(T)),
}
}
// NOTE: Should these be exposed publically?
const fp_ilogbnan = -1 - i32(@maxValue(u32) >> 1);
const fp_ilogb0 = fp_ilogbnan;
fn ilogb32(x: f32) -> i32 {
var u = @bitCast(u32, x);
var e = i32((u >> 23) & 0xFF);
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();
if (u << 9 != 0) {
return fp_ilogbnan;
} else {
return @maxValue(i32);
}
}
e - 0x7F
}
fn ilogb64(x: f64) -> i32 {
var u = @bitCast(u64, x);
var e = i32((u >> 52) & 0x7FF);
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();
if (u << 12 != 0) {
return fp_ilogbnan;
} else {
return @maxValue(i32);
}
}
e - 0x3FF
}
test "ilogb" {
assert(ilogb(f32(0.2)) == ilogb32(0.2));
assert(ilogb(f64(0.2)) == ilogb64(0.2));
}
test "ilogb32" {
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);
}
test "ilogb64" {
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);
}