Skip to content

Commit eb00ec4

Browse files
committed
---
yaml --- r: 79795 b: refs/heads/try c: 0fcb859 h: refs/heads/master i: 79793: 9475c25 79791: c6871d0 v: v3
1 parent 4dcff01 commit eb00ec4

File tree

12 files changed

+412
-384
lines changed

12 files changed

+412
-384
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: ba9fa89bfb4aae53db93e9ecac31807af96356fc
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 54ae2800ffb30513f89ce13d27ac3c8d095d98ac
5-
refs/heads/try: 5591dce52eb35730e89070c7e104e1f1bf0a8ab3
5+
refs/heads/try: 0fcb85997db852350c104b40e3d7c78214bb0708
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/num/i16.rs

Lines changed: 32 additions & 1 deletion
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+
}

branches/try/src/libstd/num/i32.rs

Lines changed: 32 additions & 1 deletion
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+
}

branches/try/src/libstd/num/i64.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i64`
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,35 @@ impl BitCount for i64 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
3233
}
34+
35+
impl CheckedAdd for i64 {
36+
#[inline]
37+
fn checked_add(&self, v: &i64) -> Option<i64> {
38+
unsafe {
39+
let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i64 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i64) -> Option<i64> {
48+
unsafe {
49+
let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
// FIXME: #8449: should not be disabled on 32-bit
56+
#[cfg(target_word_size = "64")]
57+
impl CheckedMul for i64 {
58+
#[inline]
59+
fn checked_mul(&self, v: &i64) -> Option<i64> {
60+
unsafe {
61+
let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
62+
if y { None } else { Some(x) }
63+
}
64+
}
65+
}

branches/try/src/libstd/num/i8.rs

Lines changed: 32 additions & 1 deletion
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+
}

branches/try/src/libstd/num/int.rs

Lines changed: 69 additions & 1 deletion
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)