Skip to content

Commit a06fe5a

Browse files
committed
extract core operation name instead of listing all function name variants
1 parent 6c7a54c commit a06fe5a

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/math.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,61 +210,70 @@ where
210210
let pi_over_2 = (pi / two).value;
211211
let pi_over_4 = (pi_over_2 / two).value;
212212

213-
Some(match (intrinsic_name, args) {
213+
// Remove `f32`/`f64` suffix, if any.
214+
let name = intrinsic_name
215+
.strip_suffix("f32")
216+
.or_else(|| intrinsic_name.strip_suffix("f64"))
217+
.unwrap_or(intrinsic_name);
218+
// Also strip trailing `f` (indicates "float"), with an exception for "erf" to avoid
219+
// removing that `f`.
220+
let name = if name == "erf" { name } else { name.strip_suffix("f").unwrap_or(name) };
221+
Some(match (name, args) {
214222
// cos(±0) and cosh(±0)= 1
215-
("cosf32" | "cosf64" | "coshf" | "cosh", [input]) if input.is_zero() => one,
223+
("cos" | "cosh", [input]) if input.is_zero() => one,
216224

217225
// e^0 = 1
218-
("expf32" | "expf64" | "exp2f32" | "exp2f64", [input]) if input.is_zero() => one,
226+
("exp" | "exp2", [input]) if input.is_zero() => one,
219227

220228
// tanh(±INF) = ±1
221-
("tanhf" | "tanh", [input]) if input.is_infinite() => one.copy_sign(*input),
229+
("tanh", [input]) if input.is_infinite() => one.copy_sign(*input),
222230

223231
// atan(±INF) = ±π/2
224-
("atanf" | "atan", [input]) if input.is_infinite() => pi_over_2.copy_sign(*input),
232+
("atan", [input]) if input.is_infinite() => pi_over_2.copy_sign(*input),
225233

226234
// erf(±INF) = ±1
227-
("erff" | "erf", [input]) if input.is_infinite() => one.copy_sign(*input),
235+
("erf", [input]) if input.is_infinite() => one.copy_sign(*input),
228236

229237
// erfc(-INF) = 2
230-
("erfcf" | "erfc", [input]) if input.is_neg_infinity() => (one + one).value,
238+
("erfc", [input]) if input.is_neg_infinity() => (one + one).value,
231239

232240
// hypot(x, ±0) = abs(x), if x is not a NaN.
233-
("_hypotf" | "hypotf" | "_hypot" | "hypot", [x, y]) if !x.is_nan() && y.is_zero() =>
241+
// `_hypot` is the Windows name for this.
242+
("_hypot" | "hypot", [x, y]) if !x.is_nan() && y.is_zero() =>
234243
x.abs(),
235244

236245
// atan2(±0,−0) = ±π.
237246
// atan2(±0, y) = ±π for y < 0.
238247
// Must check for non NaN because `y.is_negative()` also applies to NaN.
239-
("atan2f" | "atan2", [x, y]) if (x.is_zero() && (y.is_negative() && !y.is_nan())) =>
248+
("atan2", [x, y]) if (x.is_zero() && (y.is_negative() && !y.is_nan())) =>
240249
pi.copy_sign(*x),
241250

242251
// atan2(±x,−∞) = ±π for finite x > 0.
243-
("atan2f" | "atan2", [x, y])
252+
("atan2", [x, y])
244253
if (!x.is_zero() && !x.is_infinite()) && y.is_neg_infinity() =>
245254
pi.copy_sign(*x),
246255

247256
// atan2(x, ±0) = −π/2 for x < 0.
248257
// atan2(x, ±0) = π/2 for x > 0.
249-
("atan2f" | "atan2", [x, y]) if !x.is_zero() && y.is_zero() => pi_over_2.copy_sign(*x),
258+
("atan2", [x, y]) if !x.is_zero() && y.is_zero() => pi_over_2.copy_sign(*x),
250259

251260
//atan2(±∞, −∞) = ±3π/4
252-
("atan2f" | "atan2", [x, y]) if x.is_infinite() && y.is_neg_infinity() =>
261+
("atan2", [x, y]) if x.is_infinite() && y.is_neg_infinity() =>
253262
(pi_over_4 * three).value.copy_sign(*x),
254263

255264
//atan2(±∞, +∞) = ±π/4
256-
("atan2f" | "atan2", [x, y]) if x.is_infinite() && y.is_pos_infinity() =>
265+
("atan2", [x, y]) if x.is_infinite() && y.is_pos_infinity() =>
257266
pi_over_4.copy_sign(*x),
258267

259268
// atan2(±∞, y) returns ±π/2 for finite y.
260-
("atan2f" | "atan2", [x, y]) if x.is_infinite() && (!y.is_infinite() && !y.is_nan()) =>
269+
("atan2", [x, y]) if x.is_infinite() && (!y.is_infinite() && !y.is_nan()) =>
261270
pi_over_2.copy_sign(*x),
262271

263272
// (-1)^(±INF) = 1
264-
("powf32" | "powf64", [base, exp]) if *base == -one && exp.is_infinite() => one,
273+
("pow", [base, exp]) if *base == -one && exp.is_infinite() => one,
265274

266275
// 1^y = 1 for any y, even a NaN
267-
("powf32" | "powf64", [base, exp]) if *base == one => {
276+
("pow", [base, exp]) if *base == one => {
268277
let rng = this.machine.rng.get_mut();
269278
// SNaN exponents get special treatment: they might return 1, or a NaN.
270279
let return_nan = exp.is_signaling() && this.machine.float_nondet && rng.random();
@@ -273,7 +282,7 @@ where
273282
}
274283

275284
// x^(±0) = 1 for any x, even a NaN
276-
("powf32" | "powf64", [base, exp]) if exp.is_zero() => {
285+
("pow", [base, exp]) if exp.is_zero() => {
277286
let rng = this.machine.rng.get_mut();
278287
// SNaN bases get special treatment: they might return 1, or a NaN.
279288
let return_nan = base.is_signaling() && this.machine.float_nondet && rng.random();

0 commit comments

Comments
 (0)