2017-06-20 12:01:04 +01:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - ceil(+-0) = +-0
|
|
|
|
// - ceil(+-inf) = +-inf
|
|
|
|
// - ceil(nan) = nan
|
|
|
|
|
2017-06-16 09:26:10 +01:00
|
|
|
const builtin = @import("builtin");
|
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 ceil(x: var) @typeOf(x) {
|
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 => ceil32(x),
|
|
|
|
f64 => ceil64(x),
|
2017-06-16 09:26:10 +01:00
|
|
|
else => @compileError("ceil not implemented for " ++ @typeName(T)),
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn ceil32(x: f32) f32 {
|
2017-06-16 09:26:10 +01:00
|
|
|
var u = @bitCast(u32, x);
|
|
|
|
var e = i32((u >> 23) & 0xFF) - 0x7F;
|
|
|
|
var m: u32 = undefined;
|
|
|
|
|
2017-06-20 12:01:04 +01:00
|
|
|
// TODO: Shouldn't need this explicit check.
|
|
|
|
if (x == 0.0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-06-16 09:26:10 +01:00
|
|
|
if (e >= 23) {
|
|
|
|
return x;
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
} else if (e >= 0) {
|
|
|
|
m = u32(0x007FFFFF) >> u5(e);
|
2017-06-16 09:26:10 +01:00
|
|
|
if (u & m == 0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
math.forceEval(x + 0x1.0p120);
|
|
|
|
if (u >> 31 == 0) {
|
|
|
|
u += m;
|
|
|
|
}
|
|
|
|
u &= ~m;
|
2017-12-22 05:50:30 +00:00
|
|
|
return @bitCast(f32, u);
|
2017-06-16 09:26:10 +01:00
|
|
|
} else {
|
|
|
|
math.forceEval(x + 0x1.0p120);
|
|
|
|
if (u >> 31 != 0) {
|
|
|
|
return -0.0;
|
|
|
|
} else {
|
2017-12-22 05:50:30 +00:00
|
|
|
return 1.0;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn ceil64(x: f64) f64 {
|
2017-06-16 09:26:10 +01:00
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
var y: f64 = undefined;
|
|
|
|
|
|
|
|
if (e >= 0x3FF+52 or x == 0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 63 != 0) {
|
|
|
|
@setFloatMode(this, builtin.FloatMode.Strict);
|
|
|
|
y = x - math.f64_toint + math.f64_toint - x;
|
|
|
|
} else {
|
|
|
|
@setFloatMode(this, builtin.FloatMode.Strict);
|
|
|
|
y = x + math.f64_toint - math.f64_toint - x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e <= 0x3FF-1) {
|
|
|
|
math.forceEval(y);
|
|
|
|
if (u >> 63 != 0) {
|
2017-12-22 05:50:30 +00:00
|
|
|
return -0.0;
|
2017-06-16 09:26:10 +01:00
|
|
|
} else {
|
2017-12-22 05:50:30 +00:00
|
|
|
return 1.0;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
} else if (y < 0) {
|
2017-12-22 05:50:30 +00:00
|
|
|
return x + y + 1;
|
2017-06-16 09:26:10 +01:00
|
|
|
} else {
|
2017-12-22 05:50:30 +00:00
|
|
|
return x + y;
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ceil" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ceil(f32(0.0)) == ceil32(0.0));
|
|
|
|
assert(ceil(f64(0.0)) == ceil64(0.0));
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ceil32" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ceil32(1.3) == 2.0);
|
|
|
|
assert(ceil32(-1.3) == -1.0);
|
|
|
|
assert(ceil32(0.2) == 1.0);
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.ceil64" {
|
2017-06-16 09:26:10 +01:00
|
|
|
assert(ceil64(1.3) == 2.0);
|
|
|
|
assert(ceil64(-1.3) == -1.0);
|
|
|
|
assert(ceil64(0.2) == 1.0);
|
|
|
|
}
|
2017-06-20 12:01:04 +01:00
|
|
|
|
|
|
|
test "math.ceil32.special" {
|
|
|
|
assert(ceil32(0.0) == 0.0);
|
|
|
|
assert(ceil32(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(ceil32(math.inf(f32))));
|
|
|
|
assert(math.isNegativeInf(ceil32(-math.inf(f32))));
|
|
|
|
assert(math.isNan(ceil32(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.ceil64.special" {
|
|
|
|
assert(ceil64(0.0) == 0.0);
|
|
|
|
assert(ceil64(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(ceil64(math.inf(f64))));
|
|
|
|
assert(math.isNegativeInf(ceil64(-math.inf(f64))));
|
|
|
|
assert(math.isNan(ceil64(math.nan(f64))));
|
|
|
|
}
|