(index<- ) ./libcore/num/i16.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 16-bits integers (`i16` 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!(i16, 16)
27
28 impl Bitwise for i16 {
29 /// Returns the number of ones in the binary representation of the number.
30 #[inline]
31 fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self as u16) as i16 } }
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) -> i16 { unsafe { intrinsics::ctlz16(*self as u16) as i16 } }
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) -> i16 { unsafe { intrinsics::cttz16(*self as u16) as i16 } }
42 }
43
44 impl CheckedAdd for i16 {
45 #[inline]
46 fn checked_add(&self, v: &i16) -> Option<i16> {
47 unsafe {
48 let (x, y) = intrinsics::i16_add_with_overflow(*self, *v);
49 if y { None } else { Some(x) }
50 }
51 }
52 }
53
54 impl CheckedSub for i16 {
55 #[inline]
56 fn checked_sub(&self, v: &i16) -> Option<i16> {
57 unsafe {
58 let (x, y) = intrinsics::i16_sub_with_overflow(*self, *v);
59 if y { None } else { Some(x) }
60 }
61 }
62 }
63
64 impl CheckedMul for i16 {
65 #[inline]
66 fn checked_mul(&self, v: &i16) -> Option<i16> {
67 unsafe {
68 let (x, y) = intrinsics::i16_mul_with_overflow(*self, *v);
69 if y { None } else { Some(x) }
70 }
71 }
72 }