about summary refs log tree commit diff
path: root/src/libcore/num
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-06-14 22:53:55 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-06-18 17:01:34 -0700
commit87c529c43a41e0c04a468cd2b301013df629b040 (patch)
treee6e5397fe42d407f92a0683303f13ca88eb561a6 /src/libcore/num
parent1273f94cbb19608e6a4e02c54d8543f6fe648a47 (diff)
downloadrust-87c529c43a41e0c04a468cd2b301013df629b040.tar.gz
rust-87c529c43a41e0c04a468cd2b301013df629b040.zip
Add a ByteOrder trait for abstracting over endian conversions
The `Bitwise::swap_bytes` method was also moved into the `ByteOrder` trait. This was because it works on the byte level rather than the bit level.
Diffstat (limited to 'src/libcore/num')
-rw-r--r--src/libcore/num/int_macros.rs11
-rw-r--r--src/libcore/num/mod.rs72
-rw-r--r--src/libcore/num/uint_macros.rs11
3 files changed, 24 insertions, 70 deletions
diff --git a/src/libcore/num/int_macros.rs b/src/libcore/num/int_macros.rs
index 20bb12db694..8a1bd66aa1a 100644
--- a/src/libcore/num/int_macros.rs
+++ b/src/libcore/num/int_macros.rs
@@ -114,17 +114,6 @@ mod tests {
     }
 
     #[test]
-    fn test_swap_bytes() {
-        let n: $T = 0b0101100; assert_eq!(n.swap_bytes().swap_bytes(), n);
-        let n: $T = 0b0100001; assert_eq!(n.swap_bytes().swap_bytes(), n);
-        let n: $T = 0b1111001; assert_eq!(n.swap_bytes().swap_bytes(), n);
-
-        // Swapping these should make no difference
-        let n: $T = 0;   assert_eq!(n.swap_bytes(), n);
-        let n: $T = -1;  assert_eq!(n.swap_bytes(), n);
-    }
-
-    #[test]
     fn test_rotate() {
         let n: $T = 0b0101100; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
         let n: $T = 0b0100001; assert_eq!(n.rotate_left(3).rotate_left(2).rotate_right(5),  n);
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index eaa632be6d0..696abc05ed2 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -437,19 +437,6 @@ pub trait Bitwise: Bounded
     /// ```
     fn trailing_zeros(&self) -> Self;
 
-    /// Reverses the byte order of a binary number.
-    ///
-    /// # Example
-    ///
-    /// ```rust
-    /// use std::num::Bitwise;
-    ///
-    /// let n = 0x0123456789ABCDEFu64;
-    /// let m = 0xEFCDAB8967452301u64;
-    /// assert_eq!(n.swap_bytes(), m);
-    /// ```
-    fn swap_bytes(&self) -> Self;
-
     /// Shifts the bits to the left by a specified amount amount, `r`, wrapping
     /// the truncated bits to the end of the resulting value.
     ///
@@ -479,25 +466,17 @@ pub trait Bitwise: Bounded
     fn rotate_right(&self, r: uint) -> Self;
 }
 
-/// Swapping a single byte does nothing. This is unsafe to be consistent with
-/// the other `bswap` intrinsics.
-#[inline]
-unsafe fn bswap8(x: u8) -> u8 { x }
-
-macro_rules! bitwise_impl(
-    ($t:ty, $bits:expr, $co:ident, $lz:ident, $tz:ident, $bs:path) => {
+macro_rules! bitwise_impl {
+    ($t:ty, $bits:expr, $co:path, $lz:path, $tz:path) => {
         impl Bitwise for $t {
             #[inline]
-            fn count_ones(&self) -> $t { unsafe { intrinsics::$co(*self) } }
-
-            #[inline]
-            fn leading_zeros(&self) -> $t { unsafe { intrinsics::$lz(*self) } }
+            fn count_ones(&self) -> $t { unsafe { $co(*self) } }
 
             #[inline]
-            fn trailing_zeros(&self) -> $t { unsafe { intrinsics::$tz(*self) } }
+            fn leading_zeros(&self) -> $t { unsafe { $lz(*self) } }
 
             #[inline]
-            fn swap_bytes(&self) -> $t { unsafe { $bs(*self) } }
+            fn trailing_zeros(&self) -> $t { unsafe { $tz(*self) } }
 
             #[inline]
             fn rotate_left(&self, r: uint) -> $t {
@@ -514,22 +493,19 @@ macro_rules! bitwise_impl(
             }
         }
     }
-)
+}
 
-macro_rules! bitwise_cast_impl(
-    ($t:ty, $t_cast:ty, $bits:expr,  $co:ident, $lz:ident, $tz:ident, $bs:path) => {
+macro_rules! bitwise_cast_impl {
+    ($t:ty, $t_cast:ty, $bits:expr, $co:path, $lz:path, $tz:path) => {
         impl Bitwise for $t {
             #[inline]
-            fn count_ones(&self) -> $t { unsafe { intrinsics::$co(*self as $t_cast) as $t } }
+            fn count_ones(&self) -> $t { unsafe { $co(*self as $t_cast) as $t } }
 
             #[inline]
-            fn leading_zeros(&self) -> $t { unsafe { intrinsics::$lz(*self as $t_cast) as $t } }
+            fn leading_zeros(&self) -> $t { unsafe { $lz(*self as $t_cast) as $t } }
 
             #[inline]
-            fn trailing_zeros(&self) -> $t { unsafe { intrinsics::$tz(*self as $t_cast) as $t } }
-
-            #[inline]
-            fn swap_bytes(&self) -> $t { unsafe { $bs(*self as $t_cast) as $t } }
+            fn trailing_zeros(&self) -> $t { unsafe { $tz(*self as $t_cast) as $t } }
 
             #[inline]
             fn rotate_left(&self, r: uint) -> $t {
@@ -544,27 +520,27 @@ macro_rules! bitwise_cast_impl(
             }
         }
     }
-)
+}
 
 #[cfg(target_word_size = "32")]
-bitwise_cast_impl!(uint, u32, 32, ctpop32, ctlz32, cttz32, intrinsics::bswap32)
+bitwise_cast_impl!(uint, u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32)
 #[cfg(target_word_size = "64")]
-bitwise_cast_impl!(uint, u64, 64, ctpop64, ctlz64, cttz64, intrinsics::bswap64)
+bitwise_cast_impl!(uint, u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64)
 
-bitwise_impl!(u8, 8, ctpop8, ctlz8, cttz8, bswap8)
-bitwise_impl!(u16, 16, ctpop16, ctlz16, cttz16, intrinsics::bswap16)
-bitwise_impl!(u32, 32, ctpop32, ctlz32, cttz32, intrinsics::bswap32)
-bitwise_impl!(u64, 64, ctpop64, ctlz64, cttz64, intrinsics::bswap64)
+bitwise_impl!(u8, 8, intrinsics::ctpop8, intrinsics::ctlz8, intrinsics::cttz8)
+bitwise_impl!(u16, 16, intrinsics::ctpop16, intrinsics::ctlz16, intrinsics::cttz16)
+bitwise_impl!(u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32)
+bitwise_impl!(u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64)
 
 #[cfg(target_word_size = "32")]
-bitwise_cast_impl!(int, u32, 32, ctpop32, ctlz32, cttz32, intrinsics::bswap32)
+bitwise_cast_impl!(int, u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32)
 #[cfg(target_word_size = "64")]
-bitwise_cast_impl!(int, u64, 64, ctpop64, ctlz64, cttz64, intrinsics::bswap64)
+bitwise_cast_impl!(int, u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64)
 
-bitwise_cast_impl!(i8, u8, 8, ctpop8, ctlz8, cttz8, bswap8)
-bitwise_cast_impl!(i16, u16, 16, ctpop16, ctlz16, cttz16, intrinsics::bswap16)
-bitwise_cast_impl!(i32, u32, 32, ctpop32, ctlz32, cttz32, intrinsics::bswap32)
-bitwise_cast_impl!(i64, u64, 64, ctpop64, ctlz64, cttz64, intrinsics::bswap64)
+bitwise_cast_impl!(i8, u8, 8, intrinsics::ctpop8, intrinsics::ctlz8, intrinsics::cttz8)
+bitwise_cast_impl!(i16, u16, 16, intrinsics::ctpop16, intrinsics::ctlz16, intrinsics::cttz16)
+bitwise_cast_impl!(i32, u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32)
+bitwise_cast_impl!(i64, u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64)
 
 /// Specifies the available operations common to all of Rust's core numeric primitives.
 /// These may not always make sense from a purely mathematical point of view, but
diff --git a/src/libcore/num/uint_macros.rs b/src/libcore/num/uint_macros.rs
index 8e4ba101542..8548b141053 100644
--- a/src/libcore/num/uint_macros.rs
+++ b/src/libcore/num/uint_macros.rs
@@ -65,17 +65,6 @@ mod tests {
     }
 
     #[test]
-    fn test_swap_bytes() {
-        let n: $T = 0b0101100; assert_eq!(n.swap_bytes().swap_bytes(), n);
-        let n: $T = 0b0100001; assert_eq!(n.swap_bytes().swap_bytes(), n);
-        let n: $T = 0b1111001; assert_eq!(n.swap_bytes().swap_bytes(), n);
-
-        // Swapping these should make no difference
-        let n: $T = 0;   assert_eq!(n.swap_bytes(), n);
-        let n: $T = MAX; assert_eq!(n.swap_bytes(), n);
-    }
-
-    #[test]
     fn test_rotate() {
         let n: $T = 0b0101100; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
         let n: $T = 0b0100001; assert_eq!(n.rotate_left(3).rotate_left(2).rotate_right(5),  n);