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

Commit 1e18307

Browse files
committed
Change the CheckCtx constructor to take a Name enum
This prepares to eliminate some reliance on string matching but does not yet make those changes.
1 parent 2e9bd1d commit 1e18307

File tree

8 files changed

+40
-50
lines changed

8 files changed

+40
-50
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050
let name = Op::NAME_STR;
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::NAME, CheckBasis::Musl);
5454
let benchvec: Vec<_> =
5555
random::get_test_cases::<Op::RustArgs>(&ctx).take(BENCH_ITER_ITEMS).collect();
5656

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ pub fn get_test_cases<RustArgs>(ctx: &CheckCtx) -> impl Iterator<Item = RustArgs
110110
where
111111
CachedInput: GenerateInput<RustArgs>,
112112
{
113-
let inputs =
114-
if ctx.fn_name == "jn" || ctx.fn_name == "jnf" { &TEST_CASES_JN } else { &TEST_CASES };
113+
let inputs = if ctx.fn_name_str == "jn" || ctx.fn_name_str == "jnf" {
114+
&TEST_CASES_JN
115+
} else {
116+
&TEST_CASES
117+
};
115118
inputs.get_cases()
116119
}

crates/libm-test/src/lib.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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/precision.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,25 @@ impl MaybeOverride<(f32,)> for SpecialCase {
111111
ctx: &CheckCtx,
112112
) -> Option<TestResult> {
113113
if ctx.basis == CheckBasis::Musl {
114-
if ctx.fn_name == "expm1f" && input.0 > 80.0 && actual.is_infinite() {
114+
if ctx.fn_name_str == "expm1f" && input.0 > 80.0 && actual.is_infinite() {
115115
// we return infinity but the number is representable
116116
return XFAIL;
117117
}
118118

119-
if ctx.fn_name == "sinhf" && input.0.abs() > 80.0 && actual.is_nan() {
119+
if ctx.fn_name_str == "sinhf" && input.0.abs() > 80.0 && actual.is_nan() {
120120
// we return some NaN that should be real values or infinite
121121
// doesn't seem to happen on x86
122122
return XFAIL;
123123
}
124124
}
125125

126-
if ctx.fn_name == "acoshf" && input.0 < -1.0 {
126+
if ctx.fn_name_str == "acoshf" && input.0 < -1.0 {
127127
// acoshf is undefined for x <= 1.0, but we return a random result at lower
128128
// values.
129129
return XFAIL;
130130
}
131131

132-
if ctx.fn_name == "lgammaf" || ctx.fn_name == "lgammaf_r" && input.0 < 0.0 {
132+
if ctx.fn_name_str == "lgammaf" || ctx.fn_name_str == "lgammaf_r" && input.0 < 0.0 {
133133
// loggamma should not be defined for x < 0, yet we both return results
134134
return XFAIL;
135135
}
@@ -146,7 +146,7 @@ impl MaybeOverride<(f32,)> for SpecialCase {
146146
// On MPFR for lgammaf_r, we set -1 as the integer result for negative infinity but MPFR
147147
// sets +1
148148
if ctx.basis == CheckBasis::Mpfr
149-
&& ctx.fn_name == "lgammaf_r"
149+
&& ctx.fn_name_str == "lgammaf_r"
150150
&& input.0 == f32::NEG_INFINITY
151151
&& actual.abs() == expected.abs()
152152
{
@@ -166,13 +166,13 @@ impl MaybeOverride<(f64,)> for SpecialCase {
166166
ctx: &CheckCtx,
167167
) -> Option<TestResult> {
168168
if ctx.basis == CheckBasis::Musl {
169-
if cfg!(target_arch = "x86") && ctx.fn_name == "acosh" && input.0 < 1.0 {
169+
if cfg!(target_arch = "x86") && ctx.fn_name_str == "acosh" && input.0 < 1.0 {
170170
// The function is undefined, both implementations return random results
171171
return SKIP;
172172
}
173173

174174
if cfg!(x86_no_sse)
175-
&& ctx.fn_name == "ceil"
175+
&& ctx.fn_name_str == "ceil"
176176
&& input.0 < 0.0
177177
&& input.0 > -1.0
178178
&& expected == F::ZERO
@@ -183,13 +183,13 @@ impl MaybeOverride<(f64,)> for SpecialCase {
183183
}
184184
}
185185

186-
if ctx.fn_name == "acosh" && input.0 < 1.0 {
186+
if ctx.fn_name_str == "acosh" && input.0 < 1.0 {
187187
// The function is undefined for the inputs, musl and our libm both return
188188
// random results.
189189
return XFAIL;
190190
}
191191

192-
if ctx.fn_name == "lgamma" || ctx.fn_name == "lgamma_r" && input.0 < 0.0 {
192+
if ctx.fn_name_str == "lgamma" || ctx.fn_name_str == "lgamma_r" && input.0 < 0.0 {
193193
// loggamma should not be defined for x < 0, yet we both return results
194194
return XFAIL;
195195
}
@@ -206,7 +206,7 @@ impl MaybeOverride<(f64,)> for SpecialCase {
206206
// On MPFR for lgamma_r, we set -1 as the integer result for negative infinity but MPFR
207207
// sets +1
208208
if ctx.basis == CheckBasis::Mpfr
209-
&& ctx.fn_name == "lgamma_r"
209+
&& ctx.fn_name_str == "lgamma_r"
210210
&& input.0 == f64::NEG_INFINITY
211211
&& actual.abs() == expected.abs()
212212
{
@@ -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
@@ -308,7 +308,7 @@ impl MaybeOverride<(i32, f32)> for SpecialCase {
308308
CheckBasis::Musl => bessel_prec_dropoff(input, ulp, ctx),
309309
CheckBasis::Mpfr => {
310310
// We return +0.0, MPFR returns -0.0
311-
if ctx.fn_name == "jnf"
311+
if ctx.fn_name_str == "jnf"
312312
&& input.1 == f32::NEG_INFINITY
313313
&& actual == F::ZERO
314314
&& expected == F::ZERO
@@ -333,7 +333,7 @@ impl MaybeOverride<(i32, f64)> for SpecialCase {
333333
CheckBasis::Musl => bessel_prec_dropoff(input, ulp, ctx),
334334
CheckBasis::Mpfr => {
335335
// We return +0.0, MPFR returns -0.0
336-
if ctx.fn_name == "jn"
336+
if ctx.fn_name_str == "jn"
337337
&& input.1 == f64::NEG_INFINITY
338338
&& actual == F::ZERO
339339
&& expected == F::ZERO
@@ -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: 14 additions & 6 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, Int, MaybeOverride, Name, 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_name: Name,
22+
pub base_name: BaseName,
2123
/// Function name.
22-
pub fn_name: &'static str,
24+
pub fn_name_str: &'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_name: Name, basis: CheckBasis) -> Self {
33+
Self {
34+
ulp,
35+
fn_name,
36+
fn_name_str: fn_name.as_str(),
37+
base_name: fn_name.base_name(),
38+
base_name_str: fn_name.base_name().as_str(),
39+
basis,
40+
}
3341
}
3442
}
3543

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ where
3636
{
3737
let name = Op::NAME_STR;
3838
let ulp = musl_allowed_ulp(name);
39-
let ctx = CheckCtx::new(ulp, name, CheckBasis::Musl);
39+
let ctx = CheckCtx::new(ulp, Op::NAME, 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
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::NAME, CheckBasis::Mpfr);
3737
let cases = random::get_test_cases::<Op::RustArgs>(&ctx);
3838

3939
for input in cases {

0 commit comments

Comments
 (0)