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

Commit d7721d1

Browse files
authored
Merge pull request #359 from tgross35/update-check-ctx
Change the `CheckCtx` constructor to take a `Name` enum
2 parents 2e9bd1d + 655e812 commit d7721d1

File tree

8 files changed

+32
-45
lines changed

8 files changed

+32
-45
lines changed

crates/libm-macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,9 @@ impl VisitMut for MacroReplace {
628628
}
629629
}
630630

631-
/// Return the unsuffixed name of a function.
631+
/// Return the unsuffixed version of a function name; e.g. `abs` and `absf` both return `abs`,
632+
/// `lgamma_r` and `lgammaf_r` both return `lgamma_r`.
632633
fn base_name(name: &str) -> &str {
633-
// Keep this in sync with `libm_test::base_name`
634634
let known_mappings = &[
635635
("erff", "erf"),
636636
("erf", "erf"),

crates/libm-test/benches/random.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ where
4747
Op: MathOp,
4848
CachedInput: GenerateInput<Op::RustArgs>,
4949
{
50-
let name = Op::NAME_STR;
50+
let name = Op::NAME;
5151

5252
let ulp = libm_test::musl_allowed_ulp(name);
53-
let ctx = CheckCtx::new(ulp, name, CheckBasis::Musl);
53+
let ctx = CheckCtx::new(ulp, Op::IDENTIFIER, CheckBasis::Musl);
5454
let benchvec: Vec<_> =
5555
random::get_test_cases::<Op::RustArgs>(&ctx).take(BENCH_ITER_ITEMS).collect();
5656

crates/libm-test/src/lib.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod precision;
66
mod test_traits;
77

88
pub use libm::support::{Float, Int};
9-
pub use op::{BaseName, MathOp, Name};
9+
pub use op::{BaseName, Identifier, MathOp};
1010
pub use precision::{MaybeOverride, SpecialCase, multiprec_allowed_ulp, musl_allowed_ulp};
1111
pub use test_traits::{CheckBasis, CheckCtx, CheckOutput, GenerateInput, Hex, TupleCall};
1212

@@ -17,27 +17,6 @@ pub type TestResult<T = (), E = anyhow::Error> = Result<T, E>;
1717
// List of all files present in libm's source
1818
include!(concat!(env!("OUT_DIR"), "/all_files.rs"));
1919

20-
/// Return the unsuffixed version of a function name; e.g. `abs` and `absf` both return `abs`,
21-
/// `lgamma_r` and `lgammaf_r` both return `lgamma_r`.
22-
pub fn base_name(name: &str) -> &str {
23-
let known_mappings = &[
24-
("erff", "erf"),
25-
("erf", "erf"),
26-
("lgammaf_r", "lgamma_r"),
27-
("modff", "modf"),
28-
("modf", "modf"),
29-
];
30-
31-
match known_mappings.iter().find(|known| known.0 == name) {
32-
Some(found) => found.1,
33-
None => name
34-
.strip_suffix("f")
35-
.or_else(|| name.strip_suffix("f16"))
36-
.or_else(|| name.strip_suffix("f128"))
37-
.unwrap_or(name),
38-
}
39-
}
40-
4120
/// True if `EMULATED` is set and nonempty. Used to determine how many iterations to run.
4221
pub const fn emulated() -> bool {
4322
match option_env!("EMULATED") {

crates/libm-test/src/op.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
1616
use crate::{CheckOutput, Float, TupleCall};
1717

18-
/// An enum representing each possible routine name.
18+
/// An enum representing each possible symbol name (`sin`, `sinf`, `sinl`, etc).
1919
#[libm_macros::function_enum(BaseName)]
2020
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
21-
pub enum Name {}
21+
pub enum Identifier {}
2222

2323
/// The name without any type specifier, e.g. `sin` and `sinf` both become `sin`.
2424
#[libm_macros::base_name_enum]
@@ -58,13 +58,13 @@ pub trait MathOp {
5858
type RustRet: CheckOutput<Self::RustArgs>;
5959

6060
/// The name of this function, including suffix (e.g. `sin`, `sinf`).
61-
const NAME: Name;
61+
const IDENTIFIER: Identifier;
6262

6363
/// The name as a string.
64-
const NAME_STR: &'static str = Self::NAME.as_str();
64+
const NAME: &'static str = Self::IDENTIFIER.as_str();
6565

6666
/// The name of the function excluding the type suffix, e.g. `sin` and `sinf` are both `sin`.
67-
const BASE_NAME: BaseName = Self::NAME.base_name();
67+
const BASE_NAME: BaseName = Self::IDENTIFIER.base_name();
6868

6969
/// The function in `libm` which can be called.
7070
const ROUTINE: Self::RustFn;
@@ -96,7 +96,7 @@ macro_rules! do_thing {
9696
type RustArgs = $RustArgs;
9797
type RustRet = $RustRet;
9898

99-
const NAME: Name = Name::[< $fn_name:camel >];
99+
const IDENTIFIER: Identifier = Identifier::[< $fn_name:camel >];
100100
const ROUTINE: Self::RustFn = libm::$fn_name;
101101
}
102102
}

crates/libm-test/src/precision.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl MaybeOverride<(f64,)> for SpecialCase {
219219

220220
/// Check NaN bits if the function requires it
221221
fn maybe_check_nan_bits<F: Float>(actual: F, expected: F, ctx: &CheckCtx) -> Option<TestResult> {
222-
if !(ctx.base_name == "fabs" || ctx.base_name == "copysign") {
222+
if !(ctx.base_name_str == "fabs" || ctx.base_name_str == "copysign") {
223223
return None;
224224
}
225225

@@ -277,7 +277,7 @@ fn maybe_skip_binop_nan<F1: Float, F2: Float>(
277277
) -> Option<TestResult> {
278278
match ctx.basis {
279279
CheckBasis::Musl => {
280-
if (ctx.base_name == "fmax" || ctx.base_name == "fmin")
280+
if (ctx.base_name_str == "fmax" || ctx.base_name_str == "fmin")
281281
&& (input.0.is_nan() || input.1.is_nan())
282282
&& expected.is_nan()
283283
{
@@ -287,7 +287,7 @@ fn maybe_skip_binop_nan<F1: Float, F2: Float>(
287287
}
288288
}
289289
CheckBasis::Mpfr => {
290-
if ctx.base_name == "copysign" && input.1.is_nan() {
290+
if ctx.base_name_str == "copysign" && input.1.is_nan() {
291291
SKIP
292292
} else {
293293
None
@@ -353,7 +353,7 @@ fn bessel_prec_dropoff<F: Float>(
353353
ulp: &mut u32,
354354
ctx: &CheckCtx,
355355
) -> Option<TestResult> {
356-
if ctx.base_name == "jn" {
356+
if ctx.base_name_str == "jn" {
357357
if input.0 > 4000 {
358358
return XFAIL;
359359
} else if input.0 > 2000 {

crates/libm-test/src/test_traits.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,33 @@ use std::fmt;
1111

1212
use anyhow::{Context, bail, ensure};
1313

14-
use crate::{Float, Int, MaybeOverride, SpecialCase, TestResult};
14+
use crate::{BaseName, Float, Identifier, Int, MaybeOverride, SpecialCase, TestResult};
1515

1616
/// Context passed to [`CheckOutput`].
1717
#[derive(Clone, Debug, PartialEq, Eq)]
1818
pub struct CheckCtx {
1919
/// Allowed ULP deviation
2020
pub ulp: u32,
21+
pub fn_ident: Identifier,
22+
pub base_name: BaseName,
2123
/// Function name.
2224
pub fn_name: &'static str,
2325
/// Return the unsuffixed version of the function name.
24-
pub base_name: &'static str,
26+
pub base_name_str: &'static str,
2527
/// Source of truth for tests.
2628
pub basis: CheckBasis,
2729
}
2830

2931
impl CheckCtx {
30-
pub fn new(ulp: u32, fname: &'static str, basis: CheckBasis) -> Self {
31-
let base_name = crate::base_name(fname);
32-
Self { ulp, fn_name: fname, base_name, basis }
32+
pub fn new(ulp: u32, fn_ident: Identifier, basis: CheckBasis) -> Self {
33+
Self {
34+
ulp,
35+
fn_ident,
36+
fn_name: fn_ident.as_str(),
37+
base_name: fn_ident.base_name(),
38+
base_name_str: fn_ident.base_name().as_str(),
39+
basis,
40+
}
3341
}
3442
}
3543

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ where
3434
Op: MathOp,
3535
CachedInput: GenerateInput<Op::RustArgs>,
3636
{
37-
let name = Op::NAME_STR;
37+
let name = Op::NAME;
3838
let ulp = musl_allowed_ulp(name);
39-
let ctx = CheckCtx::new(ulp, name, CheckBasis::Musl);
39+
let ctx = CheckCtx::new(ulp, Op::IDENTIFIER, CheckBasis::Musl);
4040
let cases = random::get_test_cases::<Op::RustArgs>(&ctx);
4141

4242
for input in cases {

crates/libm-test/tests/multiprecision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ where
2929
Op: MathOp + MpOp,
3030
CachedInput: GenerateInput<Op::RustArgs>,
3131
{
32-
let name = Op::NAME_STR;
32+
let name = Op::NAME;
3333

3434
let ulp = multiprec_allowed_ulp(name);
3535
let mut mp_vals = Op::new_mp();
36-
let ctx = CheckCtx::new(ulp, name, CheckBasis::Mpfr);
36+
let ctx = CheckCtx::new(ulp, Op::IDENTIFIER, CheckBasis::Mpfr);
3737
let cases = random::get_test_cases::<Op::RustArgs>(&ctx);
3838

3939
for input in cases {

0 commit comments

Comments
 (0)