Skip to content

Commit 857ba9c

Browse files
committed
Use remquo from Rug
Rug 1.27.0 exposes `remquo`; make use of it for our tests. Removing our workaround also allows removing the direct dependency on `gmp-mpfr-sys`
1 parent a6ba249 commit 857ba9c

File tree

3 files changed

+5
-67
lines changed

3 files changed

+5
-67
lines changed

crates/libm-test/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ short-benchmarks = []
2828

2929
[dependencies]
3030
anyhow = "1.0.95"
31-
gmp-mpfr-sys = { version = "1.6.4", optional = true, default-features = false, features = ["mpfr"] }
31+
# This is not directly used but is required so we can enable `gmp-mpfr-sys/force-cross`.
32+
gmp-mpfr-sys = { version = "1.6.4", optional = true, default-features = false }
3233
iai-callgrind = { version = "0.14.0", optional = true }
3334
indicatif = { version = "0.17.9", default-features = false }
3435
libm = { path = "../..", features = ["unstable-public-internals"] }

crates/libm-test/src/mpfloat.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
//! a struct named `Operation` that implements [`MpOp`].
55
66
use std::cmp::Ordering;
7-
use std::ffi::{c_int, c_long};
87

9-
use gmp_mpfr_sys::mpfr::rnd_t;
108
use rug::Assign;
119
pub use rug::Float as MpFloat;
1210
use rug::az::{self, Az};
13-
use rug::float::Round;
1411
use rug::float::Round::Nearest;
1512
use rug::ops::{PowAssignRound, RemAssignRound};
1613

@@ -401,28 +398,20 @@ macro_rules! impl_op_for_ty {
401398
}
402399

403400
impl MpOp for crate::op::[<remquo $suffix>]::Routine {
404-
type MpTy = (MpFloat, MpFloat, MpFloat);
401+
type MpTy = (MpFloat, MpFloat);
405402

406403
fn new_mp() -> Self::MpTy {
407404
(
408405
new_mpfloat::<Self::FTy>(),
409406
new_mpfloat::<Self::FTy>(),
410-
new_mpfloat::<Self::FTy>()
411407
)
412408
}
413409

414410
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
415411
this.0.assign(input.0);
416412
this.1.assign(input.1);
417-
let (ord, ql) = mpfr_remquo(&mut this.2, &this.0, &this.1, Nearest);
418-
419-
// `remquo` integer results are sign-magnitude representation. Transfer the
420-
// sign bit from the long result to the int result.
421-
let clear = !(1 << (c_int::BITS - 1));
422-
let sign = ((ql >> (c_long::BITS - 1)) as i32) << (c_int::BITS - 1);
423-
let q = (ql as i32) & clear | sign;
424-
425-
(prep_retval::<Self::FTy>(&mut this.2, ord), q)
413+
let (ord, q) = this.0.remainder_quo31_round(&this.1, Nearest);
414+
(prep_retval::<Self::FTy>(&mut this.0, ord), q)
426415
}
427416
}
428417

@@ -547,24 +536,3 @@ impl MpOp for crate::op::nextafterf::Routine {
547536
unimplemented!("nextafter does not yet have a MPFR operation");
548537
}
549538
}
550-
551-
/// `rug` does not provide `remquo` so this exposes `mpfr_remquo`. See rug#76.
552-
fn mpfr_remquo(r: &mut MpFloat, x: &MpFloat, y: &MpFloat, round: Round) -> (Ordering, c_long) {
553-
let r = r.as_raw_mut();
554-
let x = x.as_raw();
555-
let y = y.as_raw();
556-
let mut q: c_long = 0;
557-
558-
let round = match round {
559-
Round::Nearest => rnd_t::RNDN,
560-
Round::Zero => rnd_t::RNDZ,
561-
Round::Up => rnd_t::RNDU,
562-
Round::Down => rnd_t::RNDD,
563-
Round::AwayZero => rnd_t::RNDA,
564-
_ => unreachable!(),
565-
};
566-
567-
// SAFETY: mutable and const pointers are valid and do not alias, by Rust's rules.
568-
let ord = unsafe { gmp_mpfr_sys::mpfr::remquo(r, &mut q, x, y, round) };
569-
(ord.cmp(&0), q)
570-
}

crates/libm-test/src/precision.rs

-31
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,6 @@ impl MaybeOverride<(f32, f32)> for SpecialCase {
405405
) -> CheckAction {
406406
binop_common(input, actual, expected, ctx)
407407
}
408-
409-
fn check_int<I: Int>(
410-
_input: (f32, f32),
411-
actual: I,
412-
expected: I,
413-
ctx: &CheckCtx,
414-
) -> CheckAction {
415-
remquo_common(actual, expected, ctx)
416-
}
417408
}
418409

419410
impl MaybeOverride<(f64, f64)> for SpecialCase {
@@ -425,15 +416,6 @@ impl MaybeOverride<(f64, f64)> for SpecialCase {
425416
) -> CheckAction {
426417
binop_common(input, actual, expected, ctx)
427418
}
428-
429-
fn check_int<I: Int>(
430-
_input: (f64, f64),
431-
actual: I,
432-
expected: I,
433-
ctx: &CheckCtx,
434-
) -> CheckAction {
435-
remquo_common(actual, expected, ctx)
436-
}
437419
}
438420

439421
#[cfg(f128_enabled)]
@@ -496,19 +478,6 @@ fn binop_common<F1: Float, F2: Float>(
496478
DEFAULT
497479
}
498480

499-
fn remquo_common<I: Int>(actual: I, expected: I, ctx: &CheckCtx) -> CheckAction {
500-
// FIXME: Our MPFR implementation disagrees with musl and may need to be updated.
501-
if ctx.basis == CheckBasis::Mpfr
502-
&& ctx.base_name == BaseName::Remquo
503-
&& expected == I::MIN
504-
&& actual == I::ZERO
505-
{
506-
return XFAIL("remquo integer mismatch");
507-
}
508-
509-
DEFAULT
510-
}
511-
512481
impl MaybeOverride<(i32, f32)> for SpecialCase {
513482
fn check_float<F: Float>(
514483
input: (i32, f32),

0 commit comments

Comments
 (0)