Skip to content

Commit 55e1af9

Browse files
authored
Unrolled build for rust-lang#140172
Rollup merge of rust-lang#140172 - bjoernager:const-float-algebraic, r=RalfJung Make algebraic functions into `const fn` items. Tracking issue: rust-lang#136469 This PR makes the algebraic intrinsics and the unstable, algebraic functions of `f16`, `f32`, `f64`, and `f128` into `const fn` items: ```rust impl f16 { pub const fn algebraic_add(self, rhs: f16) -> f16; pub const fn algebraic_sub(self, rhs: f16) -> f16; pub const fn algebraic_mul(self, rhs: f16) -> f16; pub const fn algebraic_div(self, rhs: f16) -> f16; pub const fn algebraic_rem(self, rhs: f16) -> f16; } impl f32 { pub const fn algebraic_add(self, rhs: f32) -> f32; pub const fn algebraic_sub(self, rhs: f32) -> f32; pub const fn algebraic_mul(self, rhs: f32) -> f32; pub const fn algebraic_div(self, rhs: f32) -> f32; pub const fn algebraic_rem(self, rhs: f32) -> f32; } impl f64 { pub const fn algebraic_add(self, rhs: f64) -> f64; pub const fn algebraic_sub(self, rhs: f64) -> f64; pub const fn algebraic_mul(self, rhs: f64) -> f64; pub const fn algebraic_div(self, rhs: f64) -> f64; pub const fn algebraic_rem(self, rhs: f64) -> f64; } impl f128 { pub const fn algebraic_add(self, rhs: f128) -> f128; pub const fn algebraic_sub(self, rhs: f128) -> f128; pub const fn algebraic_mul(self, rhs: f128) -> f128; pub const fn algebraic_div(self, rhs: f128) -> f128; pub const fn algebraic_rem(self, rhs: f128) -> f128; } // core::intrinsics pub const fn fadd_algebraic<T: Copy>(a: T, b: T) -> T; pub const fn fsub_algebraic<T: Copy>(a: T, b: T) -> T; pub const fn fmul_algebraic<T: Copy>(a: T, b: T) -> T; pub const fn fdiv_algebraic<T: Copy>(a: T, b: T) -> T; pub const fn frem_algebraic<T: Copy>(a: T, b: T) -> T; ``` This PR does not preserve the initial behaviour of these functions yielding non-deterministic output under Miri; it is most likely desired to reimplement this behaviour at some point.
2 parents 3c877f6 + 0296f05 commit 55e1af9

File tree

7 files changed

+70
-51
lines changed

7 files changed

+70
-51
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+25
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ 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: Miri should add some non-determinism 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+
self.write_immediate(*res, dest)?;
184+
}
185+
161186
sym::ctpop
162187
| sym::cttz
163188
| 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
@@ -1374,8 +1374,9 @@ impl f128 {
13741374
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13751375
#[must_use = "method returns a new number and does not mutate the original value"]
13761376
#[unstable(feature = "float_algebraic", issue = "136469")]
1377+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13771378
#[inline]
1378-
pub fn algebraic_add(self, rhs: f128) -> f128 {
1379+
pub const fn algebraic_add(self, rhs: f128) -> f128 {
13791380
intrinsics::fadd_algebraic(self, rhs)
13801381
}
13811382

@@ -1384,8 +1385,9 @@ impl f128 {
13841385
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13851386
#[must_use = "method returns a new number and does not mutate the original value"]
13861387
#[unstable(feature = "float_algebraic", issue = "136469")]
1388+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13871389
#[inline]
1388-
pub fn algebraic_sub(self, rhs: f128) -> f128 {
1390+
pub const fn algebraic_sub(self, rhs: f128) -> f128 {
13891391
intrinsics::fsub_algebraic(self, rhs)
13901392
}
13911393

@@ -1394,8 +1396,9 @@ impl f128 {
13941396
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13951397
#[must_use = "method returns a new number and does not mutate the original value"]
13961398
#[unstable(feature = "float_algebraic", issue = "136469")]
1399+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13971400
#[inline]
1398-
pub fn algebraic_mul(self, rhs: f128) -> f128 {
1401+
pub const fn algebraic_mul(self, rhs: f128) -> f128 {
13991402
intrinsics::fmul_algebraic(self, rhs)
14001403
}
14011404

@@ -1404,8 +1407,9 @@ impl f128 {
14041407
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14051408
#[must_use = "method returns a new number and does not mutate the original value"]
14061409
#[unstable(feature = "float_algebraic", issue = "136469")]
1410+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14071411
#[inline]
1408-
pub fn algebraic_div(self, rhs: f128) -> f128 {
1412+
pub const fn algebraic_div(self, rhs: f128) -> f128 {
14091413
intrinsics::fdiv_algebraic(self, rhs)
14101414
}
14111415

@@ -1414,8 +1418,9 @@ impl f128 {
14141418
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14151419
#[must_use = "method returns a new number and does not mutate the original value"]
14161420
#[unstable(feature = "float_algebraic", issue = "136469")]
1421+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14171422
#[inline]
1418-
pub fn algebraic_rem(self, rhs: f128) -> f128 {
1423+
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
14191424
intrinsics::frem_algebraic(self, rhs)
14201425
}
14211426
}

library/core/src/num/f16.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,9 @@ impl f16 {
13501350
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13511351
#[must_use = "method returns a new number and does not mutate the original value"]
13521352
#[unstable(feature = "float_algebraic", issue = "136469")]
1353+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13531354
#[inline]
1354-
pub fn algebraic_add(self, rhs: f16) -> f16 {
1355+
pub const fn algebraic_add(self, rhs: f16) -> f16 {
13551356
intrinsics::fadd_algebraic(self, rhs)
13561357
}
13571358

@@ -1360,8 +1361,9 @@ impl f16 {
13601361
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13611362
#[must_use = "method returns a new number and does not mutate the original value"]
13621363
#[unstable(feature = "float_algebraic", issue = "136469")]
1364+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13631365
#[inline]
1364-
pub fn algebraic_sub(self, rhs: f16) -> f16 {
1366+
pub const fn algebraic_sub(self, rhs: f16) -> f16 {
13651367
intrinsics::fsub_algebraic(self, rhs)
13661368
}
13671369

@@ -1370,8 +1372,9 @@ impl f16 {
13701372
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13711373
#[must_use = "method returns a new number and does not mutate the original value"]
13721374
#[unstable(feature = "float_algebraic", issue = "136469")]
1375+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13731376
#[inline]
1374-
pub fn algebraic_mul(self, rhs: f16) -> f16 {
1377+
pub const fn algebraic_mul(self, rhs: f16) -> f16 {
13751378
intrinsics::fmul_algebraic(self, rhs)
13761379
}
13771380

@@ -1380,8 +1383,9 @@ impl f16 {
13801383
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13811384
#[must_use = "method returns a new number and does not mutate the original value"]
13821385
#[unstable(feature = "float_algebraic", issue = "136469")]
1386+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13831387
#[inline]
1384-
pub fn algebraic_div(self, rhs: f16) -> f16 {
1388+
pub const fn algebraic_div(self, rhs: f16) -> f16 {
13851389
intrinsics::fdiv_algebraic(self, rhs)
13861390
}
13871391

@@ -1390,8 +1394,9 @@ impl f16 {
13901394
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13911395
#[must_use = "method returns a new number and does not mutate the original value"]
13921396
#[unstable(feature = "float_algebraic", issue = "136469")]
1397+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13931398
#[inline]
1394-
pub fn algebraic_rem(self, rhs: f16) -> f16 {
1399+
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
13951400
intrinsics::frem_algebraic(self, rhs)
13961401
}
13971402
}

library/core/src/num/f32.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1516,8 +1516,9 @@ impl f32 {
15161516
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15171517
#[must_use = "method returns a new number and does not mutate the original value"]
15181518
#[unstable(feature = "float_algebraic", issue = "136469")]
1519+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15191520
#[inline]
1520-
pub fn algebraic_add(self, rhs: f32) -> f32 {
1521+
pub const fn algebraic_add(self, rhs: f32) -> f32 {
15211522
intrinsics::fadd_algebraic(self, rhs)
15221523
}
15231524

@@ -1526,8 +1527,9 @@ impl f32 {
15261527
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15271528
#[must_use = "method returns a new number and does not mutate the original value"]
15281529
#[unstable(feature = "float_algebraic", issue = "136469")]
1530+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15291531
#[inline]
1530-
pub fn algebraic_sub(self, rhs: f32) -> f32 {
1532+
pub const fn algebraic_sub(self, rhs: f32) -> f32 {
15311533
intrinsics::fsub_algebraic(self, rhs)
15321534
}
15331535

@@ -1536,8 +1538,9 @@ impl f32 {
15361538
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15371539
#[must_use = "method returns a new number and does not mutate the original value"]
15381540
#[unstable(feature = "float_algebraic", issue = "136469")]
1541+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15391542
#[inline]
1540-
pub fn algebraic_mul(self, rhs: f32) -> f32 {
1543+
pub const fn algebraic_mul(self, rhs: f32) -> f32 {
15411544
intrinsics::fmul_algebraic(self, rhs)
15421545
}
15431546

@@ -1546,8 +1549,9 @@ impl f32 {
15461549
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15471550
#[must_use = "method returns a new number and does not mutate the original value"]
15481551
#[unstable(feature = "float_algebraic", issue = "136469")]
1552+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15491553
#[inline]
1550-
pub fn algebraic_div(self, rhs: f32) -> f32 {
1554+
pub const fn algebraic_div(self, rhs: f32) -> f32 {
15511555
intrinsics::fdiv_algebraic(self, rhs)
15521556
}
15531557

@@ -1556,8 +1560,9 @@ impl f32 {
15561560
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15571561
#[must_use = "method returns a new number and does not mutate the original value"]
15581562
#[unstable(feature = "float_algebraic", issue = "136469")]
1563+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15591564
#[inline]
1560-
pub fn algebraic_rem(self, rhs: f32) -> f32 {
1565+
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
15611566
intrinsics::frem_algebraic(self, rhs)
15621567
}
15631568
}

library/core/src/num/f64.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1515,8 +1515,9 @@ impl f64 {
15151515
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15161516
#[must_use = "method returns a new number and does not mutate the original value"]
15171517
#[unstable(feature = "float_algebraic", issue = "136469")]
1518+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15181519
#[inline]
1519-
pub fn algebraic_add(self, rhs: f64) -> f64 {
1520+
pub const fn algebraic_add(self, rhs: f64) -> f64 {
15201521
intrinsics::fadd_algebraic(self, rhs)
15211522
}
15221523

@@ -1525,8 +1526,9 @@ impl f64 {
15251526
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15261527
#[must_use = "method returns a new number and does not mutate the original value"]
15271528
#[unstable(feature = "float_algebraic", issue = "136469")]
1529+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15281530
#[inline]
1529-
pub fn algebraic_sub(self, rhs: f64) -> f64 {
1531+
pub const fn algebraic_sub(self, rhs: f64) -> f64 {
15301532
intrinsics::fsub_algebraic(self, rhs)
15311533
}
15321534

@@ -1535,8 +1537,9 @@ impl f64 {
15351537
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15361538
#[must_use = "method returns a new number and does not mutate the original value"]
15371539
#[unstable(feature = "float_algebraic", issue = "136469")]
1540+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15381541
#[inline]
1539-
pub fn algebraic_mul(self, rhs: f64) -> f64 {
1542+
pub const fn algebraic_mul(self, rhs: f64) -> f64 {
15401543
intrinsics::fmul_algebraic(self, rhs)
15411544
}
15421545

@@ -1545,8 +1548,9 @@ impl f64 {
15451548
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15461549
#[must_use = "method returns a new number and does not mutate the original value"]
15471550
#[unstable(feature = "float_algebraic", issue = "136469")]
1551+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15481552
#[inline]
1549-
pub fn algebraic_div(self, rhs: f64) -> f64 {
1553+
pub const fn algebraic_div(self, rhs: f64) -> f64 {
15501554
intrinsics::fdiv_algebraic(self, rhs)
15511555
}
15521556

@@ -1555,8 +1559,9 @@ impl f64 {
15551559
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15561560
#[must_use = "method returns a new number and does not mutate the original value"]
15571561
#[unstable(feature = "float_algebraic", issue = "136469")]
1562+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15581563
#[inline]
1559-
pub fn algebraic_rem(self, rhs: f64) -> f64 {
1564+
pub const fn algebraic_rem(self, rhs: f64) -> f64 {
15601565
intrinsics::frem_algebraic(self, rhs)
15611566
}
15621567
}

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)