Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] num: bitwise reflection methods #13209

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libstd/num/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl Bitwise for i16 {
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }

/// Returns the bitwise reflected representation of the number.
fn reflect(&self) -> i16 {
let mut r = 0;
for i in range(0, 16) {
let digit = (*self >> i) & 1;
r = r | (digit << (15-i));
}
return r;
}
}

impl CheckedAdd for i16 {
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/num/i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl Bitwise for i32 {
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }

/// Returns the bitwise reflected representation of the number.
fn reflect(&self) -> i32 {
let mut r = 0;
for i in range(0, 32) {
let digit = (*self >> i) & 1;
r = r | (digit << (31-i));
}
return r;
}
}

impl CheckedAdd for i32 {
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/num/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ impl Bitwise for i64 {
/// Counts the number of trailing zeros.
#[inline]
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }

/// Returns the bitwise reflected representation of the number.
fn reflect(&self) -> i64 {
let mut r = 0;
for i in range(0, 64) {
let digit = (*self >> i) & 1;
r = r | (digit << (63-i));
}
return r;
}
}

impl CheckedAdd for i64 {
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/num/i8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl Bitwise for i8 {
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }

/// Returns the bitwise reflected representation of the number.
fn reflect(&self) -> i8 {
let mut r = 0;
for i in range(0, 8) {
let digit = (*self >> i) & 1;
r = r | (digit << (7-i));
}
return r;
}
}

impl CheckedAdd for i8 {
Expand Down
8 changes: 8 additions & 0 deletions src/libstd/num/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl Bitwise for int {
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }

/// Returns the bitwise reflected representation of the number.
#[inline]
fn reflect(&self) -> int { (*self as i32).reflect() as int }
}

#[cfg(target_word_size = "64")]
Expand All @@ -58,6 +62,10 @@ impl Bitwise for int {
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }

/// Returns the bitwise reflected representation of the number.
#[inline]
fn reflect(&self) -> int { (*self as i64).reflect() as int }
}

#[cfg(target_word_size = "32")]
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ mod tests {
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
}

#[test]
fn test_bitwise_reflect() {
assert_eq!((0b01011001 as $T).reflect(), (0b10011010 as $T) << (BITS - 8));
assert_eq!((0b01000011 as $T).reflect(), (0b11000010 as $T) << (BITS - 8));
assert_eq!((0b11110011 as $T).reflect(), (0b11001111 as $T) << (BITS - 8));
}

#[test]
fn test_from_str() {
assert_eq!(from_str::<$T>("0"), Some(0 as $T));
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ pub trait Bitwise: Bounded
/// assert_eq!(n.trailing_zeros(), 3);
/// ```
fn trailing_zeros(&self) -> Self;

/// Returns the bitwise reflected representation of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b11010001u8;
/// assert_eq!(n.reflect(), 0b10001011u8);
/// ```
fn reflect(&self) -> Self;
}

/// Specifies the available operations common to all of Rust's core numeric primitives.
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ impl Bitwise for $T {
fn trailing_zeros(&self) -> $T {
(*self as $T_SIGNED).trailing_zeros() as $T
}

/// Returns the bitwise reflected representation of the number.
fn reflect(&self) -> $T {
(*self as $T_SIGNED).reflect() as $T
}
}

#[cfg(test)]
Expand Down Expand Up @@ -272,6 +277,13 @@ mod tests {
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
}

#[test]
fn test_bitwise_reflect() {
assert_eq!((0b01011001 as $T).reflect(), (0b10011010 as $T) << (BITS - 8));
assert_eq!((0b01000011 as $T).reflect(), (0b11000010 as $T) << (BITS - 8));
assert_eq!((0b11110011 as $T).reflect(), (0b11001111 as $T) << (BITS - 8));
}

#[test]
pub fn test_to_str() {
assert_eq!((0 as $T).to_str_radix(10u), ~"0");
Expand Down