]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/i8.rs
Convert most code to new inner attribute syntax.
[rust.git] / src / libstd / num / i8.rs
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 8-bits integers (`i8` 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 int_module!(i8, 8)
27
28 impl Bitwise for i8 {
29     /// Returns the number of ones in the binary representation of the number.
30     #[inline]
31     fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
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) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
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) -> i8 { unsafe { intrinsics::cttz8(*self) } }
42 }
43
44 impl CheckedAdd for i8 {
45     #[inline]
46     fn checked_add(&self, v: &i8) -> Option<i8> {
47         unsafe {
48             let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
49             if y { None } else { Some(x) }
50         }
51     }
52 }
53
54 impl CheckedSub for i8 {
55     #[inline]
56     fn checked_sub(&self, v: &i8) -> Option<i8> {
57         unsafe {
58             let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
59             if y { None } else { Some(x) }
60         }
61     }
62 }
63
64 impl CheckedMul for i8 {
65     #[inline]
66     fn checked_mul(&self, v: &i8) -> Option<i8> {
67         unsafe {
68             let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
69             if y { None } else { Some(x) }
70         }
71     }
72 }