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

Commit 5c3a925

Browse files
committed
mpfr tests pass locally
1 parent 677b577 commit 5c3a925

File tree

7 files changed

+99
-11
lines changed

7 files changed

+99
-11
lines changed

crates/libm-test/Cargo.toml

Lines changed: 2 additions & 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.
@@ -21,6 +21,7 @@ build-musl = ["dep:musl-math-sys"]
2121
[dependencies]
2222
anyhow = "1.0.90"
2323
az = { version = "1.2.1", optional = true }
24+
cfg-if = "1.0.0"
2425
libm = { path = "../.." }
2526
libm-macros = { path = "../libm-macros" }
2627
musl-math-sys = { path = "../musl-math-sys", optional = true }

crates/libm-test/src/mpfloat.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ libm_macros::for_each_function! {
166166
// Most of these need a manual implementation
167167
fabs, ceil, copysign, floor, rint, round, trunc,
168168
fabsf, ceilf, copysignf, floorf, rintf, roundf, truncf,
169+
fabsf16, fabsf128, copysignf16, copysignf128,
169170
fmod, fmodf, frexp, frexpf, ilogb, ilogbf, jn, jnf, ldexp, ldexpf,
170171
lgamma_r, lgammaf_r, modf, modff, nextafter, nextafterf, pow,powf,
171172
remquo, remquof, scalbn, scalbnf, sincos, sincosf,
@@ -192,8 +193,10 @@ macro_rules! impl_no_round {
192193
($($fn_name:ident, $rug_name:ident;)*) => {
193194
paste::paste! {
194195
// Implement for both f32 and f64
196+
$( impl_no_round!{ @inner_unary [< $fn_name f16 >], (f16,), $rug_name } )*
195197
$( impl_no_round!{ @inner_unary [< $fn_name f >], (f32,), $rug_name } )*
196198
$( impl_no_round!{ @inner_unary $fn_name, (f64,), $rug_name } )*
199+
$( impl_no_round!{ @inner_unary [< $fn_name f128 >], (f128,), $rug_name } )*
197200
}
198201
};
199202

@@ -372,3 +375,45 @@ impl_op_for_ty!(f64, "");
372375
pub mod lgammaf_r {
373376
pub use super::lgamma_rf::*;
374377
}
378+
379+
pub mod copysignf16 {
380+
use super::*;
381+
pub struct Operation(MpFloat, MpFloat);
382+
383+
impl MpOp for Operation {
384+
type Input = (f16, f16);
385+
type Output = f16;
386+
387+
fn new() -> Self {
388+
Self(new_mpfloat::<f16>(), new_mpfloat::<f16>())
389+
}
390+
391+
fn run(&mut self, input: Self::Input) -> Self::Output {
392+
self.0.assign(input.0);
393+
self.1.assign(input.1);
394+
self.0.copysign_mut(&self.1);
395+
prep_retval::<Self::Output>(&mut self.0, Ordering::Equal)
396+
}
397+
}
398+
}
399+
400+
pub mod copysignf128 {
401+
use super::*;
402+
pub struct Operation(MpFloat, MpFloat);
403+
404+
impl MpOp for Operation {
405+
type Input = (f128, f128);
406+
type Output = f128;
407+
408+
fn new() -> Self {
409+
Self(new_mpfloat::<f128>(), new_mpfloat::<f128>())
410+
}
411+
412+
fn run(&mut self, input: Self::Input) -> Self::Output {
413+
self.0.assign(input.0);
414+
self.1.assign(input.1);
415+
self.0.copysign_mut(&self.1);
416+
prep_retval::<Self::Output>(&mut self.0, Ordering::Equal)
417+
}
418+
}
419+
}

crates/libm-test/src/num_traits.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use crate::{MaybeOverride, SpecialCase, TestResult};
44

55
/// Common types and methods for floating point numbers.
6-
pub trait Float: Copy + fmt::Display + fmt::Debug + PartialEq<Self> {
6+
pub trait Float: Copy + fmt::Debug + fmt::Debug + PartialEq<Self> {
77
type Int: Int<OtherSign = Self::SignedInt, Unsigned = Self::Int>;
88
type SignedInt: Int + Int<OtherSign = Self::Int, Unsigned = Self::Int>;
99

@@ -70,6 +70,12 @@ impl_float!(
7070
f64, u64, i64, 52;
7171
);
7272

73+
#[cfg(f16_enabled)]
74+
impl_float!( f16, u16, i16, 10; );
75+
76+
#[cfg(f128_enabled)]
77+
impl_float!( f128, u128, i128, 112; );
78+
7379
/// Common types and methods for integers.
7480
pub trait Int: Copy + fmt::Display + fmt::Debug + PartialEq<Self> {
7581
type OtherSign: Int;
@@ -171,8 +177,10 @@ macro_rules! impl_int {
171177
}
172178

173179
impl_int!(
180+
u16, i16;
174181
u32, i32;
175182
u64, i64;
183+
u128, i128;
176184
);
177185

178186
/// A helper trait to print something as hex with the correct number of nibbles, e.g. a `u32`

crates/libm-test/src/precision.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ pub trait MaybeOverride<Input> {
102102
}
103103
}
104104

105+
#[cfg(f16_enabled)]
106+
impl MaybeOverride<(f16, f16)> for SpecialCase {
107+
fn check_float<F: Float>(
108+
input: (f16, f16),
109+
_actual: F,
110+
expected: F,
111+
_ulp: &mut u32,
112+
ctx: &CheckCtx,
113+
) -> Option<TestResult> {
114+
maybe_skip_binop_nan(input, expected, ctx)
115+
}
116+
}
117+
105118
impl MaybeOverride<(f32,)> for SpecialCase {
106119
fn check_float<F: Float>(
107120
input: (f32,),
@@ -115,12 +128,6 @@ impl MaybeOverride<(f32,)> for SpecialCase {
115128
// we return infinity but the number is representable
116129
return XFAIL;
117130
}
118-
119-
if ctx.fname == "sinhf" && input.0.abs() > 80.0 && actual.is_nan() {
120-
// we return some NaN that should be real values or infinite
121-
// doesn't seem to happen on x86
122-
return XFAIL;
123-
}
124131
}
125132

126133
if ctx.fname == "acoshf" && input.0 < -1.0 {
@@ -134,6 +141,12 @@ impl MaybeOverride<(f32,)> for SpecialCase {
134141
return XFAIL;
135142
}
136143

144+
if ctx.fname == "sinhf" && input.0.abs() > 80.0 && actual.is_nan() {
145+
// we return some NaN that should be real values or infinite
146+
// doesn't seem to happen on x86
147+
return XFAIL;
148+
}
149+
137150
maybe_check_nan_bits(actual, expected, ctx)
138151
}
139152

@@ -367,7 +380,25 @@ fn bessel_prec_dropoff<F: Float>(
367380
None
368381
}
369382

383+
#[cfg(f128_enabled)]
384+
impl MaybeOverride<(f128, f128)> for SpecialCase {
385+
fn check_float<F: Float>(
386+
input: (f128, f128),
387+
_actual: F,
388+
expected: F,
389+
_ulp: &mut u32,
390+
ctx: &CheckCtx,
391+
) -> Option<TestResult> {
392+
maybe_skip_binop_nan(input, expected, ctx)
393+
}
394+
}
395+
370396
impl MaybeOverride<(f32, f32, f32)> for SpecialCase {}
371397
impl MaybeOverride<(f64, f64, f64)> for SpecialCase {}
372398
impl MaybeOverride<(f32, i32)> for SpecialCase {}
373399
impl MaybeOverride<(f64, i32)> for SpecialCase {}
400+
401+
#[cfg(f16_enabled)]
402+
impl MaybeOverride<(f16,)> for SpecialCase {}
403+
#[cfg(f128_enabled)]
404+
impl MaybeOverride<(f128,)> for SpecialCase {}

crates/libm-test/src/test_traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ where
175175
// Make sure that the signs are the same before checing ULP to avoid wraparound
176176
let act_sig = self.signum();
177177
let exp_sig = expected.signum();
178-
ensure!(act_sig == exp_sig, "mismatched signs {act_sig} {exp_sig}");
178+
ensure!(act_sig == exp_sig, "mismatched signs {act_sig:?} {exp_sig:?}");
179179

180180
if self.is_infinite() ^ expected.is_infinite() {
181181
bail!("mismatched infinities");

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ macro_rules! musl_rand_tests {
4444

4545
libm_macros::for_each_function! {
4646
callback: musl_rand_tests,
47-
skip: [],
47+
// Musl has no implementations
48+
skip: [copysignf16, copysignf128, fabsf16, fabsf128],
4849
attributes: [
4950
#[cfg_attr(x86_no_sse, ignore)] // FIXME(correctness): wrong result on i586
5051
[exp10, exp10f, exp2, exp2f, rint]

crates/libm-test/tests/multiprecision.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Test with "infinite precision"
22
33
#![cfg(feature = "test-multiprecision")]
4+
#![cfg_attr(f128_enabled, feature(f128))]
5+
#![cfg_attr(f16_enabled, feature(f16))]
46

57
use libm_test::gen::random;
68
use libm_test::mpfloat::{self, MpOp};
@@ -16,7 +18,7 @@ macro_rules! multiprec_rand_tests {
1618
RustFn: $RustFn:ty,
1719
RustArgs: $RustArgs:ty,
1820
RustRet: $RustRet:ty,
19-
attrs: [$($meta:meta),*]
21+
attrs: [$($meta:meta),*],
2022
) => {
2123
paste::paste! {
2224
#[test]

0 commit comments

Comments
 (0)