(index<- ) ./libcore/num/i64.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 64-bits integers (`i64` 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!(i64, 64)
27
28 impl Bitwise for i64 {
29 /// Returns the number of ones in the binary representation of the number.
30 #[inline]
31 fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self as u64) as i64 } }
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) -> i64 { unsafe { intrinsics::ctlz64(*self as u64) as i64 } }
37
38 /// Counts the number of trailing zeros.
39 #[inline]
40 fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self as u64) as i64 } }
41 }
42
43 impl CheckedAdd for i64 {
44 #[inline]
45 fn checked_add(&self, v: &i64) -> Option<i64> {
46 unsafe {
47 let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
48 if y { None } else { Some(x) }
49 }
50 }
51 }
52
53 impl CheckedSub for i64 {
54 #[inline]
55 fn checked_sub(&self, v: &i64) -> Option<i64> {
56 unsafe {
57 let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
58 if y { None } else { Some(x) }
59 }
60 }
61 }
62
63 impl CheckedMul for i64 {
64 #[inline]
65 fn checked_mul(&self, v: &i64) -> Option<i64> {
66 unsafe {
67 let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
68 if y { None } else { Some(x) }
69 }
70 }
71 }