-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #59148 - lcnr:unchecked_maths, r=eddyb
add support for unchecked math add compiler support for ```rust /// Returns the result of an unchecked addition, resulting in /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`. pub fn unchecked_add<T>(x: T, y: T) -> T; /// Returns the result of an unchecked substraction, resulting in /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`. pub fn unchecked_sub<T>(x: T, y: T) -> T; /// Returns the result of an unchecked multiplication, resulting in /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`. pub fn unchecked_mul<T>(x: T, y: T) -> T; ``` cc rust-lang/rfcs#2508
- Loading branch information
Showing
11 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![crate_type = "lib"] | ||
#![feature(core_intrinsics)] | ||
|
||
use std::intrinsics::*; | ||
|
||
// CHECK-LABEL: @unchecked_add_signed | ||
#[no_mangle] | ||
pub unsafe fn unchecked_add_signed(a: i32, b: i32) -> i32 { | ||
// CHECK: add nsw | ||
unchecked_add(a, b) | ||
} | ||
|
||
// CHECK-LABEL: @unchecked_add_unsigned | ||
#[no_mangle] | ||
pub unsafe fn unchecked_add_unsigned(a: u32, b: u32) -> u32 { | ||
// CHECK: add nuw | ||
unchecked_add(a, b) | ||
} | ||
|
||
// CHECK-LABEL: @unchecked_sub_signed | ||
#[no_mangle] | ||
pub unsafe fn unchecked_sub_signed(a: i32, b: i32) -> i32 { | ||
// CHECK: sub nsw | ||
unchecked_sub(a, b) | ||
} | ||
|
||
// CHECK-LABEL: @unchecked_sub_unsigned | ||
#[no_mangle] | ||
pub unsafe fn unchecked_sub_unsigned(a: u32, b: u32) -> u32 { | ||
// CHECK: sub nuw | ||
unchecked_sub(a, b) | ||
} | ||
|
||
// CHECK-LABEL: @unchecked_mul_signed | ||
#[no_mangle] | ||
pub unsafe fn unchecked_mul_signed(a: i32, b: i32) -> i32 { | ||
// CHECK: mul nsw | ||
unchecked_mul(a, b) | ||
} | ||
|
||
// CHECK-LABEL: @unchecked_mul_unsigned | ||
#[no_mangle] | ||
pub unsafe fn unchecked_mul_unsigned(a: u32, b: u32) -> u32 { | ||
// CHECK: mul nuw | ||
unchecked_mul(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![feature(core_intrinsics)] | ||
|
||
fn main() { | ||
let (x, y) = (1u32, 2u32); | ||
let add = std::intrinsics::unchecked_add(x, y); //~ ERROR call to unsafe function | ||
let sub = std::intrinsics::unchecked_sub(x, y); //~ ERROR call to unsafe function | ||
let mul = std::intrinsics::unchecked_mul(x, y); //~ ERROR call to unsafe function | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | ||
--> $DIR/unchecked_math_unsafe.rs:5:15 | ||
| | ||
LL | let add = std::intrinsics::unchecked_add(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | ||
--> $DIR/unchecked_math_unsafe.rs:6:15 | ||
| | ||
LL | let sub = std::intrinsics::unchecked_sub(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | ||
--> $DIR/unchecked_math_unsafe.rs:7:15 | ||
| | ||
LL | let mul = std::intrinsics::unchecked_mul(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0133`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
let (x, y) = (1u32, 2u32); | ||
unsafe { | ||
let add = std::intrinsics::unchecked_add(x, y); //~ ERROR use of unstable library feature | ||
let sub = std::intrinsics::unchecked_sub(x, y); //~ ERROR use of unstable library feature | ||
let mul = std::intrinsics::unchecked_mul(x, y); //~ ERROR use of unstable library feature | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library | ||
--> $DIR/unchecked_math_unstable.rs:4:19 | ||
| | ||
LL | let add = std::intrinsics::unchecked_add(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(core_intrinsics)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library | ||
--> $DIR/unchecked_math_unstable.rs:5:19 | ||
| | ||
LL | let sub = std::intrinsics::unchecked_sub(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(core_intrinsics)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library | ||
--> $DIR/unchecked_math_unstable.rs:6:19 | ||
| | ||
LL | let mul = std::intrinsics::unchecked_mul(x, y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(core_intrinsics)] to the crate attributes to enable | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |