Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 5c16a97

Browse files
authored
Merge pull request #436 from tgross35/generic-ceil
Add `ceilf16` and `ceilf128`
2 parents f33d7f0 + 0cb4550 commit 5c16a97

File tree

18 files changed

+168
-102
lines changed

18 files changed

+168
-102
lines changed

libm/crates/libm-macros/src/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
99
FloatTy::F16,
1010
Signature { args: &[Ty::F16], returns: &[Ty::F16] },
1111
None,
12-
&["fabsf16", "sqrtf16", "truncf16"],
12+
&["ceilf16", "fabsf16", "sqrtf16", "truncf16"],
1313
),
1414
(
1515
// `fn(f32) -> f32`
@@ -40,7 +40,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
4040
FloatTy::F128,
4141
Signature { args: &[Ty::F128], returns: &[Ty::F128] },
4242
None,
43-
&["fabsf128", "sqrtf128", "truncf128"],
43+
&["ceilf128", "fabsf128", "sqrtf128", "truncf128"],
4444
),
4545
(
4646
// `(f16, f16) -> f16`

libm/crates/libm-test/benches/icount.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ main!(
6969
icount_bench_cbrt_group,
7070
icount_bench_cbrtf_group,
7171
icount_bench_ceil_group,
72+
icount_bench_ceilf128_group,
73+
icount_bench_ceilf16_group,
7274
icount_bench_ceilf_group,
7375
icount_bench_copysign_group,
7476
icount_bench_copysignf128_group,

libm/crates/libm-test/benches/random.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ libm_macros::for_each_function! {
117117
exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)),
118118

119119
// Musl does not provide `f16` and `f128` functions
120-
copysignf128
120+
ceilf128
121+
| ceilf16
122+
| copysignf128
121123
| copysignf16
122124
| fabsf128
123125
| fabsf16

libm/crates/libm-test/src/f8_impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ impl Float for f8 {
3030
const INFINITY: Self = Self(0b0_1111_000);
3131
const NEG_INFINITY: Self = Self(0b1_1111_000);
3232
const NAN: Self = Self(0b0_1111_100);
33+
// FIXME: incorrect values
34+
const EPSILON: Self = Self::ZERO;
3335
const PI: Self = Self::ZERO;
3436
const NEG_PI: Self = Self::ZERO;
3537
const FRAC_PI_2: Self = Self::ZERO;

libm/crates/libm-test/src/mpfloat.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ libm_macros::for_each_function! {
137137
// Most of these need a manual implementation
138138
ceil,
139139
ceilf,
140+
ceilf128,
141+
ceilf16,
140142
copysign,
141143
copysignf,
142144
copysignf128,
@@ -237,12 +239,14 @@ impl_no_round! {
237239
#[cfg(f16_enabled)]
238240
impl_no_round! {
239241
fabsf16 => abs_mut;
242+
ceilf16 => ceil_mut;
240243
truncf16 => trunc_mut;
241244
}
242245

243246
#[cfg(f128_enabled)]
244247
impl_no_round! {
245248
fabsf128 => abs_mut;
249+
ceilf128 => ceil_mut;
246250
truncf128 => trunc_mut;
247251
}
248252

libm/crates/libm-test/tests/compare_built_musl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ libm_macros::for_each_function! {
7979
ynf,
8080

8181
// Not provided by musl
82+
ceilf128,
83+
ceilf16,
8284
copysignf128,
8385
copysignf16,
8486
fabsf128,

libm/crates/util/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
8484
emit_types: [CFn, RustFn, RustArgs],
8585
extra: (basis, op, inputs),
8686
fn_extra: match MACRO_FN_NAME {
87-
copysignf128
87+
ceilf128
88+
| ceilf16
89+
| copysignf128
8890
| copysignf16
8991
| fabsf128
9092
| fabsf16

libm/etc/function-definitions.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,33 @@
109109
"src/libm_helper.rs",
110110
"src/math/arch/i586.rs",
111111
"src/math/arch/wasm32.rs",
112-
"src/math/ceil.rs"
112+
"src/math/ceil.rs",
113+
"src/math/generic/ceil.rs"
113114
],
114115
"type": "f64"
115116
},
116117
"ceilf": {
117118
"sources": [
118119
"src/math/arch/wasm32.rs",
119-
"src/math/ceilf.rs"
120+
"src/math/ceilf.rs",
121+
"src/math/generic/ceil.rs"
120122
],
121123
"type": "f32"
122124
},
125+
"ceilf128": {
126+
"sources": [
127+
"src/math/ceilf128.rs",
128+
"src/math/generic/ceil.rs"
129+
],
130+
"type": "f128"
131+
},
132+
"ceilf16": {
133+
"sources": [
134+
"src/math/ceilf16.rs",
135+
"src/math/generic/ceil.rs"
136+
],
137+
"type": "f16"
138+
},
123139
"copysign": {
124140
"sources": [
125141
"src/libm_helper.rs",

libm/etc/function-list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ cbrt
1717
cbrtf
1818
ceil
1919
ceilf
20+
ceilf128
21+
ceilf16
2022
copysign
2123
copysignf
2224
copysignf128

libm/src/math/ceil.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
#![allow(unreachable_code)]
2-
use core::f64;
3-
4-
const TOINT: f64 = 1. / f64::EPSILON;
5-
61
/// Ceil (f64)
72
///
83
/// Finds the nearest integer greater than or equal to `x`.
@@ -15,40 +10,5 @@ pub fn ceil(x: f64) -> f64 {
1510
args: x,
1611
}
1712

18-
let u: u64 = x.to_bits();
19-
let e: i64 = ((u >> 52) & 0x7ff) as i64;
20-
let y: f64;
21-
22-
if e >= 0x3ff + 52 || x == 0. {
23-
return x;
24-
}
25-
// y = int(x) - x, where int(x) is an integer neighbor of x
26-
y = if (u >> 63) != 0 { x - TOINT + TOINT - x } else { x + TOINT - TOINT - x };
27-
// special case because of non-nearest rounding modes
28-
if e < 0x3ff {
29-
force_eval!(y);
30-
return if (u >> 63) != 0 { -0. } else { 1. };
31-
}
32-
if y < 0. { x + y + 1. } else { x + y }
33-
}
34-
35-
#[cfg(test)]
36-
mod tests {
37-
use super::*;
38-
39-
#[test]
40-
fn sanity_check() {
41-
assert_eq!(ceil(1.1), 2.0);
42-
assert_eq!(ceil(2.9), 3.0);
43-
}
44-
45-
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil
46-
#[test]
47-
fn spec_tests() {
48-
// Not Asserted: that the current rounding mode has no effect.
49-
assert!(ceil(f64::NAN).is_nan());
50-
for f in [0.0, -0.0, f64::INFINITY, f64::NEG_INFINITY].iter().copied() {
51-
assert_eq!(ceil(f), f);
52-
}
53-
}
13+
super::generic::ceil(x)
5414
}

0 commit comments

Comments
 (0)