From cdfff9db35d037c51dfd5c2bac2174f651294adb Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Tue, 6 Jan 2015 00:56:30 -0500 Subject: rustc: implement arithmetic overflow checking Adds overflow checking to integer addition, multiplication, and subtraction when `-Z force-overflow-checks` is true, or if `--cfg ndebug` is not passed to the compiler. On overflow, it panics with `arithmetic operation overflowed`. Also adds `overflowing_add`, `overflowing_sub`, and `overflowing_mul` intrinsics for doing unchecked arithmetic. [breaking-change] --- src/libcore/intrinsics.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 1ca243134cc..ed129136091 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -546,3 +546,14 @@ extern "rust-intrinsic" { /// Performs checked `u64` multiplication. pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool); } + +// SNAP 880fb89 +#[cfg(not(stage0))] +extern "rust-intrinsic" { + /// Returns (a + b) mod 2^N, where N is the width of N in bits. + pub fn overflowing_add(a: T, b: T) -> T; + /// Returns (a - b) mod 2^N, where N is the width of N in bits. + pub fn overflowing_sub(a: T, b: T) -> T; + /// Returns (a * b) mod 2^N, where N is the width of N in bits. + pub fn overflowing_mul(a: T, b: T) -> T; +} -- cgit 1.4.1-3-g733a5