Remove usage of the #[merge] hack with int modules
This commit is contained in:
parent
b5ab1012f1
commit
ec8fb884e9
23 changed files with 937 additions and 1145 deletions
|
@ -88,34 +88,24 @@ pub mod prelude;
|
||||||
|
|
||||||
/* Primitive types */
|
/* Primitive types */
|
||||||
|
|
||||||
#[path = "num/int-template.rs"] #[merge = "num/int-template/int.rs"]
|
#[path = "num/int_macros.rs"] mod int_macros;
|
||||||
pub mod int;
|
#[path = "num/uint_macros.rs"] mod uint_macros;
|
||||||
#[path = "num/int-template.rs"] #[merge = "num/int-template/i8.rs"]
|
|
||||||
pub mod i8;
|
|
||||||
#[path = "num/int-template.rs"] #[merge = "num/int-template/i16.rs"]
|
|
||||||
pub mod i16;
|
|
||||||
#[path = "num/int-template.rs"] #[merge = "num/int-template/i32.rs"]
|
|
||||||
pub mod i32;
|
|
||||||
#[path = "num/int-template.rs"] #[merge = "num/int-template/i64.rs"]
|
|
||||||
pub mod i64;
|
|
||||||
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/uint.rs"]
|
|
||||||
pub mod uint;
|
|
||||||
|
|
||||||
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u8.rs"]
|
#[path = "num/int.rs"] pub mod int;
|
||||||
pub mod u8;
|
#[path = "num/i8.rs"] pub mod i8;
|
||||||
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u16.rs"]
|
#[path = "num/i16.rs"] pub mod i16;
|
||||||
pub mod u16;
|
#[path = "num/i32.rs"] pub mod i32;
|
||||||
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u32.rs"]
|
#[path = "num/i64.rs"] pub mod i64;
|
||||||
pub mod u32;
|
|
||||||
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u64.rs"]
|
|
||||||
pub mod u64;
|
|
||||||
|
|
||||||
#[path = "num/float.rs"]
|
#[path = "num/uint.rs"] pub mod uint;
|
||||||
pub mod float;
|
#[path = "num/u8.rs"] pub mod u8;
|
||||||
#[path = "num/f32.rs"]
|
#[path = "num/u16.rs"] pub mod u16;
|
||||||
pub mod f32;
|
#[path = "num/u32.rs"] pub mod u32;
|
||||||
#[path = "num/f64.rs"]
|
#[path = "num/u64.rs"] pub mod u64;
|
||||||
pub mod f64;
|
|
||||||
|
#[path = "num/float.rs"] pub mod float;
|
||||||
|
#[path = "num/f32.rs"] pub mod f32;
|
||||||
|
#[path = "num/f64.rs"] pub mod f64;
|
||||||
|
|
||||||
pub mod nil;
|
pub mod nil;
|
||||||
pub mod bool;
|
pub mod bool;
|
||||||
|
|
32
src/libstd/num/i16.rs
Normal file
32
src/libstd/num/i16.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `i16`
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
|
use unstable::intrinsics;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
int_module!(i16, 16)
|
||||||
|
|
||||||
|
impl BitCount for i16 {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
|
||||||
|
}
|
32
src/libstd/num/i32.rs
Normal file
32
src/libstd/num/i32.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `i32`
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
|
use unstable::intrinsics;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
int_module!(i32, 32)
|
||||||
|
|
||||||
|
impl BitCount for i32 {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
|
||||||
|
}
|
32
src/libstd/num/i64.rs
Normal file
32
src/libstd/num/i64.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `i64`
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
|
use unstable::intrinsics;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
int_module!(i64, 64)
|
||||||
|
|
||||||
|
impl BitCount for i64 {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
|
||||||
|
}
|
32
src/libstd/num/i8.rs
Normal file
32
src/libstd/num/i8.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `i8`
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
|
use unstable::intrinsics;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
int_module!(i8, 8)
|
||||||
|
|
||||||
|
impl BitCount for i8 {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
|
||||||
|
}
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `i16`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = i16;
|
|
||||||
pub static bits: uint = ::u16::bits;
|
|
||||||
|
|
||||||
impl Primitive for i16 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 16 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<i16>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for i16 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `i32`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = i32;
|
|
||||||
pub static bits: uint = ::u32::bits;
|
|
||||||
|
|
||||||
impl Primitive for i32 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 32 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<i32>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for i32 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `i64`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = i64;
|
|
||||||
pub static bits: uint = ::u64::bits;
|
|
||||||
|
|
||||||
impl Primitive for i64 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 64 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<i64>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for i64 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `i8`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = i8;
|
|
||||||
pub static bits: uint = ::u8::bits;
|
|
||||||
|
|
||||||
impl Primitive for i8 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 8 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<i8>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for i8 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,104 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `int`
|
|
||||||
|
|
||||||
pub use self::inst::pow;
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
|
|
||||||
pub type T = int;
|
|
||||||
pub static bits: uint = ::uint::bits;
|
|
||||||
|
|
||||||
impl Primitive for int {
|
|
||||||
#[cfg(target_word_size = "32")]
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 32 }
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "64")]
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 64 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<int>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "32")]
|
|
||||||
#[inline(always)]
|
|
||||||
impl BitCount for int {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> int { (*self as i32).population_count() as int }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "64")]
|
|
||||||
#[inline(always)]
|
|
||||||
impl BitCount for int {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> int { (*self as i64).population_count() as int }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `base` raised to the power of `exponent`
|
|
||||||
pub fn pow(base: int, exponent: uint) -> int {
|
|
||||||
if exponent == 0u {
|
|
||||||
//Not mathemtically true if ~[base == 0]
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if base == 0 { return 0; }
|
|
||||||
let mut my_pow = exponent;
|
|
||||||
let mut acc = 1;
|
|
||||||
let mut multiplier = base;
|
|
||||||
while(my_pow > 0u) {
|
|
||||||
if my_pow % 2u == 1u {
|
|
||||||
acc *= multiplier;
|
|
||||||
}
|
|
||||||
my_pow /= 2u;
|
|
||||||
multiplier *= multiplier;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_pow() {
|
|
||||||
assert_eq!(pow(0, 0u), 1);
|
|
||||||
assert_eq!(pow(0, 1u), 0);
|
|
||||||
assert_eq!(pow(0, 2u), 0);
|
|
||||||
assert_eq!(pow(-1, 0u), 1);
|
|
||||||
assert_eq!(pow(1, 0u), 1);
|
|
||||||
assert_eq!(pow(-3, 2u), 9);
|
|
||||||
assert_eq!(pow(-3, 3u), -27);
|
|
||||||
assert_eq!(pow(4, 9u), 262144);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_overflows() {
|
|
||||||
assert!((::int::max_value > 0));
|
|
||||||
assert!((::int::min_value <= 0));
|
|
||||||
assert_eq!(::int::min_value + ::int::max_value + 1, 0);
|
|
||||||
}
|
|
||||||
}
|
|
89
src/libstd/num/int.rs
Normal file
89
src/libstd/num/int.rs
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `int`
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
#[cfg(target_word_size = "32")] pub static bits: uint = 32;
|
||||||
|
#[cfg(target_word_size = "64")] pub static bits: uint = 64;
|
||||||
|
|
||||||
|
int_module!(int, super::bits)
|
||||||
|
|
||||||
|
#[cfg(target_word_size = "32")]
|
||||||
|
impl BitCount for int {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> int { (*self as i32).population_count() as int }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_word_size = "64")]
|
||||||
|
impl BitCount for int {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> int { (*self as i64).population_count() as int }
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `base` raised to the power of `exponent`
|
||||||
|
pub fn pow(base: int, exponent: uint) -> int {
|
||||||
|
if exponent == 0u {
|
||||||
|
//Not mathemtically true if ~[base == 0]
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if base == 0 { return 0; }
|
||||||
|
let mut my_pow = exponent;
|
||||||
|
let mut acc = 1;
|
||||||
|
let mut multiplier = base;
|
||||||
|
while(my_pow > 0u) {
|
||||||
|
if my_pow % 2u == 1u {
|
||||||
|
acc *= multiplier;
|
||||||
|
}
|
||||||
|
my_pow /= 2u;
|
||||||
|
multiplier *= multiplier;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pow() {
|
||||||
|
assert!((pow(0, 0u) == 1));
|
||||||
|
assert!((pow(0, 1u) == 0));
|
||||||
|
assert!((pow(0, 2u) == 0));
|
||||||
|
assert!((pow(-1, 0u) == 1));
|
||||||
|
assert!((pow(1, 0u) == 1));
|
||||||
|
assert!((pow(-3, 2u) == 9));
|
||||||
|
assert!((pow(-3, 3u) == -27));
|
||||||
|
assert!((pow(4, 9u) == 262144));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_overflows() {
|
||||||
|
assert!((::int::max_value > 0));
|
||||||
|
assert!((::int::min_value <= 0));
|
||||||
|
assert!((::int::min_value + ::int::max_value + 1 == 0));
|
||||||
|
}
|
|
@ -8,7 +8,11 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use T = self::inst::T;
|
// FIXME(#4375): this shouldn't have to be a nested module named 'generated'
|
||||||
|
|
||||||
|
#[macro_escape];
|
||||||
|
|
||||||
|
macro_rules! int_module (($T:ty, $bits:expr) => (mod generated {
|
||||||
|
|
||||||
use num::{ToStrRadix, FromStrRadix};
|
use num::{ToStrRadix, FromStrRadix};
|
||||||
use num::{Zero, One, strconv};
|
use num::{Zero, One, strconv};
|
||||||
|
@ -16,20 +20,20 @@ use prelude::*;
|
||||||
|
|
||||||
pub use cmp::{min, max};
|
pub use cmp::{min, max};
|
||||||
|
|
||||||
pub static bits : uint = inst::bits;
|
pub static bits : uint = $bits;
|
||||||
pub static bytes : uint = (inst::bits / 8);
|
pub static bytes : uint = ($bits / 8);
|
||||||
|
|
||||||
pub static min_value: T = (-1 as T) << (bits - 1);
|
pub static min_value: $T = (-1 as $T) << (bits - 1);
|
||||||
pub static max_value: T = min_value - 1 as T;
|
pub static max_value: $T = min_value - 1 as $T;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn add(x: T, y: T) -> T { x + y }
|
pub fn add(x: $T, y: $T) -> $T { x + y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn sub(x: T, y: T) -> T { x - y }
|
pub fn sub(x: $T, y: $T) -> $T { x - y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn mul(x: T, y: T) -> T { x * y }
|
pub fn mul(x: $T, y: $T) -> $T { x * y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn div(x: T, y: T) -> T { x / y }
|
pub fn div(x: $T, y: $T) -> $T { x / y }
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns the remainder of y / x.
|
/// Returns the remainder of y / x.
|
||||||
|
@ -52,20 +56,20 @@ pub fn div(x: T, y: T) -> T { x / y }
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn rem(x: T, y: T) -> T { x % y }
|
pub fn rem(x: $T, y: $T) -> $T { x % y }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn lt(x: T, y: T) -> bool { x < y }
|
pub fn lt(x: $T, y: $T) -> bool { x < y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn le(x: T, y: T) -> bool { x <= y }
|
pub fn le(x: $T, y: $T) -> bool { x <= y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn eq(x: T, y: T) -> bool { x == y }
|
pub fn eq(x: $T, y: $T) -> bool { x == y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn ne(x: T, y: T) -> bool { x != y }
|
pub fn ne(x: $T, y: $T) -> bool { x != y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn ge(x: T, y: T) -> bool { x >= y }
|
pub fn ge(x: $T, y: $T) -> bool { x >= y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn gt(x: T, y: T) -> bool { x > y }
|
pub fn gt(x: $T, y: $T) -> bool { x > y }
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Iterate over the range [`lo`..`hi`)
|
/// Iterate over the range [`lo`..`hi`)
|
||||||
|
@ -85,11 +89,10 @@ pub fn gt(x: T, y: T) -> bool { x > y }
|
||||||
/// ~~~
|
/// ~~~
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Iterate over the range [`start`,`start`+`step`..`stop`)
|
pub fn range_step(start: $T, stop: $T, step: $T, it: &fn($T) -> bool) -> bool {
|
||||||
pub fn _range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) -> bool {
|
|
||||||
let mut i = start;
|
let mut i = start;
|
||||||
if step == 0 {
|
if step == 0 {
|
||||||
fail!("range_step called with step == 0");
|
fail!(~"range_step called with step == 0");
|
||||||
} else if step > 0 { // ascending
|
} else if step > 0 { // ascending
|
||||||
while i < stop {
|
while i < stop {
|
||||||
if !it(i) { return false; }
|
if !it(i) { return false; }
|
||||||
|
@ -108,109 +111,101 @@ pub fn _range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) -> bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) -> bool {
|
|
||||||
_range_step(start, stop, step, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Iterate over the range [`lo`..`hi`)
|
/// Iterate over the range [`lo`..`hi`)
|
||||||
pub fn range(lo: T, hi: T, it: &fn(T) -> bool) -> bool {
|
pub fn range(lo: $T, hi: $T, it: &fn($T) -> bool) -> bool {
|
||||||
range_step(lo, hi, 1 as T, it)
|
range_step(lo, hi, 1 as $T, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Iterate over the range [`hi`..`lo`)
|
/// Iterate over the range [`hi`..`lo`)
|
||||||
pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) -> bool {
|
pub fn range_rev(hi: $T, lo: $T, it: &fn($T) -> bool) -> bool {
|
||||||
range_step(hi, lo, -1 as T, it)
|
range_step(hi, lo, -1 as $T, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the bitwise complement
|
/// Computes the bitwise complement
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn compl(i: T) -> T {
|
pub fn compl(i: $T) -> $T {
|
||||||
-1 as T ^ i
|
-1 as $T ^ i
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the absolute value
|
/// Computes the absolute value
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn abs(i: T) -> T { i.abs() }
|
pub fn abs(i: $T) -> $T { i.abs() }
|
||||||
|
|
||||||
impl Num for T {}
|
impl Num for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Ord for T {
|
impl Ord for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lt(&self, other: &T) -> bool { return (*self) < (*other); }
|
fn lt(&self, other: &$T) -> bool { return (*self) < (*other); }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn le(&self, other: &T) -> bool { return (*self) <= (*other); }
|
fn le(&self, other: &$T) -> bool { return (*self) <= (*other); }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ge(&self, other: &T) -> bool { return (*self) >= (*other); }
|
fn ge(&self, other: &$T) -> bool { return (*self) >= (*other); }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn gt(&self, other: &T) -> bool { return (*self) > (*other); }
|
fn gt(&self, other: &$T) -> bool { return (*self) > (*other); }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Eq for T {
|
impl Eq for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: &T) -> bool { return (*self) == (*other); }
|
fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
|
fn ne(&self, other: &$T) -> bool { return (*self) != (*other); }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Orderable for T {
|
impl Orderable for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn min(&self, other: &T) -> T {
|
fn min(&self, other: &$T) -> $T {
|
||||||
if *self < *other { *self } else { *other }
|
if *self < *other { *self } else { *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn max(&self, other: &T) -> T {
|
fn max(&self, other: &$T) -> $T {
|
||||||
if *self > *other { *self } else { *other }
|
if *self > *other { *self } else { *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number constrained within the range `mn <= self <= mx`.
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn clamp(&self, mn: &T, mx: &T) -> T {
|
fn clamp(&self, mn: &$T, mx: &$T) -> $T {
|
||||||
cond!(
|
if *self > *mx { *mx } else
|
||||||
(*self > *mx) { *mx }
|
if *self < *mn { *mn } else { *self }
|
||||||
(*self < *mn) { *mn }
|
|
||||||
_ { *self }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Zero for T {
|
impl Zero for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn zero() -> T { 0 }
|
fn zero() -> $T { 0 }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn is_zero(&self) -> bool { *self == 0 }
|
fn is_zero(&self) -> bool { *self == 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl One for T {
|
impl One for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn one() -> T { 1 }
|
fn one() -> $T { 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Add<T,T> for T {
|
impl Add<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn add(&self, other: &T) -> T { *self + *other }
|
fn add(&self, other: &$T) -> $T { *self + *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Sub<T,T> for T {
|
impl Sub<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn sub(&self, other: &T) -> T { *self - *other }
|
fn sub(&self, other: &$T) -> $T { *self - *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Mul<T,T> for T {
|
impl Mul<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mul(&self, other: &T) -> T { *self * *other }
|
fn mul(&self, other: &$T) -> $T { *self * *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Div<T,T> for T {
|
impl Div<$T,$T> for $T {
|
||||||
///
|
///
|
||||||
/// Integer division, truncated towards 0. As this behaviour reflects the underlying
|
/// Integer division, truncated towards 0. As this behaviour reflects the underlying
|
||||||
/// machine implementation it is more efficient than `Integer::div_floor`.
|
/// machine implementation it is more efficient than `Integer::div_floor`.
|
||||||
|
@ -230,11 +225,11 @@ impl Div<T,T> for T {
|
||||||
/// ~~~
|
/// ~~~
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div(&self, other: &T) -> T { *self / *other }
|
fn div(&self, other: &$T) -> $T { *self / *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Rem<T,T> for T {
|
impl Rem<$T,$T> for $T {
|
||||||
///
|
///
|
||||||
/// Returns the integer remainder after division, satisfying:
|
/// Returns the integer remainder after division, satisfying:
|
||||||
///
|
///
|
||||||
|
@ -257,19 +252,19 @@ impl Rem<T,T> for T {
|
||||||
/// ~~~
|
/// ~~~
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn rem(&self, other: &T) -> T { *self % *other }
|
fn rem(&self, other: &$T) -> $T { *self % *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Neg<T> for T {
|
impl Neg<$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn neg(&self) -> T { -*self }
|
fn neg(&self) -> $T { -*self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Signed for T {
|
impl Signed for $T {
|
||||||
/// Computes the absolute value
|
/// Computes the absolute value
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn abs(&self) -> T {
|
fn abs(&self) -> $T {
|
||||||
if self.is_negative() { -*self } else { *self }
|
if self.is_negative() { -*self } else { *self }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +273,7 @@ impl Signed for T {
|
||||||
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
|
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn abs_sub(&self, other: &T) -> T {
|
fn abs_sub(&self, other: &$T) -> $T {
|
||||||
if *self <= *other { 0 } else { *self - *other }
|
if *self <= *other { 0 } else { *self - *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +285,7 @@ impl Signed for T {
|
||||||
/// - `-1` if the number is negative
|
/// - `-1` if the number is negative
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn signum(&self) -> T {
|
fn signum(&self) -> $T {
|
||||||
match *self {
|
match *self {
|
||||||
n if n > 0 => 1,
|
n if n > 0 => 1,
|
||||||
0 => 0,
|
0 => 0,
|
||||||
|
@ -307,7 +302,7 @@ impl Signed for T {
|
||||||
fn is_negative(&self) -> bool { *self < 0 }
|
fn is_negative(&self) -> bool { *self < 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Integer for T {
|
impl Integer for $T {
|
||||||
///
|
///
|
||||||
/// Floored integer division
|
/// Floored integer division
|
||||||
///
|
///
|
||||||
|
@ -326,7 +321,7 @@ impl Integer for T {
|
||||||
/// ~~~
|
/// ~~~
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_floor(&self, other: &T) -> T {
|
fn div_floor(&self, other: &$T) -> $T {
|
||||||
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
||||||
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
||||||
match self.div_rem(other) {
|
match self.div_rem(other) {
|
||||||
|
@ -358,7 +353,7 @@ impl Integer for T {
|
||||||
/// ~~~
|
/// ~~~
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mod_floor(&self, other: &T) -> T {
|
fn mod_floor(&self, other: &$T) -> $T {
|
||||||
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
||||||
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
||||||
match *self % *other {
|
match *self % *other {
|
||||||
|
@ -370,7 +365,7 @@ impl Integer for T {
|
||||||
|
|
||||||
/// Calculates `div_floor` and `mod_floor` simultaneously
|
/// Calculates `div_floor` and `mod_floor` simultaneously
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_mod_floor(&self, other: &T) -> (T,T) {
|
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
|
||||||
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
|
||||||
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
|
||||||
match self.div_rem(other) {
|
match self.div_rem(other) {
|
||||||
|
@ -382,7 +377,7 @@ impl Integer for T {
|
||||||
|
|
||||||
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
|
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_rem(&self, other: &T) -> (T,T) {
|
fn div_rem(&self, other: &$T) -> ($T,$T) {
|
||||||
(*self / *other, *self % *other)
|
(*self / *other, *self % *other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,7 +387,7 @@ impl Integer for T {
|
||||||
/// The result is always positive
|
/// The result is always positive
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn gcd(&self, other: &T) -> T {
|
fn gcd(&self, other: &$T) -> $T {
|
||||||
// Use Euclid's algorithm
|
// Use Euclid's algorithm
|
||||||
let mut m = *self, n = *other;
|
let mut m = *self, n = *other;
|
||||||
while m != 0 {
|
while m != 0 {
|
||||||
|
@ -407,13 +402,13 @@ impl Integer for T {
|
||||||
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
||||||
///
|
///
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lcm(&self, other: &T) -> T {
|
fn lcm(&self, other: &$T) -> $T {
|
||||||
((*self * *other) / self.gcd(other)).abs() // should not have to recaluculate abs
|
((*self * *other) / self.gcd(other)).abs() // should not have to recaluculate abs
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
|
fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
|
||||||
|
|
||||||
/// Returns `true` if the number is divisible by `2`
|
/// Returns `true` if the number is divisible by `2`
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -424,87 +419,95 @@ impl Integer for T {
|
||||||
fn is_odd(&self) -> bool { !self.is_even() }
|
fn is_odd(&self) -> bool { !self.is_even() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bitwise for T {}
|
impl Bitwise for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitOr<T,T> for T {
|
impl BitOr<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitor(&self, other: &T) -> T { *self | *other }
|
fn bitor(&self, other: &$T) -> $T { *self | *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitAnd<T,T> for T {
|
impl BitAnd<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitand(&self, other: &T) -> T { *self & *other }
|
fn bitand(&self, other: &$T) -> $T { *self & *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitXor<T,T> for T {
|
impl BitXor<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitxor(&self, other: &T) -> T { *self ^ *other }
|
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Shl<T,T> for T {
|
impl Shl<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn shl(&self, other: &T) -> T { *self << *other }
|
fn shl(&self, other: &$T) -> $T { *self << *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Shr<T,T> for T {
|
impl Shr<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn shr(&self, other: &T) -> T { *self >> *other }
|
fn shr(&self, other: &$T) -> $T { *self >> *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Not<T> for T {
|
impl Not<$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn not(&self) -> T { !*self }
|
fn not(&self) -> $T { !*self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bounded for T {
|
impl Bounded for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn min_value() -> T { min_value }
|
fn min_value() -> $T { min_value }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn max_value() -> T { max_value }
|
fn max_value() -> $T { max_value }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Int for T {}
|
impl Int for $T {}
|
||||||
|
|
||||||
|
impl Primitive for $T {
|
||||||
|
#[inline(always)]
|
||||||
|
fn bits() -> uint { bits }
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn bytes() -> uint { bits / 8 }
|
||||||
|
}
|
||||||
|
|
||||||
// String conversion functions and impl str -> num
|
// String conversion functions and impl str -> num
|
||||||
|
|
||||||
/// Parse a string as a number in base 10.
|
/// Parse a string as a number in base 10.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn from_str(s: &str) -> Option<T> {
|
pub fn from_str(s: &str) -> Option<$T> {
|
||||||
strconv::from_str_common(s, 10u, true, false, false,
|
strconv::from_str_common(s, 10u, true, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a string as a number in the given base.
|
/// Parse a string as a number in the given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn from_str_radix(s: &str, radix: uint) -> Option<T> {
|
pub fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
|
||||||
strconv::from_str_common(s, radix, true, false, false,
|
strconv::from_str_common(s, radix, true, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a byte slice as a number in the given base.
|
/// Parse a byte slice as a number in the given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
|
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
|
||||||
strconv::from_str_bytes_common(buf, radix, true, false, false,
|
strconv::from_str_bytes_common(buf, radix, true, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for T {
|
impl FromStr for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_str(s: &str) -> Option<T> {
|
fn from_str(s: &str) -> Option<$T> {
|
||||||
from_str(s)
|
from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStrRadix for T {
|
impl FromStrRadix for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_str_radix(s: &str, radix: uint) -> Option<T> {
|
fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
|
||||||
from_str_radix(s, radix)
|
from_str_radix(s, radix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -513,7 +516,7 @@ impl FromStrRadix for T {
|
||||||
|
|
||||||
/// Convert to a string as a byte slice in a given base.
|
/// Convert to a string as a byte slice in a given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str_bytes<U>(n: T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
pub fn to_str_bytes<U>(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
||||||
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
|
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
f(buf)
|
f(buf)
|
||||||
|
@ -521,7 +524,7 @@ pub fn to_str_bytes<U>(n: T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
||||||
|
|
||||||
/// Convert to a string in base 10.
|
/// Convert to a string in base 10.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str(num: T) -> ~str {
|
pub fn to_str(num: $T) -> ~str {
|
||||||
let (buf, _) = strconv::to_str_common(&num, 10u, false,
|
let (buf, _) = strconv::to_str_common(&num, 10u, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
buf
|
buf
|
||||||
|
@ -529,20 +532,20 @@ pub fn to_str(num: T) -> ~str {
|
||||||
|
|
||||||
/// Convert to a string in a given base.
|
/// Convert to a string in a given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str_radix(num: T, radix: uint) -> ~str {
|
pub fn to_str_radix(num: $T, radix: uint) -> ~str {
|
||||||
let (buf, _) = strconv::to_str_common(&num, radix, false,
|
let (buf, _) = strconv::to_str_common(&num, radix, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToStr for T {
|
impl ToStr for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_str(&self) -> ~str {
|
fn to_str(&self) -> ~str {
|
||||||
to_str(*self)
|
to_str(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToStrRadix for T {
|
impl ToStrRadix for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_str_radix(&self, radix: uint) -> ~str {
|
fn to_str_radix(&self, radix: uint) -> ~str {
|
||||||
to_str_radix(*self, radix)
|
to_str_radix(*self, radix)
|
||||||
|
@ -552,62 +555,61 @@ impl ToStrRadix for T {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use super::inst::T;
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_num() {
|
fn test_num() {
|
||||||
num::test_num(10 as T, 2 as T);
|
num::test_num(10 as $T, 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_orderable() {
|
fn test_orderable() {
|
||||||
assert_eq!((1 as T).min(&(2 as T)), 1 as T);
|
assert_eq!((1 as $T).min(&(2 as $T)), 1 as $T);
|
||||||
assert_eq!((2 as T).min(&(1 as T)), 1 as T);
|
assert_eq!((2 as $T).min(&(1 as $T)), 1 as $T);
|
||||||
assert_eq!((1 as T).max(&(2 as T)), 2 as T);
|
assert_eq!((1 as $T).max(&(2 as $T)), 2 as $T);
|
||||||
assert_eq!((2 as T).max(&(1 as T)), 2 as T);
|
assert_eq!((2 as $T).max(&(1 as $T)), 2 as $T);
|
||||||
assert_eq!((1 as T).clamp(&(2 as T), &(4 as T)), 2 as T);
|
assert_eq!((1 as $T).clamp(&(2 as $T), &(4 as $T)), 2 as $T);
|
||||||
assert_eq!((8 as T).clamp(&(2 as T), &(4 as T)), 4 as T);
|
assert_eq!((8 as $T).clamp(&(2 as $T), &(4 as $T)), 4 as $T);
|
||||||
assert_eq!((3 as T).clamp(&(2 as T), &(4 as T)), 3 as T);
|
assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_abs() {
|
pub fn test_abs() {
|
||||||
assert_eq!((1 as T).abs(), 1 as T);
|
assert_eq!((1 as $T).abs(), 1 as $T);
|
||||||
assert_eq!((0 as T).abs(), 0 as T);
|
assert_eq!((0 as $T).abs(), 0 as $T);
|
||||||
assert_eq!((-1 as T).abs(), 1 as T);
|
assert_eq!((-1 as $T).abs(), 1 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_abs_sub() {
|
fn test_abs_sub() {
|
||||||
assert_eq!((-1 as T).abs_sub(&(1 as T)), 0 as T);
|
assert_eq!((-1 as $T).abs_sub(&(1 as $T)), 0 as $T);
|
||||||
assert_eq!((1 as T).abs_sub(&(1 as T)), 0 as T);
|
assert_eq!((1 as $T).abs_sub(&(1 as $T)), 0 as $T);
|
||||||
assert_eq!((1 as T).abs_sub(&(0 as T)), 1 as T);
|
assert_eq!((1 as $T).abs_sub(&(0 as $T)), 1 as $T);
|
||||||
assert_eq!((1 as T).abs_sub(&(-1 as T)), 2 as T);
|
assert_eq!((1 as $T).abs_sub(&(-1 as $T)), 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_signum() {
|
fn test_signum() {
|
||||||
assert_eq!((1 as T).signum(), 1 as T);
|
assert_eq!((1 as $T).signum(), 1 as $T);
|
||||||
assert_eq!((0 as T).signum(), 0 as T);
|
assert_eq!((0 as $T).signum(), 0 as $T);
|
||||||
assert_eq!((-0 as T).signum(), 0 as T);
|
assert_eq!((-0 as $T).signum(), 0 as $T);
|
||||||
assert_eq!((-1 as T).signum(), -1 as T);
|
assert_eq!((-1 as $T).signum(), -1 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_positive() {
|
fn test_is_positive() {
|
||||||
assert!((1 as T).is_positive());
|
assert!((1 as $T).is_positive());
|
||||||
assert!(!(0 as T).is_positive());
|
assert!(!(0 as $T).is_positive());
|
||||||
assert!(!(-0 as T).is_positive());
|
assert!(!(-0 as $T).is_positive());
|
||||||
assert!(!(-1 as T).is_positive());
|
assert!(!(-1 as $T).is_positive());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_negative() {
|
fn test_is_negative() {
|
||||||
assert!(!(1 as T).is_negative());
|
assert!(!(1 as $T).is_negative());
|
||||||
assert!(!(0 as T).is_negative());
|
assert!(!(0 as $T).is_negative());
|
||||||
assert!(!(-0 as T).is_negative());
|
assert!(!(-0 as $T).is_negative());
|
||||||
assert!((-1 as T).is_negative());
|
assert!((-1 as $T).is_negative());
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -618,13 +620,13 @@ mod tests {
|
||||||
/// - `qr`: quotient and remainder
|
/// - `qr`: quotient and remainder
|
||||||
///
|
///
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn test_division_rule((n,d): (T,T), (q,r): (T,T)) {
|
fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
|
||||||
assert_eq!(d * q + r, n);
|
assert_eq!(d * q + r, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div_rem() {
|
fn test_div_rem() {
|
||||||
fn test_nd_dr(nd: (T,T), qr: (T,T)) {
|
fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) {
|
||||||
let (n,d) = nd;
|
let (n,d) = nd;
|
||||||
let separate_div_rem = (n / d, n % d);
|
let separate_div_rem = (n / d, n % d);
|
||||||
let combined_div_rem = n.div_rem(&d);
|
let combined_div_rem = n.div_rem(&d);
|
||||||
|
@ -649,7 +651,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div_mod_floor() {
|
fn test_div_mod_floor() {
|
||||||
fn test_nd_dm(nd: (T,T), dm: (T,T)) {
|
fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) {
|
||||||
let (n,d) = nd;
|
let (n,d) = nd;
|
||||||
let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
|
let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
|
||||||
let combined_div_mod_floor = n.div_mod_floor(&d);
|
let combined_div_mod_floor = n.div_mod_floor(&d);
|
||||||
|
@ -674,135 +676,135 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gcd() {
|
fn test_gcd() {
|
||||||
assert_eq!((10 as T).gcd(&2), 2 as T);
|
assert_eq!((10 as $T).gcd(&2), 2 as $T);
|
||||||
assert_eq!((10 as T).gcd(&3), 1 as T);
|
assert_eq!((10 as $T).gcd(&3), 1 as $T);
|
||||||
assert_eq!((0 as T).gcd(&3), 3 as T);
|
assert_eq!((0 as $T).gcd(&3), 3 as $T);
|
||||||
assert_eq!((3 as T).gcd(&3), 3 as T);
|
assert_eq!((3 as $T).gcd(&3), 3 as $T);
|
||||||
assert_eq!((56 as T).gcd(&42), 14 as T);
|
assert_eq!((56 as $T).gcd(&42), 14 as $T);
|
||||||
assert_eq!((3 as T).gcd(&-3), 3 as T);
|
assert_eq!((3 as $T).gcd(&-3), 3 as $T);
|
||||||
assert_eq!((-6 as T).gcd(&3), 3 as T);
|
assert_eq!((-6 as $T).gcd(&3), 3 as $T);
|
||||||
assert_eq!((-4 as T).gcd(&-2), 2 as T);
|
assert_eq!((-4 as $T).gcd(&-2), 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lcm() {
|
fn test_lcm() {
|
||||||
assert_eq!((1 as T).lcm(&0), 0 as T);
|
assert_eq!((1 as $T).lcm(&0), 0 as $T);
|
||||||
assert_eq!((0 as T).lcm(&1), 0 as T);
|
assert_eq!((0 as $T).lcm(&1), 0 as $T);
|
||||||
assert_eq!((1 as T).lcm(&1), 1 as T);
|
assert_eq!((1 as $T).lcm(&1), 1 as $T);
|
||||||
assert_eq!((-1 as T).lcm(&1), 1 as T);
|
assert_eq!((-1 as $T).lcm(&1), 1 as $T);
|
||||||
assert_eq!((1 as T).lcm(&-1), 1 as T);
|
assert_eq!((1 as $T).lcm(&-1), 1 as $T);
|
||||||
assert_eq!((-1 as T).lcm(&-1), 1 as T);
|
assert_eq!((-1 as $T).lcm(&-1), 1 as $T);
|
||||||
assert_eq!((8 as T).lcm(&9), 72 as T);
|
assert_eq!((8 as $T).lcm(&9), 72 as $T);
|
||||||
assert_eq!((11 as T).lcm(&5), 55 as T);
|
assert_eq!((11 as $T).lcm(&5), 55 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bitwise() {
|
fn test_bitwise() {
|
||||||
assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));
|
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
|
||||||
assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(0b1010 as T)));
|
assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
|
||||||
assert_eq!(0b0110 as T, (0b1100 as T).bitxor(&(0b1010 as T)));
|
assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
||||||
assert_eq!(0b1110 as T, (0b0111 as T).shl(&(1 as T)));
|
assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
|
||||||
assert_eq!(0b0111 as T, (0b1110 as T).shr(&(1 as T)));
|
assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
|
||||||
assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
|
assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multiple_of() {
|
fn test_multiple_of() {
|
||||||
assert!((6 as T).is_multiple_of(&(6 as T)));
|
assert!((6 as $T).is_multiple_of(&(6 as $T)));
|
||||||
assert!((6 as T).is_multiple_of(&(3 as T)));
|
assert!((6 as $T).is_multiple_of(&(3 as $T)));
|
||||||
assert!((6 as T).is_multiple_of(&(1 as T)));
|
assert!((6 as $T).is_multiple_of(&(1 as $T)));
|
||||||
assert!((-8 as T).is_multiple_of(&(4 as T)));
|
assert!((-8 as $T).is_multiple_of(&(4 as $T)));
|
||||||
assert!((8 as T).is_multiple_of(&(-1 as T)));
|
assert!((8 as $T).is_multiple_of(&(-1 as $T)));
|
||||||
assert!((-8 as T).is_multiple_of(&(-2 as T)));
|
assert!((-8 as $T).is_multiple_of(&(-2 as $T)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_even() {
|
fn test_even() {
|
||||||
assert_eq!((-4 as T).is_even(), true);
|
assert_eq!((-4 as $T).is_even(), true);
|
||||||
assert_eq!((-3 as T).is_even(), false);
|
assert_eq!((-3 as $T).is_even(), false);
|
||||||
assert_eq!((-2 as T).is_even(), true);
|
assert_eq!((-2 as $T).is_even(), true);
|
||||||
assert_eq!((-1 as T).is_even(), false);
|
assert_eq!((-1 as $T).is_even(), false);
|
||||||
assert_eq!((0 as T).is_even(), true);
|
assert_eq!((0 as $T).is_even(), true);
|
||||||
assert_eq!((1 as T).is_even(), false);
|
assert_eq!((1 as $T).is_even(), false);
|
||||||
assert_eq!((2 as T).is_even(), true);
|
assert_eq!((2 as $T).is_even(), true);
|
||||||
assert_eq!((3 as T).is_even(), false);
|
assert_eq!((3 as $T).is_even(), false);
|
||||||
assert_eq!((4 as T).is_even(), true);
|
assert_eq!((4 as $T).is_even(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_odd() {
|
fn test_odd() {
|
||||||
assert_eq!((-4 as T).is_odd(), false);
|
assert_eq!((-4 as $T).is_odd(), false);
|
||||||
assert_eq!((-3 as T).is_odd(), true);
|
assert_eq!((-3 as $T).is_odd(), true);
|
||||||
assert_eq!((-2 as T).is_odd(), false);
|
assert_eq!((-2 as $T).is_odd(), false);
|
||||||
assert_eq!((-1 as T).is_odd(), true);
|
assert_eq!((-1 as $T).is_odd(), true);
|
||||||
assert_eq!((0 as T).is_odd(), false);
|
assert_eq!((0 as $T).is_odd(), false);
|
||||||
assert_eq!((1 as T).is_odd(), true);
|
assert_eq!((1 as $T).is_odd(), true);
|
||||||
assert_eq!((2 as T).is_odd(), false);
|
assert_eq!((2 as $T).is_odd(), false);
|
||||||
assert_eq!((3 as T).is_odd(), true);
|
assert_eq!((3 as $T).is_odd(), true);
|
||||||
assert_eq!((4 as T).is_odd(), false);
|
assert_eq!((4 as $T).is_odd(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bitcount() {
|
fn test_bitcount() {
|
||||||
assert_eq!((0b010101 as T).population_count(), 3);
|
assert_eq!((0b010101 as $T).population_count(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_primitive() {
|
fn test_primitive() {
|
||||||
assert_eq!(Primitive::bits::<T>(), sys::size_of::<T>() * 8);
|
assert_eq!(Primitive::bits::<$T>(), sys::size_of::<$T>() * 8);
|
||||||
assert_eq!(Primitive::bytes::<T>(), sys::size_of::<T>());
|
assert_eq!(Primitive::bytes::<$T>(), sys::size_of::<$T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_str() {
|
fn test_from_str() {
|
||||||
assert_eq!(from_str("0"), Some(0 as T));
|
assert_eq!(from_str(~"0"), Some(0 as $T));
|
||||||
assert_eq!(from_str("3"), Some(3 as T));
|
assert_eq!(from_str(~"3"), Some(3 as $T));
|
||||||
assert_eq!(from_str("10"), Some(10 as T));
|
assert_eq!(from_str(~"10"), Some(10 as $T));
|
||||||
assert_eq!(i32::from_str("123456789"), Some(123456789 as i32));
|
assert_eq!(i32::from_str(~"123456789"), Some(123456789 as i32));
|
||||||
assert_eq!(from_str("00100"), Some(100 as T));
|
assert_eq!(from_str(~"00100"), Some(100 as $T));
|
||||||
|
|
||||||
assert_eq!(from_str("-1"), Some(-1 as T));
|
assert_eq!(from_str(~"-1"), Some(-1 as $T));
|
||||||
assert_eq!(from_str("-3"), Some(-3 as T));
|
assert_eq!(from_str(~"-3"), Some(-3 as $T));
|
||||||
assert_eq!(from_str("-10"), Some(-10 as T));
|
assert_eq!(from_str(~"-10"), Some(-10 as $T));
|
||||||
assert_eq!(i32::from_str("-123456789"), Some(-123456789 as i32));
|
assert_eq!(i32::from_str(~"-123456789"), Some(-123456789 as i32));
|
||||||
assert_eq!(from_str("-00100"), Some(-100 as T));
|
assert_eq!(from_str(~"-00100"), Some(-100 as $T));
|
||||||
|
|
||||||
assert!(from_str(" ").is_none());
|
assert!(from_str(~" ").is_none());
|
||||||
assert!(from_str("x").is_none());
|
assert!(from_str(~"x").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_bytes() {
|
fn test_parse_bytes() {
|
||||||
use str::to_bytes;
|
use str::to_bytes;
|
||||||
assert_eq!(parse_bytes(to_bytes("123"), 10u), Some(123 as T));
|
assert_eq!(parse_bytes(to_bytes(~"123"), 10u), Some(123 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("1001"), 2u), Some(9 as T));
|
assert_eq!(parse_bytes(to_bytes(~"1001"), 2u), Some(9 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("123"), 8u), Some(83 as T));
|
assert_eq!(parse_bytes(to_bytes(~"123"), 8u), Some(83 as $T));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("123"), 16u), Some(291 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"123"), 16u), Some(291 as i32));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("ffff"), 16u), Some(65535 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"ffff"), 16u), Some(65535 as i32));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("FFFF"), 16u), Some(65535 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"FFFF"), 16u), Some(65535 as i32));
|
||||||
assert_eq!(parse_bytes(to_bytes("z"), 36u), Some(35 as T));
|
assert_eq!(parse_bytes(to_bytes(~"z"), 36u), Some(35 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("Z"), 36u), Some(35 as T));
|
assert_eq!(parse_bytes(to_bytes(~"Z"), 36u), Some(35 as $T));
|
||||||
|
|
||||||
assert_eq!(parse_bytes(to_bytes("-123"), 10u), Some(-123 as T));
|
assert_eq!(parse_bytes(to_bytes(~"-123"), 10u), Some(-123 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("-1001"), 2u), Some(-9 as T));
|
assert_eq!(parse_bytes(to_bytes(~"-1001"), 2u), Some(-9 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("-123"), 8u), Some(-83 as T));
|
assert_eq!(parse_bytes(to_bytes(~"-123"), 8u), Some(-83 as $T));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("-123"), 16u), Some(-291 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"-123"), 16u), Some(-291 as i32));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("-ffff"), 16u), Some(-65535 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"-ffff"), 16u), Some(-65535 as i32));
|
||||||
assert_eq!(i32::parse_bytes(to_bytes("-FFFF"), 16u), Some(-65535 as i32));
|
assert_eq!(i32::parse_bytes(to_bytes(~"-FFFF"), 16u), Some(-65535 as i32));
|
||||||
assert_eq!(parse_bytes(to_bytes("-z"), 36u), Some(-35 as T));
|
assert_eq!(parse_bytes(to_bytes(~"-z"), 36u), Some(-35 as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("-Z"), 36u), Some(-35 as T));
|
assert_eq!(parse_bytes(to_bytes(~"-Z"), 36u), Some(-35 as $T));
|
||||||
|
|
||||||
assert!(parse_bytes(to_bytes("Z"), 35u).is_none());
|
assert!(parse_bytes(to_bytes(~"Z"), 35u).is_none());
|
||||||
assert!(parse_bytes(to_bytes("-9"), 2u).is_none());
|
assert!(parse_bytes(to_bytes(~"-9"), 2u).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_to_str() {
|
fn test_to_str() {
|
||||||
assert_eq!(to_str_radix(0 as T, 10u), ~"0");
|
assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
|
||||||
assert_eq!(to_str_radix(1 as T, 10u), ~"1");
|
assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
|
||||||
assert_eq!(to_str_radix(-1 as T, 10u), ~"-1");
|
assert_eq!(to_str_radix(-1 as $T, 10u), ~"-1");
|
||||||
assert_eq!(to_str_radix(127 as T, 16u), ~"7f");
|
assert_eq!(to_str_radix(127 as $T, 16u), ~"7f");
|
||||||
assert_eq!(to_str_radix(100 as T, 10u), ~"100");
|
assert_eq!(to_str_radix(100 as $T, 10u), ~"100");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -836,36 +838,36 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_int_from_str_overflow() {
|
fn test_int_from_str_overflow() {
|
||||||
let mut i8_val: i8 = 127_i8;
|
let mut i8_val: i8 = 127_i8;
|
||||||
assert_eq!(i8::from_str("127"), Some(i8_val));
|
assert_eq!(i8::from_str(~"127"), Some(i8_val));
|
||||||
assert!(i8::from_str("128").is_none());
|
assert!(i8::from_str(~"128").is_none());
|
||||||
|
|
||||||
i8_val += 1 as i8;
|
i8_val += 1 as i8;
|
||||||
assert_eq!(i8::from_str("-128"), Some(i8_val));
|
assert_eq!(i8::from_str(~"-128"), Some(i8_val));
|
||||||
assert!(i8::from_str("-129").is_none());
|
assert!(i8::from_str(~"-129").is_none());
|
||||||
|
|
||||||
let mut i16_val: i16 = 32_767_i16;
|
let mut i16_val: i16 = 32_767_i16;
|
||||||
assert_eq!(i16::from_str("32767"), Some(i16_val));
|
assert_eq!(i16::from_str(~"32767"), Some(i16_val));
|
||||||
assert!(i16::from_str("32768").is_none());
|
assert!(i16::from_str(~"32768").is_none());
|
||||||
|
|
||||||
i16_val += 1 as i16;
|
i16_val += 1 as i16;
|
||||||
assert_eq!(i16::from_str("-32768"), Some(i16_val));
|
assert_eq!(i16::from_str(~"-32768"), Some(i16_val));
|
||||||
assert!(i16::from_str("-32769").is_none());
|
assert!(i16::from_str(~"-32769").is_none());
|
||||||
|
|
||||||
let mut i32_val: i32 = 2_147_483_647_i32;
|
let mut i32_val: i32 = 2_147_483_647_i32;
|
||||||
assert_eq!(i32::from_str("2147483647"), Some(i32_val));
|
assert_eq!(i32::from_str(~"2147483647"), Some(i32_val));
|
||||||
assert!(i32::from_str("2147483648").is_none());
|
assert!(i32::from_str(~"2147483648").is_none());
|
||||||
|
|
||||||
i32_val += 1 as i32;
|
i32_val += 1 as i32;
|
||||||
assert_eq!(i32::from_str("-2147483648"), Some(i32_val));
|
assert_eq!(i32::from_str(~"-2147483648"), Some(i32_val));
|
||||||
assert!(i32::from_str("-2147483649").is_none());
|
assert!(i32::from_str(~"-2147483649").is_none());
|
||||||
|
|
||||||
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
|
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
|
||||||
assert_eq!(i64::from_str("9223372036854775807"), Some(i64_val));
|
assert_eq!(i64::from_str(~"9223372036854775807"), Some(i64_val));
|
||||||
assert!(i64::from_str("9223372036854775808").is_none());
|
assert!(i64::from_str(~"9223372036854775808").is_none());
|
||||||
|
|
||||||
i64_val += 1 as i64;
|
i64_val += 1 as i64;
|
||||||
assert_eq!(i64::from_str("-9223372036854775808"), Some(i64_val));
|
assert_eq!(i64::from_str(~"-9223372036854775808"), Some(i64_val));
|
||||||
assert!(i64::from_str("-9223372036854775809").is_none());
|
assert!(i64::from_str(~"-9223372036854775809").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -907,16 +909,16 @@ mod tests {
|
||||||
|
|
||||||
// None of the `fail`s should execute.
|
// None of the `fail`s should execute.
|
||||||
for range(10,0) |_i| {
|
for range(10,0) |_i| {
|
||||||
fail!("unreachable");
|
fail!(~"unreachable");
|
||||||
}
|
}
|
||||||
for range_rev(0,10) |_i| {
|
for range_rev(0,10) |_i| {
|
||||||
fail!("unreachable");
|
fail!(~"unreachable");
|
||||||
}
|
}
|
||||||
for range_step(10,0,1) |_i| {
|
for range_step(10,0,1) |_i| {
|
||||||
fail!("unreachable");
|
fail!(~"unreachable");
|
||||||
}
|
}
|
||||||
for range_step(0,10,-1) |_i| {
|
for range_step(0,10,-1) |_i| {
|
||||||
fail!("unreachable");
|
fail!(~"unreachable");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -927,3 +929,5 @@ mod tests {
|
||||||
for range_step(0,10,0) |_i| {}
|
for range_step(0,10,0) |_i| {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}))
|
14
src/libstd/num/u16.rs
Normal file
14
src/libstd/num/u16.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `u16`
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
uint_module!(u16, i16, 16)
|
14
src/libstd/num/u32.rs
Normal file
14
src/libstd/num/u32.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `u32`
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
uint_module!(u32, i32, 32)
|
14
src/libstd/num/u64.rs
Normal file
14
src/libstd/num/u64.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `u64`
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
uint_module!(u64, i64, 64)
|
14
src/libstd/num/u8.rs
Normal file
14
src/libstd/num/u8.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `u8`
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
uint_module!(u8, i8, 8)
|
|
@ -1,43 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `u16`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = u16;
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type T_SIGNED = i16;
|
|
||||||
pub static bits: uint = 16;
|
|
||||||
|
|
||||||
impl Primitive for u16 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 16 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<u16>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for u16 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> u16 { unsafe { intrinsics::ctpop16(*self as i16) as u16 } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> u16 { unsafe { intrinsics::ctlz16(*self as i16) as u16 } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> u16 { unsafe { intrinsics::cttz16(*self as i16) as u16 } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `u32`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = u32;
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type T_SIGNED = i32;
|
|
||||||
pub static bits: uint = 32;
|
|
||||||
|
|
||||||
impl Primitive for u32 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 32 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<u32>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for u32 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> u32 { unsafe { intrinsics::ctpop32(*self as i32) as u32 } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlp` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> u32 { unsafe { intrinsics::ctlz32(*self as i32) as u32 } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttp` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> u32 { unsafe { intrinsics::cttz32(*self as i32) as u32 } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `u64`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = u64;
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type T_SIGNED = i64;
|
|
||||||
pub static bits: uint = 64;
|
|
||||||
|
|
||||||
impl Primitive for u64 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 64 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<u64>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for u64 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> u64 { unsafe { intrinsics::ctpop64(*self as i64) as u64 } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> u64 { unsafe { intrinsics::ctlz64(*self as i64) as u64 } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> u64 { unsafe { intrinsics::cttz64(*self as i64) as u64 } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `u8`
|
|
||||||
|
|
||||||
mod inst {
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use unstable::intrinsics;
|
|
||||||
|
|
||||||
pub type T = u8;
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type T_SIGNED = i8;
|
|
||||||
pub static bits: uint = 8;
|
|
||||||
|
|
||||||
impl Primitive for u8 {
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 8 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<u8>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitCount for u8 {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> u8 { unsafe { intrinsics::ctpop8(*self as i8) as u8 } }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> u8 { unsafe { intrinsics::ctlz8(*self as i8) as u8 } }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> u8 { unsafe { intrinsics::cttz8(*self as i8) as u8 } }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,256 +0,0 @@
|
||||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Operations and constants for `uint`
|
|
||||||
|
|
||||||
pub use self::inst::{
|
|
||||||
div_ceil, div_round, div_floor, iterate,
|
|
||||||
next_power_of_two
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod inst {
|
|
||||||
use iter;
|
|
||||||
use num::{Primitive, BitCount};
|
|
||||||
use sys;
|
|
||||||
|
|
||||||
pub type T = uint;
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type T_SIGNED = int;
|
|
||||||
|
|
||||||
#[cfg(target_arch = "x86")]
|
|
||||||
#[cfg(target_arch = "arm")]
|
|
||||||
#[cfg(target_arch = "mips")]
|
|
||||||
pub static bits: uint = 32;
|
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
|
||||||
pub static bits: uint = 64;
|
|
||||||
|
|
||||||
impl Primitive for uint {
|
|
||||||
#[cfg(target_word_size = "32")]
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 32 }
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "64")]
|
|
||||||
#[inline(always)]
|
|
||||||
fn bits() -> uint { 64 }
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn bytes() -> uint { Primitive::bits::<uint>() / 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "32")]
|
|
||||||
#[inline(always)]
|
|
||||||
impl BitCount for uint {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> uint { (*self as i32).population_count() as uint }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> uint { (*self as i32).leading_zeros() as uint }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> uint { (*self as i32).trailing_zeros() as uint }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_word_size = "64")]
|
|
||||||
#[inline(always)]
|
|
||||||
impl BitCount for uint {
|
|
||||||
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn population_count(&self) -> uint { (*self as i64).population_count() as uint }
|
|
||||||
|
|
||||||
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn leading_zeros(&self) -> uint { (*self as i64).leading_zeros() as uint }
|
|
||||||
|
|
||||||
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
|
||||||
#[inline(always)]
|
|
||||||
fn trailing_zeros(&self) -> uint { (*self as i64).trailing_zeros() as uint }
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Divide two numbers, return the result, rounded up.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * x - an integer
|
|
||||||
/// * y - an integer distinct from 0u
|
|
||||||
///
|
|
||||||
/// # Return value
|
|
||||||
///
|
|
||||||
/// The smallest integer `q` such that `x/y <= q`.
|
|
||||||
///
|
|
||||||
pub fn div_ceil(x: uint, y: uint) -> uint {
|
|
||||||
let div = x / y;
|
|
||||||
if x % y == 0u { div }
|
|
||||||
else { div + 1u }
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Divide two numbers, return the result, rounded to the closest integer.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * x - an integer
|
|
||||||
/// * y - an integer distinct from 0u
|
|
||||||
///
|
|
||||||
/// # Return value
|
|
||||||
///
|
|
||||||
/// The integer `q` closest to `x/y`.
|
|
||||||
///
|
|
||||||
pub fn div_round(x: uint, y: uint) -> uint {
|
|
||||||
let div = x / y;
|
|
||||||
if x % y * 2u < y { div }
|
|
||||||
else { div + 1u }
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Divide two numbers, return the result, rounded down.
|
|
||||||
///
|
|
||||||
/// Note: This is the same function as `div`.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * x - an integer
|
|
||||||
/// * y - an integer distinct from 0u
|
|
||||||
///
|
|
||||||
/// # Return value
|
|
||||||
///
|
|
||||||
/// The smallest integer `q` such that `x/y <= q`. This
|
|
||||||
/// is either `x/y` or `x/y + 1`.
|
|
||||||
///
|
|
||||||
pub fn div_floor(x: uint, y: uint) -> uint { return x / y; }
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Iterate over the range [`lo`..`hi`), or stop when requested
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * lo - The integer at which to start the loop (included)
|
|
||||||
/// * hi - The integer at which to stop the loop (excluded)
|
|
||||||
/// * it - A block to execute with each consecutive integer of the range.
|
|
||||||
/// Return `true` to continue, `false` to stop.
|
|
||||||
///
|
|
||||||
/// # Return value
|
|
||||||
///
|
|
||||||
/// `true` If execution proceeded correctly, `false` if it was interrupted,
|
|
||||||
/// that is if `it` returned `false` at any point.
|
|
||||||
///
|
|
||||||
pub fn iterate(lo: uint, hi: uint, it: &fn(uint) -> bool) -> bool {
|
|
||||||
let mut i = lo;
|
|
||||||
while i < hi {
|
|
||||||
if (!it(i)) { return false; }
|
|
||||||
i += 1u;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl iter::Times for uint {
|
|
||||||
#[inline(always)]
|
|
||||||
///
|
|
||||||
/// A convenience form for basic iteration. Given a uint `x`,
|
|
||||||
/// `for x.times { ... }` executes the given block x times.
|
|
||||||
///
|
|
||||||
/// Equivalent to `for uint::range(0, x) |_| { ... }`.
|
|
||||||
///
|
|
||||||
/// Not defined on all integer types to permit unambiguous
|
|
||||||
/// use with integer literals of inferred integer-type as
|
|
||||||
/// the self-value (eg. `for 100.times { ... }`).
|
|
||||||
///
|
|
||||||
fn times(&self, it: &fn() -> bool) -> bool {
|
|
||||||
let mut i = *self;
|
|
||||||
while i > 0 {
|
|
||||||
if !it() { return false; }
|
|
||||||
i -= 1;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the smallest power of 2 greater than or equal to `n`
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn next_power_of_two(n: uint) -> uint {
|
|
||||||
let halfbits: uint = sys::size_of::<uint>() * 4u;
|
|
||||||
let mut tmp: uint = n - 1u;
|
|
||||||
let mut shift: uint = 1u;
|
|
||||||
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
|
|
||||||
return tmp + 1u;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_next_power_of_two() {
|
|
||||||
assert_eq!(next_power_of_two(0u), 0u);
|
|
||||||
assert_eq!(next_power_of_two(1u), 1u);
|
|
||||||
assert_eq!(next_power_of_two(2u), 2u);
|
|
||||||
assert_eq!(next_power_of_two(3u), 4u);
|
|
||||||
assert_eq!(next_power_of_two(4u), 4u);
|
|
||||||
assert_eq!(next_power_of_two(5u), 8u);
|
|
||||||
assert_eq!(next_power_of_two(6u), 8u);
|
|
||||||
assert_eq!(next_power_of_two(7u), 8u);
|
|
||||||
assert_eq!(next_power_of_two(8u), 8u);
|
|
||||||
assert_eq!(next_power_of_two(9u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(10u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(11u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(12u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(13u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(14u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(15u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(16u), 16u);
|
|
||||||
assert_eq!(next_power_of_two(17u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(18u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(19u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(20u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(21u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(22u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(23u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(24u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(25u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(26u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(27u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(28u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(29u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(30u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(31u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(32u), 32u);
|
|
||||||
assert_eq!(next_power_of_two(33u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(34u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(35u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(36u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(37u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(38u), 64u);
|
|
||||||
assert_eq!(next_power_of_two(39u), 64u);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_overflows() {
|
|
||||||
use uint;
|
|
||||||
assert!((uint::max_value > 0u));
|
|
||||||
assert!((uint::min_value <= 0u));
|
|
||||||
assert_eq!(uint::min_value + uint::max_value + 1u, 0u);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_div() {
|
|
||||||
assert_eq!(div_floor(3u, 4u), 0u);
|
|
||||||
assert_eq!(div_ceil(3u, 4u), 1u);
|
|
||||||
assert_eq!(div_round(3u, 4u), 1u);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn test_times() {
|
|
||||||
use iter::Times;
|
|
||||||
let ten = 10 as uint;
|
|
||||||
let mut accum = 0;
|
|
||||||
for ten.times { accum += 1; }
|
|
||||||
assert_eq!(accum, 10);
|
|
||||||
}
|
|
||||||
}
|
|
195
src/libstd/num/uint.rs
Normal file
195
src/libstd/num/uint.rs
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! Operations and constants for `uint`
|
||||||
|
|
||||||
|
use iter;
|
||||||
|
use sys;
|
||||||
|
|
||||||
|
pub use self::generated::*;
|
||||||
|
|
||||||
|
uint_module!(uint, int, ::int::bits)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Divide two numbers, return the result, rounded up.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * x - an integer
|
||||||
|
/// * y - an integer distinct from 0u
|
||||||
|
///
|
||||||
|
/// # Return value
|
||||||
|
///
|
||||||
|
/// The smallest integer `q` such that `x/y <= q`.
|
||||||
|
///
|
||||||
|
pub fn div_ceil(x: uint, y: uint) -> uint {
|
||||||
|
let div = x / y;
|
||||||
|
if x % y == 0u { div }
|
||||||
|
else { div + 1u }
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Divide two numbers, return the result, rounded to the closest integer.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * x - an integer
|
||||||
|
/// * y - an integer distinct from 0u
|
||||||
|
///
|
||||||
|
/// # Return value
|
||||||
|
///
|
||||||
|
/// The integer `q` closest to `x/y`.
|
||||||
|
///
|
||||||
|
pub fn div_round(x: uint, y: uint) -> uint {
|
||||||
|
let div = x / y;
|
||||||
|
if x % y * 2u < y { div }
|
||||||
|
else { div + 1u }
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Divide two numbers, return the result, rounded down.
|
||||||
|
///
|
||||||
|
/// Note: This is the same function as `div`.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * x - an integer
|
||||||
|
/// * y - an integer distinct from 0u
|
||||||
|
///
|
||||||
|
/// # Return value
|
||||||
|
///
|
||||||
|
/// The smallest integer `q` such that `x/y <= q`. This
|
||||||
|
/// is either `x/y` or `x/y + 1`.
|
||||||
|
///
|
||||||
|
pub fn div_floor(x: uint, y: uint) -> uint { return x / y; }
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Iterate over the range [`lo`..`hi`), or stop when requested
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * lo - The integer at which to start the loop (included)
|
||||||
|
/// * hi - The integer at which to stop the loop (excluded)
|
||||||
|
/// * it - A block to execute with each consecutive integer of the range.
|
||||||
|
/// Return `true` to continue, `false` to stop.
|
||||||
|
///
|
||||||
|
/// # Return value
|
||||||
|
///
|
||||||
|
/// `true` If execution proceeded correctly, `false` if it was interrupted,
|
||||||
|
/// that is if `it` returned `false` at any point.
|
||||||
|
///
|
||||||
|
pub fn iterate(lo: uint, hi: uint, it: &fn(uint) -> bool) -> bool {
|
||||||
|
let mut i = lo;
|
||||||
|
while i < hi {
|
||||||
|
if (!it(i)) { return false; }
|
||||||
|
i += 1u;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl iter::Times for uint {
|
||||||
|
#[inline(always)]
|
||||||
|
///
|
||||||
|
/// A convenience form for basic iteration. Given a uint `x`,
|
||||||
|
/// `for x.times { ... }` executes the given block x times.
|
||||||
|
///
|
||||||
|
/// Equivalent to `for uint::range(0, x) |_| { ... }`.
|
||||||
|
///
|
||||||
|
/// Not defined on all integer types to permit unambiguous
|
||||||
|
/// use with integer literals of inferred integer-type as
|
||||||
|
/// the self-value (eg. `for 100.times { ... }`).
|
||||||
|
///
|
||||||
|
fn times(&self, it: &fn() -> bool) -> bool {
|
||||||
|
let mut i = *self;
|
||||||
|
while i > 0 {
|
||||||
|
if !it() { return false; }
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the smallest power of 2 greater than or equal to `n`
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn next_power_of_two(n: uint) -> uint {
|
||||||
|
let halfbits: uint = sys::size_of::<uint>() * 4u;
|
||||||
|
let mut tmp: uint = n - 1u;
|
||||||
|
let mut shift: uint = 1u;
|
||||||
|
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
|
||||||
|
return tmp + 1u;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_next_power_of_two() {
|
||||||
|
assert!((next_power_of_two(0u) == 0u));
|
||||||
|
assert!((next_power_of_two(1u) == 1u));
|
||||||
|
assert!((next_power_of_two(2u) == 2u));
|
||||||
|
assert!((next_power_of_two(3u) == 4u));
|
||||||
|
assert!((next_power_of_two(4u) == 4u));
|
||||||
|
assert!((next_power_of_two(5u) == 8u));
|
||||||
|
assert!((next_power_of_two(6u) == 8u));
|
||||||
|
assert!((next_power_of_two(7u) == 8u));
|
||||||
|
assert!((next_power_of_two(8u) == 8u));
|
||||||
|
assert!((next_power_of_two(9u) == 16u));
|
||||||
|
assert!((next_power_of_two(10u) == 16u));
|
||||||
|
assert!((next_power_of_two(11u) == 16u));
|
||||||
|
assert!((next_power_of_two(12u) == 16u));
|
||||||
|
assert!((next_power_of_two(13u) == 16u));
|
||||||
|
assert!((next_power_of_two(14u) == 16u));
|
||||||
|
assert!((next_power_of_two(15u) == 16u));
|
||||||
|
assert!((next_power_of_two(16u) == 16u));
|
||||||
|
assert!((next_power_of_two(17u) == 32u));
|
||||||
|
assert!((next_power_of_two(18u) == 32u));
|
||||||
|
assert!((next_power_of_two(19u) == 32u));
|
||||||
|
assert!((next_power_of_two(20u) == 32u));
|
||||||
|
assert!((next_power_of_two(21u) == 32u));
|
||||||
|
assert!((next_power_of_two(22u) == 32u));
|
||||||
|
assert!((next_power_of_two(23u) == 32u));
|
||||||
|
assert!((next_power_of_two(24u) == 32u));
|
||||||
|
assert!((next_power_of_two(25u) == 32u));
|
||||||
|
assert!((next_power_of_two(26u) == 32u));
|
||||||
|
assert!((next_power_of_two(27u) == 32u));
|
||||||
|
assert!((next_power_of_two(28u) == 32u));
|
||||||
|
assert!((next_power_of_two(29u) == 32u));
|
||||||
|
assert!((next_power_of_two(30u) == 32u));
|
||||||
|
assert!((next_power_of_two(31u) == 32u));
|
||||||
|
assert!((next_power_of_two(32u) == 32u));
|
||||||
|
assert!((next_power_of_two(33u) == 64u));
|
||||||
|
assert!((next_power_of_two(34u) == 64u));
|
||||||
|
assert!((next_power_of_two(35u) == 64u));
|
||||||
|
assert!((next_power_of_two(36u) == 64u));
|
||||||
|
assert!((next_power_of_two(37u) == 64u));
|
||||||
|
assert!((next_power_of_two(38u) == 64u));
|
||||||
|
assert!((next_power_of_two(39u) == 64u));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_overflows() {
|
||||||
|
use uint;
|
||||||
|
assert!((uint::max_value > 0u));
|
||||||
|
assert!((uint::min_value <= 0u));
|
||||||
|
assert!((uint::min_value + uint::max_value + 1u == 0u));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_div() {
|
||||||
|
assert!((div_floor(3u, 4u) == 0u));
|
||||||
|
assert!((div_ceil(3u, 4u) == 1u));
|
||||||
|
assert!((div_round(3u, 4u) == 1u));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_times() {
|
||||||
|
use iter::Times;
|
||||||
|
let ten = 10 as uint;
|
||||||
|
let mut accum = 0;
|
||||||
|
for ten.times { accum += 1; }
|
||||||
|
assert!((accum == 10));
|
||||||
|
}
|
|
@ -8,53 +8,54 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use T = self::inst::T;
|
// FIXME(#4375): this shouldn't have to be a nested module named 'generated'
|
||||||
use T_SIGNED = self::inst::T_SIGNED;
|
|
||||||
|
|
||||||
|
#[macro_escape];
|
||||||
|
|
||||||
|
macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (mod generated {
|
||||||
|
|
||||||
|
use num::BitCount;
|
||||||
use num::{ToStrRadix, FromStrRadix};
|
use num::{ToStrRadix, FromStrRadix};
|
||||||
use num::{Zero, One, strconv};
|
use num::{Zero, One, strconv};
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
pub use cmp::{min, max};
|
pub use cmp::{min, max};
|
||||||
|
|
||||||
pub static bits : uint = inst::bits;
|
pub static bits : uint = $bits;
|
||||||
pub static bytes : uint = (inst::bits / 8);
|
pub static bytes : uint = ($bits / 8);
|
||||||
|
|
||||||
pub static min_value: T = 0 as T;
|
pub static min_value: $T = 0 as $T;
|
||||||
pub static max_value: T = 0 as T - 1 as T;
|
pub static max_value: $T = 0 as $T - 1 as $T;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn add(x: T, y: T) -> T { x + y }
|
pub fn add(x: $T, y: $T) -> $T { x + y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn sub(x: T, y: T) -> T { x - y }
|
pub fn sub(x: $T, y: $T) -> $T { x - y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn mul(x: T, y: T) -> T { x * y }
|
pub fn mul(x: $T, y: $T) -> $T { x * y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn div(x: T, y: T) -> T { x / y }
|
pub fn div(x: $T, y: $T) -> $T { x / y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn rem(x: T, y: T) -> T { x % y }
|
pub fn rem(x: $T, y: $T) -> $T { x % y }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn lt(x: T, y: T) -> bool { x < y }
|
pub fn lt(x: $T, y: $T) -> bool { x < y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn le(x: T, y: T) -> bool { x <= y }
|
pub fn le(x: $T, y: $T) -> bool { x <= y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn eq(x: T, y: T) -> bool { x == y }
|
pub fn eq(x: $T, y: $T) -> bool { x == y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn ne(x: T, y: T) -> bool { x != y }
|
pub fn ne(x: $T, y: $T) -> bool { x != y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn ge(x: T, y: T) -> bool { x >= y }
|
pub fn ge(x: $T, y: $T) -> bool { x >= y }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn gt(x: T, y: T) -> bool { x > y }
|
pub fn gt(x: $T, y: $T) -> bool { x > y }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
///
|
///
|
||||||
/// Iterate over the range [`start`,`start`+`step`..`stop`)
|
/// Iterate over the range [`start`,`start`+`step`..`stop`)
|
||||||
///
|
///
|
||||||
pub fn _range_step(start: T,
|
pub fn range_step(start: $T, stop: $T, step: $T_SIGNED, it: &fn($T) -> bool) -> bool {
|
||||||
stop: T,
|
|
||||||
step: T_SIGNED,
|
|
||||||
it: &fn(T) -> bool) -> bool {
|
|
||||||
let mut i = start;
|
let mut i = start;
|
||||||
if step == 0 {
|
if step == 0 {
|
||||||
fail!("range_step called with step == 0");
|
fail!("range_step called with step == 0");
|
||||||
|
@ -63,78 +64,74 @@ pub fn _range_step(start: T,
|
||||||
while i < stop {
|
while i < stop {
|
||||||
if !it(i) { return false; }
|
if !it(i) { return false; }
|
||||||
// avoiding overflow. break if i + step > max_value
|
// avoiding overflow. break if i + step > max_value
|
||||||
if i > max_value - (step as T) { return true; }
|
if i > max_value - (step as $T) { return true; }
|
||||||
i += step as T;
|
i += step as $T;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while i > stop {
|
while i > stop {
|
||||||
if !it(i) { return false; }
|
if !it(i) { return false; }
|
||||||
// avoiding underflow. break if i + step < min_value
|
// avoiding underflow. break if i + step < min_value
|
||||||
if i < min_value + ((-step) as T) { return true; }
|
if i < min_value + ((-step) as $T) { return true; }
|
||||||
i -= -step as T;
|
i -= -step as $T;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range_step(start: T, stop: T, step: T_SIGNED, it: &fn(T) -> bool) -> bool {
|
|
||||||
_range_step(start, stop, step, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Iterate over the range [`lo`..`hi`)
|
/// Iterate over the range [`lo`..`hi`)
|
||||||
pub fn range(lo: T, hi: T, it: &fn(T) -> bool) -> bool {
|
pub fn range(lo: $T, hi: $T, it: &fn($T) -> bool) -> bool {
|
||||||
range_step(lo, hi, 1 as T_SIGNED, it)
|
range_step(lo, hi, 1 as $T_SIGNED, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Iterate over the range [`hi`..`lo`)
|
/// Iterate over the range [`hi`..`lo`)
|
||||||
pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) -> bool {
|
pub fn range_rev(hi: $T, lo: $T, it: &fn($T) -> bool) -> bool {
|
||||||
range_step(hi, lo, -1 as T_SIGNED, it)
|
range_step(hi, lo, -1 as $T_SIGNED, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the bitwise complement
|
/// Computes the bitwise complement
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn compl(i: T) -> T {
|
pub fn compl(i: $T) -> $T {
|
||||||
max_value ^ i
|
max_value ^ i
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Num for T {}
|
impl Num for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Ord for T {
|
impl Ord for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lt(&self, other: &T) -> bool { (*self) < (*other) }
|
fn lt(&self, other: &$T) -> bool { (*self) < (*other) }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn le(&self, other: &T) -> bool { (*self) <= (*other) }
|
fn le(&self, other: &$T) -> bool { (*self) <= (*other) }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ge(&self, other: &T) -> bool { (*self) >= (*other) }
|
fn ge(&self, other: &$T) -> bool { (*self) >= (*other) }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn gt(&self, other: &T) -> bool { (*self) > (*other) }
|
fn gt(&self, other: &$T) -> bool { (*self) > (*other) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Eq for T {
|
impl Eq for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: &T) -> bool { return (*self) == (*other); }
|
fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
|
fn ne(&self, other: &$T) -> bool { return (*self) != (*other); }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Orderable for T {
|
impl Orderable for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn min(&self, other: &T) -> T {
|
fn min(&self, other: &$T) -> $T {
|
||||||
if *self < *other { *self } else { *other }
|
if *self < *other { *self } else { *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn max(&self, other: &T) -> T {
|
fn max(&self, other: &$T) -> $T {
|
||||||
if *self > *other { *self } else { *other }
|
if *self > *other { *self } else { *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number constrained within the range `mn <= self <= mx`.
|
/// Returns the number constrained within the range `mn <= self <= mx`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn clamp(&self, mn: &T, mx: &T) -> T {
|
fn clamp(&self, mn: &$T, mx: &$T) -> $T {
|
||||||
cond!(
|
cond!(
|
||||||
(*self > *mx) { *mx }
|
(*self > *mx) { *mx }
|
||||||
(*self < *mn) { *mn }
|
(*self < *mn) { *mn }
|
||||||
|
@ -143,81 +140,81 @@ impl Orderable for T {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Zero for T {
|
impl Zero for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn zero() -> T { 0 }
|
fn zero() -> $T { 0 }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn is_zero(&self) -> bool { *self == 0 }
|
fn is_zero(&self) -> bool { *self == 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl One for T {
|
impl One for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn one() -> T { 1 }
|
fn one() -> $T { 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Add<T,T> for T {
|
impl Add<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn add(&self, other: &T) -> T { *self + *other }
|
fn add(&self, other: &$T) -> $T { *self + *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Sub<T,T> for T {
|
impl Sub<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn sub(&self, other: &T) -> T { *self - *other }
|
fn sub(&self, other: &$T) -> $T { *self - *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Mul<T,T> for T {
|
impl Mul<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mul(&self, other: &T) -> T { *self * *other }
|
fn mul(&self, other: &$T) -> $T { *self * *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Div<T,T> for T {
|
impl Div<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div(&self, other: &T) -> T { *self / *other }
|
fn div(&self, other: &$T) -> $T { *self / *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Rem<T,T> for T {
|
impl Rem<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn rem(&self, other: &T) -> T { *self % *other }
|
fn rem(&self, other: &$T) -> $T { *self % *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Neg<T> for T {
|
impl Neg<$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn neg(&self) -> T { -*self }
|
fn neg(&self) -> $T { -*self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Unsigned for T {}
|
impl Unsigned for $T {}
|
||||||
|
|
||||||
impl Integer for T {
|
impl Integer for $T {
|
||||||
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
|
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_rem(&self, other: &T) -> (T,T) {
|
fn div_rem(&self, other: &$T) -> ($T,$T) {
|
||||||
(*self / *other, *self % *other)
|
(*self / *other, *self % *other)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unsigned integer division. Returns the same result as `div` (`/`).
|
/// Unsigned integer division. Returns the same result as `div` (`/`).
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_floor(&self, other: &T) -> T { *self / *other }
|
fn div_floor(&self, other: &$T) -> $T { *self / *other }
|
||||||
|
|
||||||
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
|
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mod_floor(&self, other: &T) -> T { *self / *other }
|
fn mod_floor(&self, other: &$T) -> $T { *self / *other }
|
||||||
|
|
||||||
/// Calculates `div_floor` and `modulo_floor` simultaneously
|
/// Calculates `div_floor` and `modulo_floor` simultaneously
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn div_mod_floor(&self, other: &T) -> (T,T) {
|
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
|
||||||
(*self / *other, *self % *other)
|
(*self / *other, *self % *other)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`
|
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn gcd(&self, other: &T) -> T {
|
fn gcd(&self, other: &$T) -> $T {
|
||||||
// Use Euclid's algorithm
|
// Use Euclid's algorithm
|
||||||
let mut m = *self, n = *other;
|
let mut m = *self, n = *other;
|
||||||
while m != 0 {
|
while m != 0 {
|
||||||
|
@ -230,13 +227,13 @@ impl Integer for T {
|
||||||
|
|
||||||
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lcm(&self, other: &T) -> T {
|
fn lcm(&self, other: &$T) -> $T {
|
||||||
(*self * *other) / self.gcd(other)
|
(*self * *other) / self.gcd(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
|
fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
|
||||||
|
|
||||||
/// Returns `true` if the number is divisible by `2`
|
/// Returns `true` if the number is divisible by `2`
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -247,87 +244,87 @@ impl Integer for T {
|
||||||
fn is_odd(&self) -> bool { !self.is_even() }
|
fn is_odd(&self) -> bool { !self.is_even() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bitwise for T {}
|
impl Bitwise for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitOr<T,T> for T {
|
impl BitOr<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitor(&self, other: &T) -> T { *self | *other }
|
fn bitor(&self, other: &$T) -> $T { *self | *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitAnd<T,T> for T {
|
impl BitAnd<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitand(&self, other: &T) -> T { *self & *other }
|
fn bitand(&self, other: &$T) -> $T { *self & *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl BitXor<T,T> for T {
|
impl BitXor<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn bitxor(&self, other: &T) -> T { *self ^ *other }
|
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Shl<T,T> for T {
|
impl Shl<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn shl(&self, other: &T) -> T { *self << *other }
|
fn shl(&self, other: &$T) -> $T { *self << *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Shr<T,T> for T {
|
impl Shr<$T,$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn shr(&self, other: &T) -> T { *self >> *other }
|
fn shr(&self, other: &$T) -> $T { *self >> *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl Not<T> for T {
|
impl Not<$T> for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn not(&self) -> T { !*self }
|
fn not(&self) -> $T { !*self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bounded for T {
|
impl Bounded for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn min_value() -> T { min_value }
|
fn min_value() -> $T { min_value }
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn max_value() -> T { max_value }
|
fn max_value() -> $T { max_value }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Int for T {}
|
impl Int for $T {}
|
||||||
|
|
||||||
// String conversion functions and impl str -> num
|
// String conversion functions and impl str -> num
|
||||||
|
|
||||||
/// Parse a string as a number in base 10.
|
/// Parse a string as a number in base 10.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn from_str(s: &str) -> Option<T> {
|
pub fn from_str(s: &str) -> Option<$T> {
|
||||||
strconv::from_str_common(s, 10u, false, false, false,
|
strconv::from_str_common(s, 10u, false, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a string as a number in the given base.
|
/// Parse a string as a number in the given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn from_str_radix(s: &str, radix: uint) -> Option<T> {
|
pub fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
|
||||||
strconv::from_str_common(s, radix, false, false, false,
|
strconv::from_str_common(s, radix, false, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a byte slice as a number in the given base.
|
/// Parse a byte slice as a number in the given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
|
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
|
||||||
strconv::from_str_bytes_common(buf, radix, false, false, false,
|
strconv::from_str_bytes_common(buf, radix, false, false, false,
|
||||||
strconv::ExpNone, false, false)
|
strconv::ExpNone, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for T {
|
impl FromStr for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_str(s: &str) -> Option<T> {
|
fn from_str(s: &str) -> Option<$T> {
|
||||||
from_str(s)
|
from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStrRadix for T {
|
impl FromStrRadix for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_str_radix(s: &str, radix: uint) -> Option<T> {
|
fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
|
||||||
from_str_radix(s, radix)
|
from_str_radix(s, radix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -336,7 +333,7 @@ impl FromStrRadix for T {
|
||||||
|
|
||||||
/// Convert to a string as a byte slice in a given base.
|
/// Convert to a string as a byte slice in a given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str_bytes<U>(n: T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
pub fn to_str_bytes<U>(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
||||||
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
|
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
f(buf)
|
f(buf)
|
||||||
|
@ -344,7 +341,7 @@ pub fn to_str_bytes<U>(n: T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
|
||||||
|
|
||||||
/// Convert to a string in base 10.
|
/// Convert to a string in base 10.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str(num: T) -> ~str {
|
pub fn to_str(num: $T) -> ~str {
|
||||||
let (buf, _) = strconv::to_str_common(&num, 10u, false,
|
let (buf, _) = strconv::to_str_common(&num, 10u, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
buf
|
buf
|
||||||
|
@ -352,149 +349,176 @@ pub fn to_str(num: T) -> ~str {
|
||||||
|
|
||||||
/// Convert to a string in a given base.
|
/// Convert to a string in a given base.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_str_radix(num: T, radix: uint) -> ~str {
|
pub fn to_str_radix(num: $T, radix: uint) -> ~str {
|
||||||
let (buf, _) = strconv::to_str_common(&num, radix, false,
|
let (buf, _) = strconv::to_str_common(&num, radix, false,
|
||||||
strconv::SignNeg, strconv::DigAll);
|
strconv::SignNeg, strconv::DigAll);
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToStr for T {
|
impl ToStr for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_str(&self) -> ~str {
|
fn to_str(&self) -> ~str {
|
||||||
to_str(*self)
|
to_str(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToStrRadix for T {
|
impl ToStrRadix for $T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_str_radix(&self, radix: uint) -> ~str {
|
fn to_str_radix(&self, radix: uint) -> ~str {
|
||||||
to_str_radix(*self, radix)
|
to_str_radix(*self, radix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Primitive for $T {
|
||||||
|
#[inline(always)]
|
||||||
|
fn bits() -> uint { bits }
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn bytes() -> uint { bits / 8 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BitCount for $T {
|
||||||
|
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn population_count(&self) -> $T {
|
||||||
|
(*self as $T_SIGNED).population_count() as $T
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn leading_zeros(&self) -> $T {
|
||||||
|
(*self as $T_SIGNED).leading_zeros() as $T
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
|
||||||
|
#[inline(always)]
|
||||||
|
fn trailing_zeros(&self) -> $T {
|
||||||
|
(*self as $T_SIGNED).trailing_zeros() as $T
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use super::inst::T;
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_num() {
|
fn test_num() {
|
||||||
num::test_num(10 as T, 2 as T);
|
num::test_num(10 as $T, 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_orderable() {
|
fn test_orderable() {
|
||||||
assert_eq!((1 as T).min(&(2 as T)), 1 as T);
|
assert_eq!((1 as $T).min(&(2 as $T)), 1 as $T);
|
||||||
assert_eq!((2 as T).min(&(1 as T)), 1 as T);
|
assert_eq!((2 as $T).min(&(1 as $T)), 1 as $T);
|
||||||
assert_eq!((1 as T).max(&(2 as T)), 2 as T);
|
assert_eq!((1 as $T).max(&(2 as $T)), 2 as $T);
|
||||||
assert_eq!((2 as T).max(&(1 as T)), 2 as T);
|
assert_eq!((2 as $T).max(&(1 as $T)), 2 as $T);
|
||||||
assert_eq!((1 as T).clamp(&(2 as T), &(4 as T)), 2 as T);
|
assert_eq!((1 as $T).clamp(&(2 as $T), &(4 as $T)), 2 as $T);
|
||||||
assert_eq!((8 as T).clamp(&(2 as T), &(4 as T)), 4 as T);
|
assert_eq!((8 as $T).clamp(&(2 as $T), &(4 as $T)), 4 as $T);
|
||||||
assert_eq!((3 as T).clamp(&(2 as T), &(4 as T)), 3 as T);
|
assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gcd() {
|
fn test_gcd() {
|
||||||
assert_eq!((10 as T).gcd(&2), 2 as T);
|
assert_eq!((10 as $T).gcd(&2), 2 as $T);
|
||||||
assert_eq!((10 as T).gcd(&3), 1 as T);
|
assert_eq!((10 as $T).gcd(&3), 1 as $T);
|
||||||
assert_eq!((0 as T).gcd(&3), 3 as T);
|
assert_eq!((0 as $T).gcd(&3), 3 as $T);
|
||||||
assert_eq!((3 as T).gcd(&3), 3 as T);
|
assert_eq!((3 as $T).gcd(&3), 3 as $T);
|
||||||
assert_eq!((56 as T).gcd(&42), 14 as T);
|
assert_eq!((56 as $T).gcd(&42), 14 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lcm() {
|
fn test_lcm() {
|
||||||
assert_eq!((1 as T).lcm(&0), 0 as T);
|
assert_eq!((1 as $T).lcm(&0), 0 as $T);
|
||||||
assert_eq!((0 as T).lcm(&1), 0 as T);
|
assert_eq!((0 as $T).lcm(&1), 0 as $T);
|
||||||
assert_eq!((1 as T).lcm(&1), 1 as T);
|
assert_eq!((1 as $T).lcm(&1), 1 as $T);
|
||||||
assert_eq!((8 as T).lcm(&9), 72 as T);
|
assert_eq!((8 as $T).lcm(&9), 72 as $T);
|
||||||
assert_eq!((11 as T).lcm(&5), 55 as T);
|
assert_eq!((11 as $T).lcm(&5), 55 as $T);
|
||||||
assert_eq!((99 as T).lcm(&17), 1683 as T);
|
assert_eq!((99 as $T).lcm(&17), 1683 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multiple_of() {
|
fn test_multiple_of() {
|
||||||
assert!((6 as T).is_multiple_of(&(6 as T)));
|
assert!((6 as $T).is_multiple_of(&(6 as $T)));
|
||||||
assert!((6 as T).is_multiple_of(&(3 as T)));
|
assert!((6 as $T).is_multiple_of(&(3 as $T)));
|
||||||
assert!((6 as T).is_multiple_of(&(1 as T)));
|
assert!((6 as $T).is_multiple_of(&(1 as $T)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_even() {
|
fn test_even() {
|
||||||
assert_eq!((0 as T).is_even(), true);
|
assert_eq!((0 as $T).is_even(), true);
|
||||||
assert_eq!((1 as T).is_even(), false);
|
assert_eq!((1 as $T).is_even(), false);
|
||||||
assert_eq!((2 as T).is_even(), true);
|
assert_eq!((2 as $T).is_even(), true);
|
||||||
assert_eq!((3 as T).is_even(), false);
|
assert_eq!((3 as $T).is_even(), false);
|
||||||
assert_eq!((4 as T).is_even(), true);
|
assert_eq!((4 as $T).is_even(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_odd() {
|
fn test_odd() {
|
||||||
assert_eq!((0 as T).is_odd(), false);
|
assert_eq!((0 as $T).is_odd(), false);
|
||||||
assert_eq!((1 as T).is_odd(), true);
|
assert_eq!((1 as $T).is_odd(), true);
|
||||||
assert_eq!((2 as T).is_odd(), false);
|
assert_eq!((2 as $T).is_odd(), false);
|
||||||
assert_eq!((3 as T).is_odd(), true);
|
assert_eq!((3 as $T).is_odd(), true);
|
||||||
assert_eq!((4 as T).is_odd(), false);
|
assert_eq!((4 as $T).is_odd(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bitwise() {
|
fn test_bitwise() {
|
||||||
assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));
|
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
|
||||||
assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(0b1010 as T)));
|
assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
|
||||||
assert_eq!(0b0110 as T, (0b1100 as T).bitxor(&(0b1010 as T)));
|
assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
||||||
assert_eq!(0b1110 as T, (0b0111 as T).shl(&(1 as T)));
|
assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
|
||||||
assert_eq!(0b0111 as T, (0b1110 as T).shr(&(1 as T)));
|
assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
|
||||||
assert_eq!(max_value - (0b1011 as T), (0b1011 as T).not());
|
assert_eq!(max_value - (0b1011 as $T), (0b1011 as $T).not());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bitcount() {
|
fn test_bitcount() {
|
||||||
assert_eq!((0b010101 as T).population_count(), 3);
|
assert_eq!((0b010101 as $T).population_count(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_primitive() {
|
fn test_primitive() {
|
||||||
assert_eq!(Primitive::bits::<T>(), sys::size_of::<T>() * 8);
|
assert_eq!(Primitive::bits::<$T>(), sys::size_of::<$T>() * 8);
|
||||||
assert_eq!(Primitive::bytes::<T>(), sys::size_of::<T>());
|
assert_eq!(Primitive::bytes::<$T>(), sys::size_of::<$T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_to_str() {
|
pub fn test_to_str() {
|
||||||
assert_eq!(to_str_radix(0 as T, 10u), ~"0");
|
assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
|
||||||
assert_eq!(to_str_radix(1 as T, 10u), ~"1");
|
assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
|
||||||
assert_eq!(to_str_radix(2 as T, 10u), ~"2");
|
assert_eq!(to_str_radix(2 as $T, 10u), ~"2");
|
||||||
assert_eq!(to_str_radix(11 as T, 10u), ~"11");
|
assert_eq!(to_str_radix(11 as $T, 10u), ~"11");
|
||||||
assert_eq!(to_str_radix(11 as T, 16u), ~"b");
|
assert_eq!(to_str_radix(11 as $T, 16u), ~"b");
|
||||||
assert_eq!(to_str_radix(255 as T, 16u), ~"ff");
|
assert_eq!(to_str_radix(255 as $T, 16u), ~"ff");
|
||||||
assert_eq!(to_str_radix(0xff as T, 10u), ~"255");
|
assert_eq!(to_str_radix(0xff as $T, 10u), ~"255");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_from_str() {
|
pub fn test_from_str() {
|
||||||
assert_eq!(from_str("0"), Some(0u as T));
|
assert_eq!(from_str(~"0"), Some(0u as $T));
|
||||||
assert_eq!(from_str("3"), Some(3u as T));
|
assert_eq!(from_str(~"3"), Some(3u as $T));
|
||||||
assert_eq!(from_str("10"), Some(10u as T));
|
assert_eq!(from_str(~"10"), Some(10u as $T));
|
||||||
assert_eq!(u32::from_str("123456789"), Some(123456789 as u32));
|
assert_eq!(u32::from_str(~"123456789"), Some(123456789 as u32));
|
||||||
assert_eq!(from_str("00100"), Some(100u as T));
|
assert_eq!(from_str(~"00100"), Some(100u as $T));
|
||||||
|
|
||||||
assert!(from_str("").is_none());
|
assert!(from_str(~"").is_none());
|
||||||
assert!(from_str(" ").is_none());
|
assert!(from_str(~" ").is_none());
|
||||||
assert!(from_str("x").is_none());
|
assert!(from_str(~"x").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_parse_bytes() {
|
pub fn test_parse_bytes() {
|
||||||
use str::to_bytes;
|
use str::to_bytes;
|
||||||
assert_eq!(parse_bytes(to_bytes("123"), 10u), Some(123u as T));
|
assert_eq!(parse_bytes(to_bytes(~"123"), 10u), Some(123u as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("1001"), 2u), Some(9u as T));
|
assert_eq!(parse_bytes(to_bytes(~"1001"), 2u), Some(9u as $T));
|
||||||
assert_eq!(parse_bytes(to_bytes("123"), 8u), Some(83u as T));
|
assert_eq!(parse_bytes(to_bytes(~"123"), 8u), Some(83u as $T));
|
||||||
assert_eq!(u16::parse_bytes(to_bytes("123"), 16u), Some(291u as u16));
|
assert_eq!(u16::parse_bytes(to_bytes(~"123"), 16u), Some(291u as u16));
|
||||||
assert_eq!(u16::parse_bytes(to_bytes("ffff"), 16u), Some(65535u as u16));
|
assert_eq!(u16::parse_bytes(to_bytes(~"ffff"), 16u), Some(65535u as u16));
|
||||||
assert_eq!(parse_bytes(to_bytes("z"), 36u), Some(35u as T));
|
assert_eq!(parse_bytes(to_bytes(~"z"), 36u), Some(35u as $T));
|
||||||
|
|
||||||
assert!(parse_bytes(to_bytes("Z"), 10u).is_none());
|
assert!(parse_bytes(to_bytes(~"Z"), 10u).is_none());
|
||||||
assert!(parse_bytes(to_bytes("_"), 2u).is_none());
|
assert!(parse_bytes(to_bytes(~"_"), 2u).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -527,36 +551,36 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uint_from_str_overflow() {
|
fn test_uint_from_str_overflow() {
|
||||||
let mut u8_val: u8 = 255_u8;
|
let mut u8_val: u8 = 255_u8;
|
||||||
assert_eq!(u8::from_str("255"), Some(u8_val));
|
assert_eq!(u8::from_str(~"255"), Some(u8_val));
|
||||||
assert!(u8::from_str("256").is_none());
|
assert!(u8::from_str(~"256").is_none());
|
||||||
|
|
||||||
u8_val += 1 as u8;
|
u8_val += 1 as u8;
|
||||||
assert_eq!(u8::from_str("0"), Some(u8_val));
|
assert_eq!(u8::from_str(~"0"), Some(u8_val));
|
||||||
assert!(u8::from_str("-1").is_none());
|
assert!(u8::from_str(~"-1").is_none());
|
||||||
|
|
||||||
let mut u16_val: u16 = 65_535_u16;
|
let mut u16_val: u16 = 65_535_u16;
|
||||||
assert_eq!(u16::from_str("65535"), Some(u16_val));
|
assert_eq!(u16::from_str(~"65535"), Some(u16_val));
|
||||||
assert!(u16::from_str("65536").is_none());
|
assert!(u16::from_str(~"65536").is_none());
|
||||||
|
|
||||||
u16_val += 1 as u16;
|
u16_val += 1 as u16;
|
||||||
assert_eq!(u16::from_str("0"), Some(u16_val));
|
assert_eq!(u16::from_str(~"0"), Some(u16_val));
|
||||||
assert!(u16::from_str("-1").is_none());
|
assert!(u16::from_str(~"-1").is_none());
|
||||||
|
|
||||||
let mut u32_val: u32 = 4_294_967_295_u32;
|
let mut u32_val: u32 = 4_294_967_295_u32;
|
||||||
assert_eq!(u32::from_str("4294967295"), Some(u32_val));
|
assert_eq!(u32::from_str(~"4294967295"), Some(u32_val));
|
||||||
assert!(u32::from_str("4294967296").is_none());
|
assert!(u32::from_str(~"4294967296").is_none());
|
||||||
|
|
||||||
u32_val += 1 as u32;
|
u32_val += 1 as u32;
|
||||||
assert_eq!(u32::from_str("0"), Some(u32_val));
|
assert_eq!(u32::from_str(~"0"), Some(u32_val));
|
||||||
assert!(u32::from_str("-1").is_none());
|
assert!(u32::from_str(~"-1").is_none());
|
||||||
|
|
||||||
let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
|
let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
|
||||||
assert_eq!(u64::from_str("18446744073709551615"), Some(u64_val));
|
assert_eq!(u64::from_str(~"18446744073709551615"), Some(u64_val));
|
||||||
assert!(u64::from_str("18446744073709551616").is_none());
|
assert!(u64::from_str(~"18446744073709551616").is_none());
|
||||||
|
|
||||||
u64_val += 1 as u64;
|
u64_val += 1 as u64;
|
||||||
assert_eq!(u64::from_str("0"), Some(u64_val));
|
assert_eq!(u64::from_str(~"0"), Some(u64_val));
|
||||||
assert!(u64::from_str("-1").is_none());
|
assert!(u64::from_str(~"-1").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -639,3 +663,5 @@ mod tests {
|
||||||
for range_step(0,-10,0) |_i| {}
|
for range_step(0,-10,0) |_i| {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}))
|
Loading…
Add table
Add a link
Reference in a new issue