From acc57a44fde9e60163f3d4700bdca453f8a23a11 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 12 Feb 2012 21:58:06 -0800 Subject: [PATCH] core: Add compl functions for the rest of the integer types --- src/libcore/i16.rs | 5 +++++ src/libcore/i32.rs | 5 +++++ src/libcore/i64.rs | 5 +++++ src/libcore/i8.rs | 5 +++++ src/libcore/int.rs | 5 +++++ src/libcore/u16.rs | 5 +++++ src/libcore/u32.rs | 5 +++++ src/libcore/u64.rs | 5 +++++ src/libcore/u8.rs | 5 +++++ src/libcore/uint.rs | 2 +- 10 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/libcore/i16.rs b/src/libcore/i16.rs index 5962e8f8c28..1a4bc209902 100644 --- a/src/libcore/i16.rs +++ b/src/libcore/i16.rs @@ -29,3 +29,8 @@ fn range(lo: i16, hi: i16, it: fn(i16)) { let i = lo; while i < hi { it(i); i += 1i16; } } + +#[doc = "Computes the bitwise complement"] +fn compl(i: i16) -> i16 { + u16::compl(i as u16) as i16 +} diff --git a/src/libcore/i32.rs b/src/libcore/i32.rs index c63d8472d2c..9e20712fdcc 100644 --- a/src/libcore/i32.rs +++ b/src/libcore/i32.rs @@ -29,3 +29,8 @@ fn range(lo: i32, hi: i32, it: fn(i32)) { let i = lo; while i < hi { it(i); i += 1i32; } } + +#[doc = "Computes the bitwise complement"] +fn compl(i: i32) -> i32 { + u32::compl(i as u32) as i32 +} diff --git a/src/libcore/i64.rs b/src/libcore/i64.rs index 16c09d34f63..97c746b7b11 100644 --- a/src/libcore/i64.rs +++ b/src/libcore/i64.rs @@ -29,3 +29,8 @@ fn range(lo: i64, hi: i64, it: fn(i64)) { let i = lo; while i < hi { it(i); i += 1i64; } } + +#[doc = "Computes the bitwise complement"] +fn compl(i: i64) -> i64 { + u64::compl(i as u64) as i64 +} diff --git a/src/libcore/i8.rs b/src/libcore/i8.rs index 319231a4a34..96fdbf9934f 100644 --- a/src/libcore/i8.rs +++ b/src/libcore/i8.rs @@ -29,3 +29,8 @@ fn range(lo: i8, hi: i8, it: fn(i8)) { let i = lo; while i < hi { it(i); i += 1i8; } } + +#[doc = "Computes the bitwise complement"] +fn compl(i: i8) -> i8 { + u8::compl(i as u8) as i8 +} diff --git a/src/libcore/int.rs b/src/libcore/int.rs index 5484cb576a2..8b32450e36c 100644 --- a/src/libcore/int.rs +++ b/src/libcore/int.rs @@ -186,6 +186,11 @@ fn pow(base: int, exponent: uint) -> int { ret acc; } +#[doc = "Computes the bitwise complement"] +fn compl(i: int) -> int { + uint::compl(i as uint) as int +} + #[test] fn test_from_str() { assert(from_str("0") == 0); diff --git a/src/libcore/u16.rs b/src/libcore/u16.rs index 6e28b12b7e1..b038f525201 100644 --- a/src/libcore/u16.rs +++ b/src/libcore/u16.rs @@ -29,3 +29,8 @@ fn range(lo: u16, hi: u16, it: fn(u16)) { let i = lo; while i < hi { it(i); i += 1u16; } } + +#[doc = "Computes the bitwise complement"] +fn compl(i: u16) -> u16 { + max_value ^ i +} diff --git a/src/libcore/u32.rs b/src/libcore/u32.rs index 1837e20b1f6..2fb393b6577 100644 --- a/src/libcore/u32.rs +++ b/src/libcore/u32.rs @@ -64,6 +64,11 @@ fn range(lo: u32, hi: u32, it: fn(u32)) { while i < hi { it(i); i += 1u32; } } +#[doc = "Computes the bitwise complement"] +fn compl(i: u32) -> u32 { + max_value ^ i +} + // // Local Variables: // mode: rust diff --git a/src/libcore/u64.rs b/src/libcore/u64.rs index e169b6192db..4c76e79bd0a 100644 --- a/src/libcore/u64.rs +++ b/src/libcore/u64.rs @@ -134,3 +134,8 @@ fn from_str(buf: str, radix: u64) -> u64 { } fail; } + +#[doc = "Computes the bitwise complement"] +fn compl(i: u64) -> u64 { + max_value ^ i +} diff --git a/src/libcore/u8.rs b/src/libcore/u8.rs index 7cb55d4efa5..03eead6b763 100644 --- a/src/libcore/u8.rs +++ b/src/libcore/u8.rs @@ -64,6 +64,11 @@ fn range(lo: u8, hi: u8, it: fn(u8)) { while i < hi { it(i); i += 1u8; } } +#[doc = "Computes the bitwise complement"] +fn compl(i: u8) -> u8 { + max_value ^ i +} + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/libcore/uint.rs b/src/libcore/uint.rs index 22b2a95f3cb..ee2ec7dff40 100644 --- a/src/libcore/uint.rs +++ b/src/libcore/uint.rs @@ -274,7 +274,7 @@ Function: compl Computes the bitwise complement. */ fn compl(i: uint) -> uint { - uint::max_value ^ i + max_value ^ i } #[cfg(test)]