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

Commit 1617fc4

Browse files
committed
Update for f16 f128
1 parent 944db11 commit 1617fc4

File tree

11 files changed

+141
-30
lines changed

11 files changed

+141
-30
lines changed

crates/libm-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ publish = false
88
default = ["unstable-float"]
99

1010
# Propagated from libm because this affects which functions we test.
11-
unstable-float = ["libm/unstable-float"]
11+
unstable-float = ["libm/unstable-float", "rug?/nightly-float"]
1212

1313
# Generate tests which are random inputs and the outputs are calculated with
1414
# musl libc.

crates/libm-test/benches/random.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::time::Duration;
44
use criterion::{Criterion, criterion_main};
55
use libm_test::gen::random;
66
use libm_test::gen::random::RandomInput;
7-
use libm_test::{CheckBasis, CheckCtx, MathOp, TupleCall};
7+
use libm_test::{CheckBasis, CheckCtx, MathOp, OpCFn, TupleCall};
88

99
/// Benchmark with this many items to get a variety
1010
const BENCH_ITER_ITEMS: usize = if cfg!(feature = "short-benchmarks") { 50 } else { 500 };
@@ -20,23 +20,24 @@ macro_rules! musl_rand_benches {
2020
(
2121
fn_name: $fn_name:ident,
2222
attrs: [$($attr:meta),*],
23-
fn_extra: $skip_on_i586:expr,
23+
fn_extra: ($skip_on_i586:expr, $musl_fn:expr),
2424
) => {
2525
paste::paste! {
2626
$(#[$attr])*
2727
fn [< musl_bench_ $fn_name >](c: &mut Criterion) {
2828
type Op = libm_test::op::$fn_name::Routine;
29+
let musl_fn: Option<OpCFn<Op>> = $musl_fn;
2930

3031
#[cfg(feature = "build-musl")]
3132
let musl_extra = MuslExtra {
32-
musl_fn: Some(musl_math_sys::$fn_name as libm_test::OpCFn<Op>),
33-
skip_on_i586: $skip_on_i586
33+
musl_fn,
34+
skip_on_i586: $skip_on_i586,
3435
};
3536

3637
#[cfg(not(feature = "build-musl"))]
3738
let musl_extra = MuslExtra {
3839
musl_fn: None,
39-
skip_on_i586: $skip_on_i586
40+
skip_on_i586: $skip_on_i586,
4041
};
4142

4243
bench_one::<Op>(c, musl_extra);
@@ -67,7 +68,10 @@ where
6768
break;
6869
}
6970

70-
let musl_res = input.call(musl_extra.musl_fn.unwrap());
71+
let Some(musl_fn) = musl_extra.musl_fn else {
72+
continue;
73+
};
74+
let musl_res = input.call(musl_fn);
7175
let crate_res = input.call(Op::ROUTINE);
7276

7377
crate_res.validate(musl_res, input, &ctx).context(name).unwrap();
@@ -91,25 +95,30 @@ where
9195
// Don't test against musl if it is not available
9296
#[cfg(feature = "build-musl")]
9397
{
94-
let musl_fn = musl_extra.musl_fn.unwrap();
95-
group.bench_function("musl", |b| {
96-
b.iter(|| {
97-
let f = black_box(musl_fn);
98-
for input in benchvec.iter().copied() {
99-
input.call(f);
100-
}
101-
})
102-
});
98+
if let Some(musl_fn) = musl_extra.musl_fn {
99+
group.bench_function("musl", |b| {
100+
b.iter(|| {
101+
let f = black_box(musl_fn);
102+
for input in benchvec.iter().copied() {
103+
input.call(f);
104+
}
105+
})
106+
});
107+
}
103108
}
104109
}
105110

106111
libm_macros::for_each_function! {
107112
callback: musl_rand_benches,
108113
skip: [],
109114
fn_extra: match MACRO_FN_NAME {
110-
// FIXME(correctness): wrong result on i586
111-
exp10 | exp10f | exp2 | exp2f => true,
112-
_ => false
115+
// We pass a tuple of `(skip_on_i586, musl_fn)`. By default we never skip (false) and
116+
// we do pass a function, but there are a couple exceptions.
117+
// FIXME(correctness): exp functions have the wrong result on i586
118+
exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)),
119+
// Musl does not provide `f16` and `f128` functions
120+
copysignf16 | copysignf128 | fabsf16 | fabsf128 => (false, None),
121+
_ => (false, Some(musl_math_sys::MACRO_FN_NAME))
113122
}
114123
}
115124

crates/libm-test/src/domain.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,15 @@ impl HasDomain<f32> for crate::op::lgammaf_r::Routine {
187187
impl HasDomain<f64> for crate::op::lgamma_r::Routine {
188188
const DOMAIN: Domain<f64> = Domain::<f64>::LGAMMA;
189189
}
190+
191+
/* Not all `f16` and `f128` functions exist yet so we can't easily use the macros. */
192+
193+
#[cfg(f16_enabled)]
194+
impl HasDomain<f16> for crate::op::fabsf16::Routine {
195+
const DOMAIN: Domain<f16> = Domain::<f16>::UNBOUNDED;
196+
}
197+
198+
#[cfg(f128_enabled)]
199+
impl HasDomain<f128> for crate::op::fabsf128::Routine {
200+
const DOMAIN: Domain<f128> = Domain::<f128>::UNBOUNDED;
201+
}

crates/libm-test/src/gen/extensive.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ macro_rules! impl_extensive_input {
118118
};
119119
}
120120

121+
#[cfg(f16_enabled)]
122+
impl_extensive_input!(f16);
121123
impl_extensive_input!(f32);
122124
impl_extensive_input!(f64);
125+
#[cfg(f128_enabled)]
126+
impl_extensive_input!(f128);
123127

124128
/// Create a test case iterator for extensive inputs.
125129
pub fn get_test_cases<Op>(

crates/libm-test/src/gen/random.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ macro_rules! impl_random_input {
9494
};
9595
}
9696

97+
#[cfg(f16_enabled)]
98+
impl_random_input!(f16);
9799
impl_random_input!(f32);
98100
impl_random_input!(f64);
101+
#[cfg(f128_enabled)]
102+
impl_random_input!(f128);
99103

100104
/// Create a test case iterator.
101105
pub fn get_test_cases<RustArgs: RandomInput>(

crates/libm-test/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![cfg_attr(f16_enabled, feature(f16))]
22
#![cfg_attr(f128_enabled, feature(f128))]
33
#![allow(clippy::unusual_byte_groupings)] // sometimes we group by sign_exp_sig
4-
#![cfg_attr(f128_enabled, feature(f128))]
5-
#![cfg_attr(f16_enabled, feature(f16))]
64

75
pub mod domain;
86
mod f8_impl;

crates/libm-test/src/mpfloat.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ libm_macros::for_each_function! {
137137
fmod, fmodf, frexp, frexpf, ilogb, ilogbf, jn, jnf, ldexp, ldexpf,
138138
lgamma_r, lgammaf_r, modf, modff, nextafter, nextafterf, pow,powf,
139139
remquo, remquof, scalbn, scalbnf, sincos, sincosf, yn, ynf,
140+
copysignf16, copysignf128, fabsf16, fabsf128,
140141
],
141142
fn_extra: match MACRO_FN_NAME {
142143
// Remap function names that are different between mpfr and libm
@@ -157,10 +158,8 @@ libm_macros::for_each_function! {
157158
/// Implement unary functions that don't have a `_round` version
158159
macro_rules! impl_no_round {
159160
// Unary matcher
160-
($($fn_name:ident, $rug_name:ident;)*) => {
161+
($($fn_name:ident => $rug_name:ident;)*) => {
161162
paste::paste! {
162-
// Implement for both f32 and f64
163-
$( impl_no_round!{ @inner_unary [< $fn_name f >], $rug_name } )*
164163
$( impl_no_round!{ @inner_unary $fn_name, $rug_name } )*
165164
}
166165
};
@@ -183,12 +182,28 @@ macro_rules! impl_no_round {
183182
}
184183

185184
impl_no_round! {
186-
fabs, abs_mut;
187-
ceil, ceil_mut;
188-
floor, floor_mut;
189-
rint, round_even_mut; // FIXME: respect rounding mode
190-
round, round_mut;
191-
trunc, trunc_mut;
185+
ceil => ceil_mut;
186+
ceilf => ceil_mut;
187+
fabs => abs_mut;
188+
fabsf => abs_mut;
189+
floor => floor_mut;
190+
floorf => floor_mut;
191+
rint => round_even_mut; // FIXME: respect rounding mode
192+
rintf => round_even_mut; // FIXME: respect rounding mode
193+
round => round_mut;
194+
roundf => round_mut;
195+
trunc => trunc_mut;
196+
truncf => trunc_mut;
197+
}
198+
199+
#[cfg(f16_enabled)]
200+
impl_no_round! {
201+
fabsf16 => abs_mut;
202+
}
203+
204+
#[cfg(f128_enabled)]
205+
impl_no_round! {
206+
fabsf128 => abs_mut;
192207
}
193208

194209
/// Some functions are difficult to do in a generic way. Implement them here.
@@ -324,3 +339,37 @@ impl MpOp for crate::op::lgammaf_r::Routine {
324339
(ret, sign as i32)
325340
}
326341
}
342+
343+
/* Not all `f16` and `f128` functions exist yet so we can't easily use the macros. */
344+
345+
#[cfg(f16_enabled)]
346+
impl MpOp for crate::op::copysignf16::Routine {
347+
type MpTy = (MpFloat, MpFloat);
348+
349+
fn new_mp() -> Self::MpTy {
350+
(new_mpfloat::<f16>(), new_mpfloat::<f16>())
351+
}
352+
353+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
354+
this.0.assign(input.0);
355+
this.1.assign(input.1);
356+
this.0.copysign_mut(&this.1);
357+
prep_retval::<Self::RustRet>(&mut this.0, Ordering::Equal)
358+
}
359+
}
360+
361+
#[cfg(f128_enabled)]
362+
impl MpOp for crate::op::copysignf128::Routine {
363+
type MpTy = (MpFloat, MpFloat);
364+
365+
fn new_mp() -> Self::MpTy {
366+
(new_mpfloat::<f128>(), new_mpfloat::<f128>())
367+
}
368+
369+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
370+
this.0.assign(input.0);
371+
this.1.assign(input.1);
372+
this.0.copysign_mut(&this.1);
373+
prep_retval::<Self::RustRet>(&mut this.0, Ordering::Equal)
374+
}
375+
}

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ where
4646

4747
libm_macros::for_each_function! {
4848
callback: musl_rand_tests,
49+
skip: [copysignf16, copysignf128, fabsf16, fabsf128],
4950
attributes: [
5051
#[cfg_attr(x86_no_sse, ignore)] // FIXME(correctness): wrong result on i586
5152
[exp10, exp10f, exp2, exp2f, rint]

crates/libm-test/tests/multiprecision.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ libm_macros::for_each_function! {
130130
atan2f,
131131
copysign,
132132
copysignf,
133+
copysignf16,
134+
copysignf128,
133135
fdim,
134136
fdimf,
135137
fma,

etc/function-definitions.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@
136136
],
137137
"type": "f32"
138138
},
139+
"copysignf128": {
140+
"sources": [
141+
"src/math/copysignf128.rs",
142+
"src/math/generic/copysign.rs"
143+
],
144+
"type": "f128"
145+
},
146+
"copysignf16": {
147+
"sources": [
148+
"src/math/copysignf16.rs",
149+
"src/math/generic/copysign.rs"
150+
],
151+
"type": "f16"
152+
},
139153
"cos": {
140154
"sources": [
141155
"src/libm_helper.rs",
@@ -258,6 +272,20 @@
258272
],
259273
"type": "f32"
260274
},
275+
"fabsf128": {
276+
"sources": [
277+
"src/math/fabsf128.rs",
278+
"src/math/generic/fabs.rs"
279+
],
280+
"type": "f128"
281+
},
282+
"fabsf16": {
283+
"sources": [
284+
"src/math/fabsf16.rs",
285+
"src/math/generic/fabs.rs"
286+
],
287+
"type": "f16"
288+
},
261289
"fdim": {
262290
"sources": [
263291
"src/libm_helper.rs",

0 commit comments

Comments
 (0)