Skip to content

Commit

Permalink
Add ceilf16 and ceilf128
Browse files Browse the repository at this point in the history
Use the generic algorithms to provide implementations for these
routines.
  • Loading branch information
tgross35 committed Jan 22, 2025
1 parent eeb0d8e commit b25aa4b
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 6 deletions.
44 changes: 44 additions & 0 deletions br-ceil.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.globl libm::math::ceil::ceil
.p2align 2
libm::math::ceil::ceil:
Lfunc_begin17:
.cfi_startproc
sub sp, sp, #16
.cfi_def_cfa_offset 16
fcmp d0, #0.0
b.eq LBB17_5
fmov x8, d0
ubfx x9, x8, #52, #11
cmp w9, #1074
b.hi LBB17_5
mov x10, #-4382002437431492608
fmov d1, x10
fadd d2, d0, d1
mov x10, #4841369599423283200
fmov d3, x10
fadd d2, d2, d3
fadd d3, d0, d3
fadd d1, d3, d1
cmp x8, #0
fcsel d1, d2, d1, lt
fsub d1, d1, d0
str d1, [sp, #8]
cmp w9, #1023
b.hs LBB17_4
ldr d0, [sp, #8]
cmp x8, #0
mov x8, #-9223372036854775808
fmov d0, x8
fmov d1, #1.00000000
fcsel d0, d0, d1, lt
b LBB17_5
LBB17_4:
fadd d0, d1, d0
fmov d2, #1.00000000
fadd d2, d0, d2
fcmp d1, #0.0
fcsel d0, d2, d0, mi
LBB17_5:
add sp, sp, #16
.cfi_def_cfa_offset 0
ret
41 changes: 41 additions & 0 deletions br-ceil1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.globl libm::math::ceilf::ceilf
.p2align 2
libm::math::ceilf::ceilf:
Lfunc_begin18:
.cfi_startproc
sub sp, sp, #16
.cfi_def_cfa_offset 16
fcmp s0, #0.0
b.eq LBB18_5
fmov w8, s0
ubfx w9, w8, #23, #8
cmp w9, #149
b.hi LBB18_5
movi.2s v1, #203, lsl #24
fadd s2, s0, s1
movi.2s v3, #75, lsl #24
fadd s2, s2, s3
fadd s3, s0, s3
fadd s1, s3, s1
cmp w8, #0
fcsel s1, s2, s1, lt
fsub s1, s1, s0
str s1, [sp, #12]
cmp w9, #127
b.hs LBB18_4
ldr s0, [sp, #12]
cmp w8, #0
movi.2s v0, #128, lsl #24
fmov s1, #1.00000000
fcsel s0, s0, s1, lt
b LBB18_5
LBB18_4:
fadd s0, s1, s0
fmov s2, #1.00000000
fadd s2, s0, s2
fcmp s1, #0.0
fcsel s0, s2, s0, mi
LBB18_5:
add sp, sp, #16
.cfi_def_cfa_offset 0
ret
4 changes: 2 additions & 2 deletions crates/libm-macros/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
FloatTy::F16,
Signature { args: &[Ty::F16], returns: &[Ty::F16] },
None,
&["fabsf16", "sqrtf16", "truncf16"],
&["ceilf16", "fabsf16", "sqrtf16", "truncf16"],
),
(
// `fn(f32) -> f32`
Expand Down Expand Up @@ -40,7 +40,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
FloatTy::F128,
Signature { args: &[Ty::F128], returns: &[Ty::F128] },
None,
&["fabsf128", "sqrtf128", "truncf128"],
&["ceilf128", "fabsf128", "sqrtf128", "truncf128"],
),
(
// `(f16, f16) -> f16`
Expand Down
4 changes: 3 additions & 1 deletion crates/libm-test/benches/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ libm_macros::for_each_function! {
exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)),

// Musl does not provide `f16` and `f128` functions
copysignf128
ceilf128
| ceilf16
| copysignf128
| copysignf16
| fabsf128
| fabsf16
Expand Down
4 changes: 4 additions & 0 deletions crates/libm-test/src/mpfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ libm_macros::for_each_function! {
// Most of these need a manual implementation
ceil,
ceilf,
ceilf128,
ceilf16,
copysign,
copysignf,
copysignf128,
Expand Down Expand Up @@ -237,12 +239,14 @@ impl_no_round! {
#[cfg(f16_enabled)]
impl_no_round! {
fabsf16 => abs_mut;
ceilf16 => ceil_mut;
truncf16 => trunc_mut;
}

#[cfg(f128_enabled)]
impl_no_round! {
fabsf128 => abs_mut;
ceilf128 => ceil_mut;
truncf128 => trunc_mut;
}

Expand Down
2 changes: 2 additions & 0 deletions crates/libm-test/tests/compare_built_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ libm_macros::for_each_function! {
ynf,

// Not provided by musl
ceilf128,
ceilf16,
copysignf128,
copysignf16,
fabsf128,
Expand Down
4 changes: 3 additions & 1 deletion crates/util/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
emit_types: [CFn, RustFn, RustArgs],
extra: (basis, op, inputs),
fn_extra: match MACRO_FN_NAME {
copysignf128
ceilf128
| ceilf16
| copysignf128
| copysignf16
| fabsf128
| fabsf16
Expand Down
20 changes: 18 additions & 2 deletions etc/function-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,33 @@
"src/libm_helper.rs",
"src/math/arch/i586.rs",
"src/math/arch/wasm32.rs",
"src/math/ceil.rs"
"src/math/ceil.rs",
"src/math/generic/ceil.rs"
],
"type": "f64"
},
"ceilf": {
"sources": [
"src/math/arch/wasm32.rs",
"src/math/ceilf.rs"
"src/math/ceilf.rs",
"src/math/generic/ceil.rs"
],
"type": "f32"
},
"ceilf128": {
"sources": [
"src/math/ceilf128.rs",
"src/math/generic/ceil.rs"
],
"type": "f128"
},
"ceilf16": {
"sources": [
"src/math/ceilf16.rs",
"src/math/generic/ceil.rs"
],
"type": "f16"
},
"copysign": {
"sources": [
"src/libm_helper.rs",
Expand Down
2 changes: 2 additions & 0 deletions etc/function-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ cbrt
cbrtf
ceil
ceilf
ceilf128
ceilf16
copysign
copysignf
copysignf128
Expand Down
52 changes: 52 additions & 0 deletions m-ceil.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.globl libm::math::ceil::ceil
.p2align 2
libm::math::ceil::ceil:
Lfunc_begin17:
.cfi_startproc
sub sp, sp, #16
.cfi_def_cfa_offset 16
fcmp d0, #0.0
b.eq LBB17_8
fmov x9, d0
ubfx x8, x9, #52, #11
cmp x8, #1074
b.hi LBB17_8
tbnz x9, #63, LBB17_5
mov x9, #4841369599423283200
fmov d1, x9
fadd d1, d0, d1
mov x9, #-4382002437431492608
fmov d2, x9
fadd d1, d1, d2
fsub d1, d1, d0
str d1, [sp, #8]
cmp x8, #1023
b.hs LBB17_7
ldr d0, [sp, #8]
fmov d0, #1.00000000
b LBB17_8
LBB17_5:
mov x9, #-4382002437431492608
fmov d1, x9
fadd d1, d0, d1
mov x9, #4841369599423283200
fmov d2, x9
fadd d1, d1, d2
fsub d1, d1, d0
str d1, [sp, #8]
cmp x8, #1023
b.hs LBB17_7
ldr d0, [sp, #8]
mov x8, #-9223372036854775808
fmov d0, x8
b LBB17_8
LBB17_7:
fadd d0, d1, d0
fmov d2, #1.00000000
fadd d2, d0, d2
fcmp d1, #0.0
fcsel d0, d2, d0, mi
LBB17_8:
add sp, sp, #16
.cfi_def_cfa_offset 0
ret
49 changes: 49 additions & 0 deletions m-ceil1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.globl libm::math::ceilf::ceilf
.p2align 2
libm::math::ceilf::ceilf:
Lfunc_begin18:
.cfi_startproc
sub sp, sp, #16
.cfi_def_cfa_offset 16
fmov w8, s0
ubfx w9, w8, #23, #8
cmp w9, #149
b.hi LBB18_5
cmp w9, #126
b.ls LBB18_4
lsr w9, w8, #23
add w10, w9, #1
mov w9, #8388607
lsr w9, w9, w10
tst w9, w8
b.eq LBB18_5
and w10, w10, #0x1f
mov w11, #2071986176
fmov s1, w11
fadd s0, s0, s1
str s0, [sp, #8]
ldr s0, [sp, #8]
cmp w8, #0
csel w9, wzr, w9, lt
mov w11, #-8388608
asr w10, w11, w10
add w8, w9, w8
and w8, w8, w10
fmov s0, w8
b LBB18_5
LBB18_4:
mov w9, #2071986176
fmov s1, w9
fadd s1, s0, s1
str s1, [sp, #12]
ldr s1, [sp, #12]
movi.2s v1, #128, lsl #24
cmp w8, #0
fmov s2, #1.00000000
fcsel s0, s0, s2, eq
tst w8, #0x80000000
fcsel s0, s0, s1, eq
LBB18_5:
add sp, sp, #16
.cfi_def_cfa_offset 0
ret
7 changes: 7 additions & 0 deletions src/math/ceilf128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Ceil (f128)
///
/// Finds the nearest integer greater than or equal to `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn ceilf128(x: f128) -> f128 {
super::generic::ceil(x)
}
7 changes: 7 additions & 0 deletions src/math/ceilf16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Ceil (f16)
///
/// Finds the nearest integer greater than or equal to `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn ceilf16(x: f16) -> f16 {
super::generic::ceil(x)
}
14 changes: 14 additions & 0 deletions src/math/generic/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ mod tests {
}
}

/* Skipping f16 / f128 "sanity_check"s due to rejected literal lexing at MSRV */

#[test]
#[cfg(f16_enabled)]
fn spec_tests_f16() {
spec_test::<f16>();
}

#[test]
fn sanity_check_f32() {
assert_eq!(ceil(1.1f32), 2.0);
Expand All @@ -84,4 +92,10 @@ mod tests {
fn spec_tests_f64() {
spec_test::<f64>();
}

#[test]
#[cfg(f128_enabled)]
fn spec_tests_f128() {
spec_test::<f128>();
}
}
4 changes: 4 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,14 @@ pub use self::truncf::truncf;

cfg_if! {
if #[cfg(f16_enabled)] {
mod ceilf16;
mod copysignf16;
mod fabsf16;
mod fdimf16;
mod sqrtf16;
mod truncf16;

pub use self::ceilf16::ceilf16;
pub use self::copysignf16::copysignf16;
pub use self::fabsf16::fabsf16;
pub use self::fdimf16::fdimf16;
Expand All @@ -357,12 +359,14 @@ cfg_if! {

cfg_if! {
if #[cfg(f128_enabled)] {
mod ceilf128;
mod copysignf128;
mod fabsf128;
mod fdimf128;
mod sqrtf128;
mod truncf128;

pub use self::ceilf128::ceilf128;
pub use self::copysignf128::copysignf128;
pub use self::fabsf128::fabsf128;
pub use self::fdimf128::fdimf128;
Expand Down
Loading

0 comments on commit b25aa4b

Please sign in to comment.