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

Commit 39d645d

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 5f3fdcd commit 39d645d

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

@@ -410,28 +407,20 @@ macro_rules! impl_op_for_ty {
410407
}
411408

412409
impl MpOp for crate::op::[<remquo $suffix>]::Routine {
413-
type MpTy = (MpFloat, MpFloat, MpFloat);
410+
type MpTy = (MpFloat, MpFloat);
414411

415412
fn new_mp() -> Self::MpTy {
416413
(
417414
new_mpfloat::<Self::FTy>(),
418415
new_mpfloat::<Self::FTy>(),
419-
new_mpfloat::<Self::FTy>()
420416
)
421417
}
422418

423419
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
424420
this.0.assign(input.0);
425421
this.1.assign(input.1);
426-
let (ord, ql) = mpfr_remquo(&mut this.2, &this.0, &this.1, Nearest);
427-
428-
// `remquo` integer results are sign-magnitude representation. Transfer the
429-
// sign bit from the long result to the int result.
430-
let clear = !(1 << (c_int::BITS - 1));
431-
let sign = ((ql >> (c_long::BITS - 1)) as i32) << (c_int::BITS - 1);
432-
let q = (ql as i32) & clear | sign;
433-
434-
(prep_retval::<Self::FTy>(&mut this.2, ord), q)
422+
let (ord, q) = this.0.remainder_quo31_round(&this.1, Nearest);
423+
(prep_retval::<Self::FTy>(&mut this.0, ord), q)
435424
}
436425
}
437426

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

crates/libm-test/src/precision.rs

-31
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,6 @@ impl MaybeOverride<(f32, f32)> for SpecialCase {
408408
) -> CheckAction {
409409
binop_common(input, actual, expected, ctx)
410410
}
411-
412-
fn check_int<I: Int>(
413-
_input: (f32, f32),
414-
actual: I,
415-
expected: I,
416-
ctx: &CheckCtx,
417-
) -> CheckAction {
418-
remquo_common(actual, expected, ctx)
419-
}
420411
}
421412

422413
impl MaybeOverride<(f64, f64)> for SpecialCase {
@@ -428,15 +419,6 @@ impl MaybeOverride<(f64, f64)> for SpecialCase {
428419
) -> CheckAction {
429420
binop_common(input, actual, expected, ctx)
430421
}
431-
432-
fn check_int<I: Int>(
433-
_input: (f64, f64),
434-
actual: I,
435-
expected: I,
436-
ctx: &CheckCtx,
437-
) -> CheckAction {
438-
remquo_common(actual, expected, ctx)
439-
}
440422
}
441423

442424
#[cfg(f128_enabled)]
@@ -499,19 +481,6 @@ fn binop_common<F1: Float, F2: Float>(
499481
DEFAULT
500482
}
501483

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

0 commit comments

Comments
 (0)