(index<- ) ./libcore/num/int.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-2014 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 architecture-sized signed integers (`int` 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 #[cfg(target_word_size = "32")] int_module!(int, 32)
27 #[cfg(target_word_size = "64")] int_module!(int, 64)
28
29 #[cfg(target_word_size = "32")]
30 impl Bitwise for int {
31 /// Returns the number of ones in the binary representation of the number.
32 #[inline]
33 fn count_ones(&self) -> int { (*self as i32).count_ones() as int }
34
35 /// Returns the number of leading zeros in the in the binary representation
36 /// of the number.
37 #[inline]
38 fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
39
40 /// Returns the number of trailing zeros in the in the binary representation
41 /// of the number.
42 #[inline]
43 fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
44 }
45
46 #[cfg(target_word_size = "64")]
47 impl Bitwise for int {
48 /// Returns the number of ones in the binary representation of the number.
49 #[inline]
50 fn count_ones(&self) -> int { (*self as i64).count_ones() as int }
51
52 /// Returns the number of leading zeros in the in the binary representation
53 /// of the number.
54 #[inline]
55 fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
56
57 /// Returns the number of trailing zeros in the in the binary representation
58 /// of the number.
59 #[inline]
60 fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
61 }
62
63 #[cfg(target_word_size = "32")]
64 impl CheckedAdd for int {
65 #[inline]
66 fn checked_add(&self, v: &int) -> Option<int> {
67 unsafe {
68 let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
69 if y { None } else { Some(x as int) }
70 }
71 }
72 }
73
74 #[cfg(target_word_size = "64")]
75 impl CheckedAdd for int {
76 #[inline]
77 fn checked_add(&self, v: &int) -> Option<int> {
78 unsafe {
79 let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
80 if y { None } else { Some(x as int) }
81 }
82 }
83 }
84
85 #[cfg(target_word_size = "32")]
86 impl CheckedSub for int {
87 #[inline]
88 fn checked_sub(&self, v: &int) -> Option<int> {
89 unsafe {
90 let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
91 if y { None } else { Some(x as int) }
92 }
93 }
94 }
95
96 #[cfg(target_word_size = "64")]
97 impl CheckedSub for int {
98 #[inline]
99 fn checked_sub(&self, v: &int) -> Option<int> {
100 unsafe {
101 let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
102 if y { None } else { Some(x as int) }
103 }
104 }
105 }
106
107 #[cfg(target_word_size = "32")]
108 impl CheckedMul for int {
109 #[inline]
110 fn checked_mul(&self, v: &int) -> Option<int> {
111 unsafe {
112 let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
113 if y { None } else { Some(x as int) }
114 }
115 }
116 }
117
118 #[cfg(target_word_size = "64")]
119 impl CheckedMul for int {
120 #[inline]
121 fn checked_mul(&self, v: &int) -> Option<int> {
122 unsafe {
123 let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
124 if y { None } else { Some(x as int) }
125 }
126 }
127 }