Skip to content

Commit 607d4df

Browse files
authored
Merge pull request rust-lang#369 from tgross35/eliminate-string-matching
Replace string function name matching with enums where possible
2 parents 83b37a0 + ff185c6 commit 607d4df

File tree

3 files changed

+46
-62
lines changed

3 files changed

+46
-62
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rand::{Rng, SeedableRng};
77
use rand_chacha::ChaCha8Rng;
88

99
use super::CachedInput;
10-
use crate::{CheckCtx, GenerateInput};
10+
use crate::{BaseName, CheckCtx, GenerateInput};
1111

1212
const SEED: [u8; 32] = *b"3.141592653589793238462643383279";
1313

@@ -110,7 +110,6 @@ 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.base_name == BaseName::Jn { &TEST_CASES_JN } else { &TEST_CASES };
115114
inputs.get_cases()
116115
}

crates/libm-test/src/precision.rs

+44-56
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::f32;
66
use CheckBasis::{Mpfr, Musl};
77
use Identifier as Id;
88

9-
use crate::{CheckBasis, CheckCtx, Float, Identifier, Int, TestResult};
9+
use crate::{BaseName, CheckBasis, CheckCtx, Float, Identifier, Int, TestResult};
1010

1111
/// Type implementing [`IgnoreCase`].
1212
pub struct SpecialCase;
@@ -106,25 +106,26 @@ impl MaybeOverride<(f32,)> for SpecialCase {
106106
ctx: &CheckCtx,
107107
) -> Option<TestResult> {
108108
if ctx.basis == CheckBasis::Musl {
109-
if ctx.fn_name == "expm1f" && input.0 > 80.0 && actual.is_infinite() {
109+
if ctx.base_name == BaseName::Expm1 && input.0 > 80.0 && actual.is_infinite() {
110110
// we return infinity but the number is representable
111111
return XFAIL;
112112
}
113113

114-
if ctx.fn_name == "sinhf" && input.0.abs() > 80.0 && actual.is_nan() {
114+
if ctx.base_name == BaseName::Sinh && input.0.abs() > 80.0 && actual.is_nan() {
115115
// we return some NaN that should be real values or infinite
116116
// doesn't seem to happen on x86
117117
return XFAIL;
118118
}
119119
}
120120

121-
if ctx.fn_name == "acoshf" && input.0 < -1.0 {
121+
if ctx.base_name == BaseName::Acosh && input.0 < -1.0 {
122122
// acoshf is undefined for x <= 1.0, but we return a random result at lower
123123
// values.
124124
return XFAIL;
125125
}
126126

127-
if ctx.fn_name == "lgammaf" || ctx.fn_name == "lgammaf_r" && input.0 < 0.0 {
127+
if ctx.base_name == BaseName::Lgamma || ctx.base_name == BaseName::LgammaR && input.0 < 0.0
128+
{
128129
// loggamma should not be defined for x < 0, yet we both return results
129130
return XFAIL;
130131
}
@@ -141,7 +142,7 @@ impl MaybeOverride<(f32,)> for SpecialCase {
141142
// On MPFR for lgammaf_r, we set -1 as the integer result for negative infinity but MPFR
142143
// sets +1
143144
if ctx.basis == CheckBasis::Mpfr
144-
&& ctx.fn_name == "lgammaf_r"
145+
&& ctx.base_name == BaseName::LgammaR
145146
&& input.0 == f32::NEG_INFINITY
146147
&& actual.abs() == expected.abs()
147148
{
@@ -161,13 +162,13 @@ impl MaybeOverride<(f64,)> for SpecialCase {
161162
ctx: &CheckCtx,
162163
) -> Option<TestResult> {
163164
if ctx.basis == CheckBasis::Musl {
164-
if cfg!(target_arch = "x86") && ctx.fn_name == "acosh" && input.0 < 1.0 {
165+
if cfg!(target_arch = "x86") && ctx.base_name == BaseName::Acosh && input.0 < 1.0 {
165166
// The function is undefined, both implementations return random results
166167
return SKIP;
167168
}
168169

169170
if cfg!(x86_no_sse)
170-
&& ctx.fn_name == "ceil"
171+
&& ctx.base_name == BaseName::Ceil
171172
&& input.0 < 0.0
172173
&& input.0 > -1.0
173174
&& expected == F::ZERO
@@ -178,13 +179,14 @@ impl MaybeOverride<(f64,)> for SpecialCase {
178179
}
179180
}
180181

181-
if ctx.fn_name == "acosh" && input.0 < 1.0 {
182+
if ctx.base_name == BaseName::Acosh && input.0 < 1.0 {
182183
// The function is undefined for the inputs, musl and our libm both return
183184
// random results.
184185
return XFAIL;
185186
}
186187

187-
if ctx.fn_name == "lgamma" || ctx.fn_name == "lgamma_r" && input.0 < 0.0 {
188+
if ctx.base_name == BaseName::Lgamma || ctx.base_name == BaseName::LgammaR && input.0 < 0.0
189+
{
188190
// loggamma should not be defined for x < 0, yet we both return results
189191
return XFAIL;
190192
}
@@ -201,7 +203,7 @@ impl MaybeOverride<(f64,)> for SpecialCase {
201203
// On MPFR for lgamma_r, we set -1 as the integer result for negative infinity but MPFR
202204
// sets +1
203205
if ctx.basis == CheckBasis::Mpfr
204-
&& ctx.fn_name == "lgamma_r"
206+
&& ctx.base_name == BaseName::LgammaR
205207
&& input.0 == f64::NEG_INFINITY
206208
&& actual.abs() == expected.abs()
207209
{
@@ -214,7 +216,7 @@ impl MaybeOverride<(f64,)> for SpecialCase {
214216

215217
/// Check NaN bits if the function requires it
216218
fn maybe_check_nan_bits<F: Float>(actual: F, expected: F, ctx: &CheckCtx) -> Option<TestResult> {
217-
if !(ctx.base_name_str == "fabs" || ctx.base_name_str == "copysign") {
219+
if !(ctx.base_name == BaseName::Fabs || ctx.base_name == BaseName::Copysign) {
218220
return None;
219221
}
220222

@@ -270,24 +272,16 @@ fn maybe_skip_binop_nan<F1: Float, F2: Float>(
270272
expected: F2,
271273
ctx: &CheckCtx,
272274
) -> Option<TestResult> {
273-
match ctx.basis {
274-
CheckBasis::Musl => {
275-
if (ctx.base_name_str == "fmax" || ctx.base_name_str == "fmin")
276-
&& (input.0.is_nan() || input.1.is_nan())
277-
&& expected.is_nan()
278-
{
279-
XFAIL
280-
} else {
281-
None
282-
}
283-
}
284-
CheckBasis::Mpfr => {
285-
if ctx.base_name_str == "copysign" && input.1.is_nan() {
286-
SKIP
287-
} else {
288-
None
289-
}
275+
match (&ctx.basis, ctx.base_name) {
276+
(Musl, BaseName::Fmin | BaseName::Fmax)
277+
if (input.0.is_nan() || input.1.is_nan()) && expected.is_nan() =>
278+
{
279+
XFAIL
290280
}
281+
282+
(Mpfr, BaseName::Copysign) if input.1.is_nan() => SKIP,
283+
284+
_ => None,
291285
}
292286
}
293287

@@ -299,20 +293,17 @@ impl MaybeOverride<(i32, f32)> for SpecialCase {
299293
ulp: &mut u32,
300294
ctx: &CheckCtx,
301295
) -> Option<TestResult> {
302-
match ctx.basis {
303-
CheckBasis::Musl => bessel_prec_dropoff(input, ulp, ctx),
304-
CheckBasis::Mpfr => {
305-
// We return +0.0, MPFR returns -0.0
306-
if ctx.fn_name == "jnf"
307-
&& input.1 == f32::NEG_INFINITY
308-
&& actual == F::ZERO
309-
&& expected == F::ZERO
310-
{
311-
XFAIL
312-
} else {
313-
None
314-
}
296+
match (&ctx.basis, ctx.base_name) {
297+
(Musl, _) => bessel_prec_dropoff(input, ulp, ctx),
298+
299+
// We return +0.0, MPFR returns -0.0
300+
(Mpfr, BaseName::Jn)
301+
if input.1 == f32::NEG_INFINITY && actual == F::ZERO && expected == F::ZERO =>
302+
{
303+
XFAIL
315304
}
305+
306+
_ => None,
316307
}
317308
}
318309
}
@@ -324,20 +315,17 @@ impl MaybeOverride<(i32, f64)> for SpecialCase {
324315
ulp: &mut u32,
325316
ctx: &CheckCtx,
326317
) -> Option<TestResult> {
327-
match ctx.basis {
328-
CheckBasis::Musl => bessel_prec_dropoff(input, ulp, ctx),
329-
CheckBasis::Mpfr => {
330-
// We return +0.0, MPFR returns -0.0
331-
if ctx.fn_name == "jn"
332-
&& input.1 == f64::NEG_INFINITY
333-
&& actual == F::ZERO
334-
&& expected == F::ZERO
335-
{
336-
XFAIL
337-
} else {
338-
bessel_prec_dropoff(input, ulp, ctx)
339-
}
318+
match (&ctx.basis, ctx.base_name) {
319+
(Musl, _) => bessel_prec_dropoff(input, ulp, ctx),
320+
321+
// We return +0.0, MPFR returns -0.0
322+
(Mpfr, BaseName::Jn)
323+
if input.1 == f64::NEG_INFINITY && actual == F::ZERO && expected == F::ZERO =>
324+
{
325+
XFAIL
340326
}
327+
328+
_ => None,
341329
}
342330
}
343331
}
@@ -348,7 +336,7 @@ fn bessel_prec_dropoff<F: Float>(
348336
ulp: &mut u32,
349337
ctx: &CheckCtx,
350338
) -> Option<TestResult> {
351-
if ctx.base_name_str == "jn" {
339+
if ctx.base_name == BaseName::Jn {
352340
if input.0 > 4000 {
353341
return XFAIL;
354342
} else if input.0 > 2000 {

crates/libm-test/src/test_traits.rs

-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub struct CheckCtx {
2222
pub base_name: BaseName,
2323
/// Function name.
2424
pub fn_name: &'static str,
25-
/// Return the unsuffixed version of the function name.
26-
pub base_name_str: &'static str,
2725
/// Source of truth for tests.
2826
pub basis: CheckBasis,
2927
}
@@ -36,7 +34,6 @@ impl CheckCtx {
3634
fn_ident,
3735
fn_name: fn_ident.as_str(),
3836
base_name: fn_ident.base_name(),
39-
base_name_str: fn_ident.base_name().as_str(),
4037
basis,
4138
};
4239
ret.ulp = crate::default_ulp(&ret);

0 commit comments

Comments
 (0)