Skip to content

Commit fd2488b

Browse files
committed
auto merge of #9051 : bjz/rust/master, r=huonw
2 parents 7e2827b + 2c31053 commit fd2488b

File tree

11 files changed

+419
-384
lines changed

11 files changed

+419
-384
lines changed

src/libstd/num/i16.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i16`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i16 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
3233
}
34+
35+
impl CheckedAdd for i16 {
36+
#[inline]
37+
fn checked_add(&self, v: &i16) -> Option<i16> {
38+
unsafe {
39+
let (x, y) = intrinsics::i16_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i16 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i16) -> Option<i16> {
48+
unsafe {
49+
let (x, y) = intrinsics::i16_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i16 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i16) -> Option<i16> {
58+
unsafe {
59+
let (x, y) = intrinsics::i16_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/i32.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i32`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i32 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
3233
}
34+
35+
impl CheckedAdd for i32 {
36+
#[inline]
37+
fn checked_add(&self, v: &i32) -> Option<i32> {
38+
unsafe {
39+
let (x, y) = intrinsics::i32_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i32 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i32) -> Option<i32> {
48+
unsafe {
49+
let (x, y) = intrinsics::i32_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i32 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i32) -> Option<i32> {
58+
unsafe {
59+
let (x, y) = intrinsics::i32_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/i64.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
//! Operations and constants for `i64`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub};
14+
#[cfg(target_word_size = "64")]
15+
use num::CheckedMul;
16+
use option::{Option, Some, None};
1417
use unstable::intrinsics;
1518

1619
pub use self::generated::*;
@@ -30,3 +33,35 @@ impl BitCount for i64 {
3033
#[inline]
3134
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
3235
}
36+
37+
impl CheckedAdd for i64 {
38+
#[inline]
39+
fn checked_add(&self, v: &i64) -> Option<i64> {
40+
unsafe {
41+
let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
42+
if y { None } else { Some(x) }
43+
}
44+
}
45+
}
46+
47+
impl CheckedSub for i64 {
48+
#[inline]
49+
fn checked_sub(&self, v: &i64) -> Option<i64> {
50+
unsafe {
51+
let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
52+
if y { None } else { Some(x) }
53+
}
54+
}
55+
}
56+
57+
// FIXME: #8449: should not be disabled on 32-bit
58+
#[cfg(target_word_size = "64")]
59+
impl CheckedMul for i64 {
60+
#[inline]
61+
fn checked_mul(&self, v: &i64) -> Option<i64> {
62+
unsafe {
63+
let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
64+
if y { None } else { Some(x) }
65+
}
66+
}
67+
}

src/libstd/num/i8.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i8`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i8 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
3233
}
34+
35+
impl CheckedAdd for i8 {
36+
#[inline]
37+
fn checked_add(&self, v: &i8) -> Option<i8> {
38+
unsafe {
39+
let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i8 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i8) -> Option<i8> {
48+
unsafe {
49+
let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i8 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i8) -> Option<i8> {
58+
unsafe {
59+
let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/int.rs

+69-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
1313
#[allow(non_uppercase_statics)];
1414

15-
use num::BitCount;
15+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
16+
use option::{Option, Some, None};
17+
use unstable::intrinsics;
1618

1719
pub use self::generated::*;
1820

@@ -51,6 +53,72 @@ impl BitCount for int {
5153
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
5254
}
5355

56+
#[cfg(target_word_size = "32")]
57+
impl CheckedAdd for int {
58+
#[inline]
59+
fn checked_add(&self, v: &int) -> Option<int> {
60+
unsafe {
61+
let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
62+
if y { None } else { Some(x as int) }
63+
}
64+
}
65+
}
66+
67+
#[cfg(target_word_size = "64")]
68+
impl CheckedAdd for int {
69+
#[inline]
70+
fn checked_add(&self, v: &int) -> Option<int> {
71+
unsafe {
72+
let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
73+
if y { None } else { Some(x as int) }
74+
}
75+
}
76+
}
77+
78+
#[cfg(target_word_size = "32")]
79+
impl CheckedSub for int {
80+
#[inline]
81+
fn checked_sub(&self, v: &int) -> Option<int> {
82+
unsafe {
83+
let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
84+
if y { None } else { Some(x as int) }
85+
}
86+
}
87+
}
88+
89+
#[cfg(target_word_size = "64")]
90+
impl CheckedSub for int {
91+
#[inline]
92+
fn checked_sub(&self, v: &int) -> Option<int> {
93+
unsafe {
94+
let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
95+
if y { None } else { Some(x as int) }
96+
}
97+
}
98+
}
99+
100+
#[cfg(target_word_size = "32")]
101+
impl CheckedMul for int {
102+
#[inline]
103+
fn checked_mul(&self, v: &int) -> Option<int> {
104+
unsafe {
105+
let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
106+
if y { None } else { Some(x as int) }
107+
}
108+
}
109+
}
110+
111+
#[cfg(target_word_size = "64")]
112+
impl CheckedMul for int {
113+
#[inline]
114+
fn checked_mul(&self, v: &int) -> Option<int> {
115+
unsafe {
116+
let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
117+
if y { None } else { Some(x as int) }
118+
}
119+
}
120+
}
121+
54122
/// Returns `base` raised to the power of `exponent`
55123
pub fn pow(base: int, exponent: uint) -> int {
56124
if exponent == 0u {

0 commit comments

Comments
 (0)