Skip to content

Commit c04314c

Browse files
QelxirosRalfJung
andcommitted
unstably constify float mul_add methods
Co-authored-by: Ralf Jung <[email protected]>
1 parent 70e1680 commit c04314c

File tree

14 files changed

+59
-94
lines changed

14 files changed

+59
-94
lines changed

core/src/intrinsics/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,28 +1312,28 @@ pub fn log2f128(x: f128) -> f128;
13121312
/// [`f16::mul_add`](../../std/primitive.f16.html#method.mul_add)
13131313
#[rustc_intrinsic]
13141314
#[rustc_nounwind]
1315-
pub fn fmaf16(a: f16, b: f16, c: f16) -> f16;
1315+
pub const fn fmaf16(a: f16, b: f16, c: f16) -> f16;
13161316
/// Returns `a * b + c` for `f32` values.
13171317
///
13181318
/// The stabilized version of this intrinsic is
13191319
/// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
13201320
#[rustc_intrinsic]
13211321
#[rustc_nounwind]
1322-
pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1322+
pub const fn fmaf32(a: f32, b: f32, c: f32) -> f32;
13231323
/// Returns `a * b + c` for `f64` values.
13241324
///
13251325
/// The stabilized version of this intrinsic is
13261326
/// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
13271327
#[rustc_intrinsic]
13281328
#[rustc_nounwind]
1329-
pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1329+
pub const fn fmaf64(a: f64, b: f64, c: f64) -> f64;
13301330
/// Returns `a * b + c` for `f128` values.
13311331
///
13321332
/// The stabilized version of this intrinsic is
13331333
/// [`f128::mul_add`](../../std/primitive.f128.html#method.mul_add)
13341334
#[rustc_intrinsic]
13351335
#[rustc_nounwind]
1336-
pub fn fmaf128(a: f128, b: f128, c: f128) -> f128;
1336+
pub const fn fmaf128(a: f128, b: f128, c: f128) -> f128;
13371337

13381338
/// Returns `a * b + c` for `f16` values, non-deterministically executing
13391339
/// either a fused multiply-add or two operations with rounding of the
@@ -1347,7 +1347,7 @@ pub fn fmaf128(a: f128, b: f128, c: f128) -> f128;
13471347
/// example.
13481348
#[rustc_intrinsic]
13491349
#[rustc_nounwind]
1350-
pub fn fmuladdf16(a: f16, b: f16, c: f16) -> f16;
1350+
pub const fn fmuladdf16(a: f16, b: f16, c: f16) -> f16;
13511351
/// Returns `a * b + c` for `f32` values, non-deterministically executing
13521352
/// either a fused multiply-add or two operations with rounding of the
13531353
/// intermediate result.
@@ -1360,7 +1360,7 @@ pub fn fmuladdf16(a: f16, b: f16, c: f16) -> f16;
13601360
/// example.
13611361
#[rustc_intrinsic]
13621362
#[rustc_nounwind]
1363-
pub fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
1363+
pub const fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
13641364
/// Returns `a * b + c` for `f64` values, non-deterministically executing
13651365
/// either a fused multiply-add or two operations with rounding of the
13661366
/// intermediate result.
@@ -1373,7 +1373,7 @@ pub fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
13731373
/// example.
13741374
#[rustc_intrinsic]
13751375
#[rustc_nounwind]
1376-
pub fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
1376+
pub const fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
13771377
/// Returns `a * b + c` for `f128` values, non-deterministically executing
13781378
/// either a fused multiply-add or two operations with rounding of the
13791379
/// intermediate result.
@@ -1386,7 +1386,7 @@ pub fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
13861386
/// example.
13871387
#[rustc_intrinsic]
13881388
#[rustc_nounwind]
1389-
pub fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
1389+
pub const fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
13901390

13911391
/// Returns the largest integer less than or equal to an `f16`.
13921392
///

core/src/num/f128.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,8 @@ impl f128 {
16591659
#[doc(alias = "fmaf128", alias = "fusedMultiplyAdd")]
16601660
#[unstable(feature = "f128", issue = "116909")]
16611661
#[must_use = "method returns a new number and does not mutate the original value"]
1662-
pub fn mul_add(self, a: f128, b: f128) -> f128 {
1662+
#[rustc_const_unstable(feature = "const_mul_add", issue = "146724")]
1663+
pub const fn mul_add(self, a: f128, b: f128) -> f128 {
16631664
intrinsics::fmaf128(self, a, b)
16641665
}
16651666

core/src/num/f16.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,8 @@ impl f16 {
16341634
#[unstable(feature = "f16", issue = "116909")]
16351635
#[doc(alias = "fmaf16", alias = "fusedMultiplyAdd")]
16361636
#[must_use = "method returns a new number and does not mutate the original value"]
1637-
pub fn mul_add(self, a: f16, b: f16) -> f16 {
1637+
#[rustc_const_unstable(feature = "const_mul_add", issue = "146724")]
1638+
pub const fn mul_add(self, a: f16, b: f16) -> f16 {
16381639
intrinsics::fmaf16(self, a, b)
16391640
}
16401641

core/src/num/f32.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,8 @@ pub mod math {
17991799
#[doc(alias = "fmaf", alias = "fusedMultiplyAdd")]
18001800
#[must_use = "method returns a new number and does not mutate the original value"]
18011801
#[unstable(feature = "core_float_math", issue = "137578")]
1802-
pub fn mul_add(x: f32, y: f32, z: f32) -> f32 {
1802+
#[rustc_const_unstable(feature = "const_mul_add", issue = "146724")]
1803+
pub const fn mul_add(x: f32, y: f32, z: f32) -> f32 {
18031804
intrinsics::fmaf32(x, y, z)
18041805
}
18051806

core/src/num/f64.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,8 @@ pub mod math {
17971797
#[doc(alias = "fma", alias = "fusedMultiplyAdd")]
17981798
#[unstable(feature = "core_float_math", issue = "137578")]
17991799
#[must_use = "method returns a new number and does not mutate the original value"]
1800-
pub fn mul_add(x: f64, a: f64, b: f64) -> f64 {
1800+
#[rustc_const_unstable(feature = "const_mul_add", issue = "146724")]
1801+
pub const fn mul_add(x: f64, a: f64, b: f64) -> f64 {
18011802
intrinsics::fmaf64(x, a, b)
18021803
}
18031804

coretests/tests/floats/f128.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ const TOL_PRECISE: f128 = 1e-28;
2020
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
2121
// the intrinsics.
2222

23-
#[test]
24-
#[cfg(not(miri))]
25-
#[cfg(target_has_reliable_f128_math)]
26-
fn test_mul_add() {
27-
let nan: f128 = f128::NAN;
28-
let inf: f128 = f128::INFINITY;
29-
let neg_inf: f128 = f128::NEG_INFINITY;
30-
assert_biteq!(12.3f128.mul_add(4.5, 6.7), 62.0500000000000000000000000000000037);
31-
assert_biteq!((-12.3f128).mul_add(-4.5, -6.7), 48.6500000000000000000000000000000049);
32-
assert_biteq!(0.0f128.mul_add(8.9, 1.2), 1.2);
33-
assert_biteq!(3.4f128.mul_add(-0.0, 5.6), 5.6);
34-
assert!(nan.mul_add(7.8, 9.0).is_nan());
35-
assert_biteq!(inf.mul_add(7.8, 9.0), inf);
36-
assert_biteq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
37-
assert_biteq!(8.9f128.mul_add(inf, 3.2), inf);
38-
assert_biteq!((-3.2f128).mul_add(2.4, neg_inf), neg_inf);
39-
}
40-
4123
#[test]
4224
#[cfg(any(miri, target_has_reliable_f128_math))]
4325
fn test_max_recip() {

coretests/tests/floats/f16.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@ const TOL_P4: f16 = 10.0;
2222
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
2323
// the intrinsics.
2424

25-
#[test]
26-
#[cfg(not(miri))]
27-
#[cfg(target_has_reliable_f16_math)]
28-
fn test_mul_add() {
29-
let nan: f16 = f16::NAN;
30-
let inf: f16 = f16::INFINITY;
31-
let neg_inf: f16 = f16::NEG_INFINITY;
32-
assert_biteq!(12.3f16.mul_add(4.5, 6.7), 62.031);
33-
assert_biteq!((-12.3f16).mul_add(-4.5, -6.7), 48.625);
34-
assert_biteq!(0.0f16.mul_add(8.9, 1.2), 1.2);
35-
assert_biteq!(3.4f16.mul_add(-0.0, 5.6), 5.6);
36-
assert!(nan.mul_add(7.8, 9.0).is_nan());
37-
assert_biteq!(inf.mul_add(7.8, 9.0), inf);
38-
assert_biteq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
39-
assert_biteq!(8.9f16.mul_add(inf, 3.2), inf);
40-
assert_biteq!((-3.2f16).mul_add(2.4, neg_inf), neg_inf);
41-
}
42-
4325
#[test]
4426
#[cfg(any(miri, target_has_reliable_f16_math))]
4527
fn test_max_recip() {

coretests/tests/floats/f32.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

coretests/tests/floats/f64.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

coretests/tests/floats/mod.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ trait TestableFloat: Sized {
3434
const RAW_12_DOT_5: Self;
3535
const RAW_1337: Self;
3636
const RAW_MINUS_14_DOT_25: Self;
37+
/// The result of 12.3.mul_add(4.5, 6.7)
38+
const MUL_ADD_RESULT: Self;
39+
/// The result of (-12.3).mul_add(-4.5, -6.7)
40+
const NEG_MUL_ADD_RESULT: Self;
3741
}
3842

3943
impl TestableFloat for f16 {
@@ -58,6 +62,8 @@ impl TestableFloat for f16 {
5862
const RAW_12_DOT_5: Self = Self::from_bits(0x4a40);
5963
const RAW_1337: Self = Self::from_bits(0x6539);
6064
const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xcb20);
65+
const MUL_ADD_RESULT: Self = 62.031;
66+
const NEG_MUL_ADD_RESULT: Self = 48.625;
6167
}
6268

6369
impl TestableFloat for f32 {
@@ -84,6 +90,8 @@ impl TestableFloat for f32 {
8490
const RAW_12_DOT_5: Self = Self::from_bits(0x41480000);
8591
const RAW_1337: Self = Self::from_bits(0x44a72000);
8692
const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc1640000);
93+
const MUL_ADD_RESULT: Self = 62.05;
94+
const NEG_MUL_ADD_RESULT: Self = 48.65;
8795
}
8896

8997
impl TestableFloat for f64 {
@@ -106,6 +114,8 @@ impl TestableFloat for f64 {
106114
const RAW_12_DOT_5: Self = Self::from_bits(0x4029000000000000);
107115
const RAW_1337: Self = Self::from_bits(0x4094e40000000000);
108116
const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc02c800000000000);
117+
const MUL_ADD_RESULT: Self = 62.050000000000004;
118+
const NEG_MUL_ADD_RESULT: Self = 48.650000000000006;
109119
}
110120

111121
impl TestableFloat for f128 {
@@ -128,6 +138,8 @@ impl TestableFloat for f128 {
128138
const RAW_12_DOT_5: Self = Self::from_bits(0x40029000000000000000000000000000);
129139
const RAW_1337: Self = Self::from_bits(0x40094e40000000000000000000000000);
130140
const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc002c800000000000000000000000000);
141+
const MUL_ADD_RESULT: Self = 62.0500000000000000000000000000000037;
142+
const NEG_MUL_ADD_RESULT: Self = 48.6500000000000000000000000000000049;
131143
}
132144

133145
/// Determine the tolerance for values of the argument type.
@@ -359,8 +371,6 @@ macro_rules! float_test {
359371

360372
mod f128;
361373
mod f16;
362-
mod f32;
363-
mod f64;
364374

365375
float_test! {
366376
name: num,
@@ -1542,3 +1552,28 @@ float_test! {
15421552
assert_biteq!(Float::from_bits(masked_nan2), Float::from_bits(masked_nan2));
15431553
}
15441554
}
1555+
1556+
float_test! {
1557+
name: mul_add,
1558+
attrs: {
1559+
f16: #[cfg(any(miri, target_has_reliable_f16))],
1560+
// FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/
1561+
f32: #[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)],
1562+
f64: #[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)],
1563+
f128: #[cfg(any(miri, target_has_reliable_f128))],
1564+
},
1565+
test<Float> {
1566+
let nan: Float = Float::NAN;
1567+
let inf: Float = Float::INFINITY;
1568+
let neg_inf: Float = Float::NEG_INFINITY;
1569+
assert_biteq!(flt(12.3).mul_add(4.5, 6.7), Float::MUL_ADD_RESULT);
1570+
assert_biteq!((flt(-12.3)).mul_add(-4.5, -6.7), Float::NEG_MUL_ADD_RESULT);
1571+
assert_biteq!(flt(0.0).mul_add(8.9, 1.2), 1.2);
1572+
assert_biteq!(flt(3.4).mul_add(-0.0, 5.6), 5.6);
1573+
assert!(nan.mul_add(7.8, 9.0).is_nan());
1574+
assert_biteq!(inf.mul_add(7.8, 9.0), inf);
1575+
assert_biteq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1576+
assert_biteq!(flt(8.9).mul_add(inf, 3.2), inf);
1577+
assert_biteq!((flt(-3.2)).mul_add(2.4, neg_inf), neg_inf);
1578+
}
1579+
}

0 commit comments

Comments
 (0)