diff options
| author | Andrea Orru <andrea@orru.io> | 2018-08-06 01:43:19 -0400 |
|---|---|---|
| committer | Andrea Orru <andrea@orru.io> | 2018-08-06 01:43:19 -0400 |
| commit | d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d (patch) | |
| tree | e9fa3caec533a0d1e2b434868b2fde1f9240e5c8 /std/math/floor.zig | |
| parent | 06614b3fa09954464c2e2f32756cacedc178a282 (diff) | |
| parent | 63a23e848a62d5f167f8d5478de9766cb24aa6eb (diff) | |
| download | zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.tar.gz zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.zip | |
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'std/math/floor.zig')
| -rw-r--r-- | std/math/floor.zig | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/std/math/floor.zig b/std/math/floor.zig index 1b8e2dfeed..0858598eea 100644 --- a/std/math/floor.zig +++ b/std/math/floor.zig @@ -12,15 +12,50 @@ const math = std.math; pub fn floor(x: var) @typeOf(x) { const T = @typeOf(x); return switch (T) { + f16 => floor16(x), f32 => floor32(x), f64 => floor64(x), else => @compileError("floor not implemented for " ++ @typeName(T)), }; } +fn floor16(x: f16) f16 { + var u = @bitCast(u16, x); + const e = @intCast(i16, (u >> 10) & 31) - 15; + var m: u16 = undefined; + + // TODO: Shouldn't need this explicit check. + if (x == 0.0) { + return x; + } + + if (e >= 10) { + return x; + } + + if (e >= 0) { + m = u16(1023) >> @intCast(u4, e); + if (u & m == 0) { + return x; + } + math.forceEval(x + 0x1.0p120); + if (u >> 15 != 0) { + u += m; + } + return @bitCast(f16, u & ~m); + } else { + math.forceEval(x + 0x1.0p120); + if (u >> 15 == 0) { + return 0.0; + } else { + return -1.0; + } + } +} + fn floor32(x: f32) f32 { var u = @bitCast(u32, x); - const e = i32((u >> 23) & 0xFF) - 0x7F; + const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F; var m: u32 = undefined; // TODO: Shouldn't need this explicit check. @@ -33,7 +68,7 @@ fn floor32(x: f32) f32 { } if (e >= 0) { - m = u32(0x007FFFFF) >> u5(e); + m = u32(0x007FFFFF) >> @intCast(u5, e); if (u & m == 0) { return x; } @@ -57,7 +92,7 @@ fn floor64(x: f64) f64 { const e = (u >> 52) & 0x7FF; var y: f64 = undefined; - if (e >= 0x3FF+52 or x == 0) { + if (e >= 0x3FF + 52 or x == 0) { return x; } @@ -69,7 +104,7 @@ fn floor64(x: f64) f64 { y = x + math.f64_toint - math.f64_toint - x; } - if (e <= 0x3FF-1) { + if (e <= 0x3FF - 1) { math.forceEval(y); if (u >> 63 != 0) { return -1.0; @@ -84,10 +119,17 @@ fn floor64(x: f64) f64 { } test "math.floor" { + assert(floor(f16(1.3)) == floor16(1.3)); assert(floor(f32(1.3)) == floor32(1.3)); assert(floor(f64(1.3)) == floor64(1.3)); } +test "math.floor16" { + assert(floor16(1.3) == 1.0); + assert(floor16(-1.3) == -2.0); + assert(floor16(0.2) == 0.0); +} + test "math.floor32" { assert(floor32(1.3) == 1.0); assert(floor32(-1.3) == -2.0); @@ -100,6 +142,14 @@ test "math.floor64" { assert(floor64(0.2) == 0.0); } +test "math.floor16.special" { + assert(floor16(0.0) == 0.0); + assert(floor16(-0.0) == -0.0); + assert(math.isPositiveInf(floor16(math.inf(f16)))); + assert(math.isNegativeInf(floor16(-math.inf(f16)))); + assert(math.isNan(floor16(math.nan(f16)))); +} + test "math.floor32.special" { assert(floor32(0.0) == 0.0); assert(floor32(-0.0) == -0.0); |
