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/ceilf.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/ceil.c
|
2017-06-20 04:01:04 -07:00
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
const builtin = @import("builtin");
|
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;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the least integer value greater than of equal to x.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - ceil(+-0) = +-0
|
|
|
|
/// - ceil(+-inf) = +-inf
|
|
|
|
/// - ceil(nan) = nan
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn ceil(x: var) @TypeOf(x) {
|
|
|
|
const T = @TypeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => ceil32(x),
|
|
|
|
f64 => ceil64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("ceil not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn ceil32(x: f32) f32 {
|
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) - 0x7F;
|
2017-06-16 01:26:10 -07:00
|
|
|
var m: u32 = undefined;
|
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
// TODO: Shouldn't need this explicit check.
|
|
|
|
if (x == 0.0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07: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-18 22:32:15 -07:00
|
|
|
} else if (e >= 0) {
|
2019-11-06 20:25:57 -08:00
|
|
|
m = @as(u32, 0x007FFFFF) >> @intCast(u5, e);
|
2017-06-16 01:26:10 -07:00
|
|
|
if (u & m == 0) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
math.forceEval(x + 0x1.0p120);
|
|
|
|
if (u >> 31 == 0) {
|
|
|
|
u += m;
|
|
|
|
}
|
|
|
|
u &= ~m;
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f32, u);
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
|
|
|
math.forceEval(x + 0x1.0p120);
|
|
|
|
if (u >> 31 != 0) {
|
|
|
|
return -0.0;
|
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return 1.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn ceil64(x: f64) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
var y: f64 = undefined;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e >= 0x3FF + 52 or x == 0) {
|
2017-06-16 01:26:10 -07:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 63 != 0) {
|
|
|
|
y = x - math.f64_toint + math.f64_toint - x;
|
|
|
|
} else {
|
|
|
|
y = x + math.f64_toint - math.f64_toint - x;
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e <= 0x3FF - 1) {
|
2017-06-16 01:26:10 -07:00
|
|
|
math.forceEval(y);
|
|
|
|
if (u >> 63 != 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return -0.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return 1.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
} else if (y < 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return x + y + 1;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return x + y;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ceil" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(ceil(@as(f32, 0.0)) == ceil32(0.0));
|
|
|
|
expect(ceil(@as(f64, 0.0)) == ceil64(0.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ceil32" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ceil32(1.3) == 2.0);
|
|
|
|
expect(ceil32(-1.3) == -1.0);
|
|
|
|
expect(ceil32(0.2) == 1.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.ceil64" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ceil64(1.3) == 2.0);
|
|
|
|
expect(ceil64(-1.3) == -1.0);
|
|
|
|
expect(ceil64(0.2) == 1.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.ceil32.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ceil32(0.0) == 0.0);
|
|
|
|
expect(ceil32(-0.0) == -0.0);
|
|
|
|
expect(math.isPositiveInf(ceil32(math.inf(f32))));
|
|
|
|
expect(math.isNegativeInf(ceil32(-math.inf(f32))));
|
|
|
|
expect(math.isNan(ceil32(math.nan(f32))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.ceil64.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(ceil64(0.0) == 0.0);
|
|
|
|
expect(ceil64(-0.0) == -0.0);
|
|
|
|
expect(math.isPositiveInf(ceil64(math.inf(f64))));
|
|
|
|
expect(math.isNegativeInf(ceil64(-math.inf(f64))));
|
|
|
|
expect(math.isNan(ceil64(math.nan(f64))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|