(index<- ) ./libcore/num/i32.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations and constants for signed 32-bits integers (`i32` type)
12
13 use default::Default;
14 use intrinsics;
15 use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
16 use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
17 use option::{Option, Some, None};
18
19 #[cfg(not(test))]
20 use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
21 #[cfg(not(test))]
22 use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
23 #[cfg(not(test))]
24 use ops::{Shl, Shr, Not};
25
26 int_module!(i32, 32)
27
28 impl Bitwise for i32 {
29 /// Returns the number of ones in the binary representation of the number.
30 #[inline]
31 fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self as u32) as i32 } }
32
33 /// Returns the number of leading zeros in the in the binary representation
34 /// of the number.
35 #[inline]
36 fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self as u32) as i32 } }
37
38 /// Returns the number of trailing zeros in the in the binary representation
39 /// of the number.
40 #[inline]
41 fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self as u32) as i32 } }
42 }
43
44 impl CheckedAdd for i32 {
45 #[inline]
46 fn checked_add(&self, v: &i32) -> Option<i32> {
47 unsafe {
48 let (x, y) = intrinsics::i32_add_with_overflow(*self, *v);
49 if y { None } else { Some(x) }
50 }
51 }
52 }
53
54 impl CheckedSub for i32 {
55 #[inline]
56 fn checked_sub(&self, v: &i32) -> Option<i32> {
57 unsafe {
58 let (x, y) = intrinsics::i32_sub_with_overflow(*self, *v);
59 if y { None } else { Some(x) }
60 }
61 }
62 }
63
64 impl CheckedMul for i32 {
65 #[inline]
66 fn checked_mul(&self, v: &i32) -> Option<i32> {
67 unsafe {
68 let (x, y) = intrinsics::i32_mul_with_overflow(*self, *v);
69 if y { None } else { Some(x) }
70 }
71 }
72 }