Skip to content

Commit 82ccba1

Browse files
committed
Make algebraic intrinsics into 'const fn' items; Make algebraic functions of 'f16', 'f32', 'f64', and 'f128' into 'const fn' items;
1 parent 6bc57c6 commit 82ccba1

File tree

7 files changed

+71
-51
lines changed

7 files changed

+71
-51
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+26
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
158158
self.copy_op(&val, dest)?;
159159
}
160160

161+
sym::fadd_algebraic
162+
| sym::fsub_algebraic
163+
| sym::fmul_algebraic
164+
| sym::fdiv_algebraic
165+
| sym::frem_algebraic => {
166+
let a = self.read_immediate(&args[0])?;
167+
let b = self.read_immediate(&args[1])?;
168+
169+
let op = match intrinsic_name {
170+
sym::fadd_algebraic => BinOp::Add,
171+
sym::fsub_algebraic => BinOp::Sub,
172+
sym::fmul_algebraic => BinOp::Mul,
173+
sym::fdiv_algebraic => BinOp::Div,
174+
sym::frem_algebraic => BinOp::Rem,
175+
176+
_ => bug!(),
177+
};
178+
179+
let res = self.binary_op(op, &a, &b)?;
180+
// `binary_op` already called `generate_nan` if needed.
181+
182+
// FIXME: Tests do likely benefit from us adding some randomness to the result here to catch any dependences on exact computations. This has previously been done, but the behaviour was removed as part of constification.
183+
184+
self.write_immediate(*res, dest)?;
185+
}
186+
161187
sym::ctpop
162188
| sym::cttz
163189
| sym::cttz_nonzero

library/core/src/intrinsics/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2429,35 +2429,35 @@ pub unsafe fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> In
24292429
/// Stabilized as [`f16::algebraic_add`], [`f32::algebraic_add`], [`f64::algebraic_add`] and [`f128::algebraic_add`].
24302430
#[rustc_nounwind]
24312431
#[rustc_intrinsic]
2432-
pub fn fadd_algebraic<T: Copy>(a: T, b: T) -> T;
2432+
pub const fn fadd_algebraic<T: Copy>(a: T, b: T) -> T;
24332433

24342434
/// Float subtraction that allows optimizations based on algebraic rules.
24352435
///
24362436
/// Stabilized as [`f16::algebraic_sub`], [`f32::algebraic_sub`], [`f64::algebraic_sub`] and [`f128::algebraic_sub`].
24372437
#[rustc_nounwind]
24382438
#[rustc_intrinsic]
2439-
pub fn fsub_algebraic<T: Copy>(a: T, b: T) -> T;
2439+
pub const fn fsub_algebraic<T: Copy>(a: T, b: T) -> T;
24402440

24412441
/// Float multiplication that allows optimizations based on algebraic rules.
24422442
///
24432443
/// Stabilized as [`f16::algebraic_mul`], [`f32::algebraic_mul`], [`f64::algebraic_mul`] and [`f128::algebraic_mul`].
24442444
#[rustc_nounwind]
24452445
#[rustc_intrinsic]
2446-
pub fn fmul_algebraic<T: Copy>(a: T, b: T) -> T;
2446+
pub const fn fmul_algebraic<T: Copy>(a: T, b: T) -> T;
24472447

24482448
/// Float division that allows optimizations based on algebraic rules.
24492449
///
24502450
/// Stabilized as [`f16::algebraic_div`], [`f32::algebraic_div`], [`f64::algebraic_div`] and [`f128::algebraic_div`].
24512451
#[rustc_nounwind]
24522452
#[rustc_intrinsic]
2453-
pub fn fdiv_algebraic<T: Copy>(a: T, b: T) -> T;
2453+
pub const fn fdiv_algebraic<T: Copy>(a: T, b: T) -> T;
24542454

24552455
/// Float remainder that allows optimizations based on algebraic rules.
24562456
///
24572457
/// Stabilized as [`f16::algebraic_rem`], [`f32::algebraic_rem`], [`f64::algebraic_rem`] and [`f128::algebraic_rem`].
24582458
#[rustc_nounwind]
24592459
#[rustc_intrinsic]
2460-
pub fn frem_algebraic<T: Copy>(a: T, b: T) -> T;
2460+
pub const fn frem_algebraic<T: Copy>(a: T, b: T) -> T;
24612461

24622462
/// Returns the number of bits set in an integer type `T`
24632463
///

library/core/src/num/f128.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1370,8 +1370,9 @@ impl f128 {
13701370
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13711371
#[must_use = "method returns a new number and does not mutate the original value"]
13721372
#[unstable(feature = "float_algebraic", issue = "136469")]
1373+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13731374
#[inline]
1374-
pub fn algebraic_add(self, rhs: f128) -> f128 {
1375+
pub const fn algebraic_add(self, rhs: f128) -> f128 {
13751376
intrinsics::fadd_algebraic(self, rhs)
13761377
}
13771378

@@ -1380,8 +1381,9 @@ impl f128 {
13801381
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13811382
#[must_use = "method returns a new number and does not mutate the original value"]
13821383
#[unstable(feature = "float_algebraic", issue = "136469")]
1384+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13831385
#[inline]
1384-
pub fn algebraic_sub(self, rhs: f128) -> f128 {
1386+
pub const fn algebraic_sub(self, rhs: f128) -> f128 {
13851387
intrinsics::fsub_algebraic(self, rhs)
13861388
}
13871389

@@ -1390,8 +1392,9 @@ impl f128 {
13901392
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13911393
#[must_use = "method returns a new number and does not mutate the original value"]
13921394
#[unstable(feature = "float_algebraic", issue = "136469")]
1395+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13931396
#[inline]
1394-
pub fn algebraic_mul(self, rhs: f128) -> f128 {
1397+
pub const fn algebraic_mul(self, rhs: f128) -> f128 {
13951398
intrinsics::fmul_algebraic(self, rhs)
13961399
}
13971400

@@ -1400,8 +1403,9 @@ impl f128 {
14001403
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14011404
#[must_use = "method returns a new number and does not mutate the original value"]
14021405
#[unstable(feature = "float_algebraic", issue = "136469")]
1406+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14031407
#[inline]
1404-
pub fn algebraic_div(self, rhs: f128) -> f128 {
1408+
pub const fn algebraic_div(self, rhs: f128) -> f128 {
14051409
intrinsics::fdiv_algebraic(self, rhs)
14061410
}
14071411

@@ -1410,8 +1414,9 @@ impl f128 {
14101414
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14111415
#[must_use = "method returns a new number and does not mutate the original value"]
14121416
#[unstable(feature = "float_algebraic", issue = "136469")]
1417+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14131418
#[inline]
1414-
pub fn algebraic_rem(self, rhs: f128) -> f128 {
1419+
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
14151420
intrinsics::frem_algebraic(self, rhs)
14161421
}
14171422
}

library/core/src/num/f16.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,9 @@ impl f16 {
13461346
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13471347
#[must_use = "method returns a new number and does not mutate the original value"]
13481348
#[unstable(feature = "float_algebraic", issue = "136469")]
1349+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13491350
#[inline]
1350-
pub fn algebraic_add(self, rhs: f16) -> f16 {
1351+
pub const fn algebraic_add(self, rhs: f16) -> f16 {
13511352
intrinsics::fadd_algebraic(self, rhs)
13521353
}
13531354

@@ -1356,8 +1357,9 @@ impl f16 {
13561357
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13571358
#[must_use = "method returns a new number and does not mutate the original value"]
13581359
#[unstable(feature = "float_algebraic", issue = "136469")]
1360+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13591361
#[inline]
1360-
pub fn algebraic_sub(self, rhs: f16) -> f16 {
1362+
pub const fn algebraic_sub(self, rhs: f16) -> f16 {
13611363
intrinsics::fsub_algebraic(self, rhs)
13621364
}
13631365

@@ -1366,8 +1368,9 @@ impl f16 {
13661368
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13671369
#[must_use = "method returns a new number and does not mutate the original value"]
13681370
#[unstable(feature = "float_algebraic", issue = "136469")]
1371+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13691372
#[inline]
1370-
pub fn algebraic_mul(self, rhs: f16) -> f16 {
1373+
pub const fn algebraic_mul(self, rhs: f16) -> f16 {
13711374
intrinsics::fmul_algebraic(self, rhs)
13721375
}
13731376

@@ -1376,8 +1379,9 @@ impl f16 {
13761379
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13771380
#[must_use = "method returns a new number and does not mutate the original value"]
13781381
#[unstable(feature = "float_algebraic", issue = "136469")]
1382+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13791383
#[inline]
1380-
pub fn algebraic_div(self, rhs: f16) -> f16 {
1384+
pub const fn algebraic_div(self, rhs: f16) -> f16 {
13811385
intrinsics::fdiv_algebraic(self, rhs)
13821386
}
13831387

@@ -1386,8 +1390,9 @@ impl f16 {
13861390
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13871391
#[must_use = "method returns a new number and does not mutate the original value"]
13881392
#[unstable(feature = "float_algebraic", issue = "136469")]
1393+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13891394
#[inline]
1390-
pub fn algebraic_rem(self, rhs: f16) -> f16 {
1395+
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
13911396
intrinsics::frem_algebraic(self, rhs)
13921397
}
13931398
}

library/core/src/num/f32.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,9 @@ impl f32 {
15121512
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15131513
#[must_use = "method returns a new number and does not mutate the original value"]
15141514
#[unstable(feature = "float_algebraic", issue = "136469")]
1515+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15151516
#[inline]
1516-
pub fn algebraic_add(self, rhs: f32) -> f32 {
1517+
pub const fn algebraic_add(self, rhs: f32) -> f32 {
15171518
intrinsics::fadd_algebraic(self, rhs)
15181519
}
15191520

@@ -1522,8 +1523,9 @@ impl f32 {
15221523
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15231524
#[must_use = "method returns a new number and does not mutate the original value"]
15241525
#[unstable(feature = "float_algebraic", issue = "136469")]
1526+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15251527
#[inline]
1526-
pub fn algebraic_sub(self, rhs: f32) -> f32 {
1528+
pub const fn algebraic_sub(self, rhs: f32) -> f32 {
15271529
intrinsics::fsub_algebraic(self, rhs)
15281530
}
15291531

@@ -1532,8 +1534,9 @@ impl f32 {
15321534
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15331535
#[must_use = "method returns a new number and does not mutate the original value"]
15341536
#[unstable(feature = "float_algebraic", issue = "136469")]
1537+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15351538
#[inline]
1536-
pub fn algebraic_mul(self, rhs: f32) -> f32 {
1539+
pub const fn algebraic_mul(self, rhs: f32) -> f32 {
15371540
intrinsics::fmul_algebraic(self, rhs)
15381541
}
15391542

@@ -1542,8 +1545,9 @@ impl f32 {
15421545
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15431546
#[must_use = "method returns a new number and does not mutate the original value"]
15441547
#[unstable(feature = "float_algebraic", issue = "136469")]
1548+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15451549
#[inline]
1546-
pub fn algebraic_div(self, rhs: f32) -> f32 {
1550+
pub const fn algebraic_div(self, rhs: f32) -> f32 {
15471551
intrinsics::fdiv_algebraic(self, rhs)
15481552
}
15491553

@@ -1552,8 +1556,9 @@ impl f32 {
15521556
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15531557
#[must_use = "method returns a new number and does not mutate the original value"]
15541558
#[unstable(feature = "float_algebraic", issue = "136469")]
1559+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15551560
#[inline]
1556-
pub fn algebraic_rem(self, rhs: f32) -> f32 {
1561+
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
15571562
intrinsics::frem_algebraic(self, rhs)
15581563
}
15591564
}

library/core/src/num/f64.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,9 @@ impl f64 {
15111511
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15121512
#[must_use = "method returns a new number and does not mutate the original value"]
15131513
#[unstable(feature = "float_algebraic", issue = "136469")]
1514+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15141515
#[inline]
1515-
pub fn algebraic_add(self, rhs: f64) -> f64 {
1516+
pub const fn algebraic_add(self, rhs: f64) -> f64 {
15161517
intrinsics::fadd_algebraic(self, rhs)
15171518
}
15181519

@@ -1521,8 +1522,9 @@ impl f64 {
15211522
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15221523
#[must_use = "method returns a new number and does not mutate the original value"]
15231524
#[unstable(feature = "float_algebraic", issue = "136469")]
1525+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15241526
#[inline]
1525-
pub fn algebraic_sub(self, rhs: f64) -> f64 {
1527+
pub const fn algebraic_sub(self, rhs: f64) -> f64 {
15261528
intrinsics::fsub_algebraic(self, rhs)
15271529
}
15281530

@@ -1531,8 +1533,9 @@ impl f64 {
15311533
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15321534
#[must_use = "method returns a new number and does not mutate the original value"]
15331535
#[unstable(feature = "float_algebraic", issue = "136469")]
1536+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15341537
#[inline]
1535-
pub fn algebraic_mul(self, rhs: f64) -> f64 {
1538+
pub const fn algebraic_mul(self, rhs: f64) -> f64 {
15361539
intrinsics::fmul_algebraic(self, rhs)
15371540
}
15381541

@@ -1541,8 +1544,9 @@ impl f64 {
15411544
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15421545
#[must_use = "method returns a new number and does not mutate the original value"]
15431546
#[unstable(feature = "float_algebraic", issue = "136469")]
1547+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15441548
#[inline]
1545-
pub fn algebraic_div(self, rhs: f64) -> f64 {
1549+
pub const fn algebraic_div(self, rhs: f64) -> f64 {
15461550
intrinsics::fdiv_algebraic(self, rhs)
15471551
}
15481552

@@ -1551,8 +1555,9 @@ impl f64 {
15511555
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15521556
#[must_use = "method returns a new number and does not mutate the original value"]
15531557
#[unstable(feature = "float_algebraic", issue = "136469")]
1558+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15541559
#[inline]
1555-
pub fn algebraic_rem(self, rhs: f64) -> f64 {
1560+
pub const fn algebraic_rem(self, rhs: f64) -> f64 {
15561561
intrinsics::frem_algebraic(self, rhs)
15571562
}
15581563
}

src/tools/miri/src/intrinsics/mod.rs

-26
Original file line numberDiff line numberDiff line change
@@ -391,32 +391,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
391391
this.write_scalar(res, dest)?;
392392
}
393393

394-
#[rustfmt::skip]
395-
| "fadd_algebraic"
396-
| "fsub_algebraic"
397-
| "fmul_algebraic"
398-
| "fdiv_algebraic"
399-
| "frem_algebraic"
400-
=> {
401-
let [a, b] = check_intrinsic_arg_count(args)?;
402-
let a = this.read_immediate(a)?;
403-
let b = this.read_immediate(b)?;
404-
let op = match intrinsic_name {
405-
"fadd_algebraic" => mir::BinOp::Add,
406-
"fsub_algebraic" => mir::BinOp::Sub,
407-
"fmul_algebraic" => mir::BinOp::Mul,
408-
"fdiv_algebraic" => mir::BinOp::Div,
409-
"frem_algebraic" => mir::BinOp::Rem,
410-
_ => bug!(),
411-
};
412-
let res = this.binary_op(op, &a, &b)?;
413-
// `binary_op` already called `generate_nan` if needed.
414-
// Apply a relative error of 4ULP to simulate non-deterministic precision loss
415-
// due to optimizations.
416-
let res = apply_random_float_error_to_imm(this, res, 2 /* log2(4) */)?;
417-
this.write_immediate(*res, dest)?;
418-
}
419-
420394
#[rustfmt::skip]
421395
| "fadd_fast"
422396
| "fsub_fast"

0 commit comments

Comments
 (0)