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.
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.
11 //! Operations and constants for architecture-sized signed integers (`int` type)
13 #![allow(non_uppercase_statics)]
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};
26 #[cfg(target_word_size = "32")] int_module!(int, 32)
27 #[cfg(target_word_size = "64")] int_module!(int, 64)
29 #[cfg(target_word_size = "32")]
30 impl Bitwise for int {
31 /// Returns the number of ones in the binary representation of the number.
33 fn count_ones(&self) -> int { (*self as i32).count_ones() as int }
35 /// Returns the number of leading zeros in the in the binary representation
38 fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
40 /// Returns the number of trailing zeros in the in the binary representation
43 fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
46 #[cfg(target_word_size = "64")]
47 impl Bitwise for int {
48 /// Returns the number of ones in the binary representation of the number.
50 fn count_ones(&self) -> int { (*self as i64).count_ones() as int }
52 /// Returns the number of leading zeros in the in the binary representation
55 fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
57 /// Returns the number of trailing zeros in the in the binary representation
60 fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
63 #[cfg(target_word_size = "32")]
64 impl CheckedAdd for int {
66 fn checked_add(&self, v: &int) -> Option<int> {
68 let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
69 if y { None } else { Some(x as int) }
74 #[cfg(target_word_size = "64")]
75 impl CheckedAdd for int {
77 fn checked_add(&self, v: &int) -> Option<int> {
79 let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
80 if y { None } else { Some(x as int) }
85 #[cfg(target_word_size = "32")]
86 impl CheckedSub for int {
88 fn checked_sub(&self, v: &int) -> Option<int> {
90 let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
91 if y { None } else { Some(x as int) }
96 #[cfg(target_word_size = "64")]
97 impl CheckedSub for int {
99 fn checked_sub(&self, v: &int) -> Option<int> {
101 let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
102 if y { None } else { Some(x as int) }
107 #[cfg(target_word_size = "32")]
108 impl CheckedMul for int {
110 fn checked_mul(&self, v: &int) -> Option<int> {
112 let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
113 if y { None } else { Some(x as int) }
118 #[cfg(target_word_size = "64")]
119 impl CheckedMul for int {
121 fn checked_mul(&self, v: &int) -> Option<int> {
123 let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
124 if y { None } else { Some(x as int) }