]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/int.rs
Convert most code to new inner attribute syntax.
[rust.git] / src / libstd / num / int.rs
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 #![allow(non_uppercase_statics)]
14
15 use prelude::*;
16
17 use default::Default;
18 use from_str::FromStr;
19 use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
20 use num::{CheckedDiv, Zero, One, strconv};
21 use num::{ToStrRadix, FromStrRadix};
22 use option::{Option, Some, None};
23 use str;
24 use intrinsics;
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 }