]> git.lizzy.rs Git - rust.git/commitdiff
Use the unsigned integer types for bitwise intrinsics.
authorHuon Wilson <dbau.pp+github@gmail.com>
Mon, 14 Apr 2014 10:04:14 +0000 (20:04 +1000)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 16 Apr 2014 02:45:00 +0000 (19:45 -0700)
Exposing ctpop, ctlz, cttz and bswap as taking signed i8/i16/... is just
exposing the internal LLVM names pointlessly (LLVM doesn't have "signed
integers" or "unsigned integers", it just has sized integer types
with (un)signed *operations*).

These operations are semantically working with raw bytes, which the
unsigned types model better.

15 files changed:
src/libnative/io/net.rs
src/librustc/middle/typeck/check/mod.rs
src/librustc/util/sha2.rs
src/librustuv/net.rs
src/libserialize/ebml.rs
src/libstd/intrinsics.rs
src/libstd/io/extensions.rs
src/libstd/mem.rs
src/libstd/num/i16.rs
src/libstd/num/i32.rs
src/libstd/num/i64.rs
src/libstd/num/i8.rs
src/libuuid/lib.rs
src/test/bench/sudoku.rs
src/test/run-pass/intrinsics-integer.rs

index cef6a247a00af9024a6ac81c05a5f3fbb784c90a..2e64b82a84a31d1e7e1fb4c319761878df134359 100644 (file)
 #[cfg(unix)]    pub type sock_t = super::file::fd_t;
 
 pub fn htons(u: u16) -> u16 {
-    mem::to_be16(u as i16) as u16
+    mem::to_be16(u)
 }
 pub fn ntohs(u: u16) -> u16 {
-    mem::from_be16(u as i16) as u16
+    mem::from_be16(u)
 }
 
 enum InAddr {
index fee510b82b7a2703a91c4f6ee1cfeb196192983a..cff8c149bb6ef8fbba88e229792bce95ef75ba0b 100644 (file)
@@ -4143,21 +4143,21 @@ fn param(ccx: &CrateCtxt, n: uint) -> ty::t {
             "nearbyintf64" => (0, vec!( ty::mk_f64() ), ty::mk_f64()),
             "roundf32"     => (0, vec!( ty::mk_f32() ), ty::mk_f32()),
             "roundf64"     => (0, vec!( ty::mk_f64() ), ty::mk_f64()),
-            "ctpop8"       => (0, vec!( ty::mk_i8()  ), ty::mk_i8()),
-            "ctpop16"      => (0, vec!( ty::mk_i16() ), ty::mk_i16()),
-            "ctpop32"      => (0, vec!( ty::mk_i32() ), ty::mk_i32()),
-            "ctpop64"      => (0, vec!( ty::mk_i64() ), ty::mk_i64()),
-            "ctlz8"        => (0, vec!( ty::mk_i8()  ), ty::mk_i8()),
-            "ctlz16"       => (0, vec!( ty::mk_i16() ), ty::mk_i16()),
-            "ctlz32"       => (0, vec!( ty::mk_i32() ), ty::mk_i32()),
-            "ctlz64"       => (0, vec!( ty::mk_i64() ), ty::mk_i64()),
-            "cttz8"        => (0, vec!( ty::mk_i8()  ), ty::mk_i8()),
-            "cttz16"       => (0, vec!( ty::mk_i16() ), ty::mk_i16()),
-            "cttz32"       => (0, vec!( ty::mk_i32() ), ty::mk_i32()),
-            "cttz64"       => (0, vec!( ty::mk_i64() ), ty::mk_i64()),
-            "bswap16"      => (0, vec!( ty::mk_i16() ), ty::mk_i16()),
-            "bswap32"      => (0, vec!( ty::mk_i32() ), ty::mk_i32()),
-            "bswap64"      => (0, vec!( ty::mk_i64() ), ty::mk_i64()),
+            "ctpop8"       => (0, vec!( ty::mk_u8()  ), ty::mk_u8()),
+            "ctpop16"      => (0, vec!( ty::mk_u16() ), ty::mk_u16()),
+            "ctpop32"      => (0, vec!( ty::mk_u32() ), ty::mk_u32()),
+            "ctpop64"      => (0, vec!( ty::mk_u64() ), ty::mk_u64()),
+            "ctlz8"        => (0, vec!( ty::mk_u8()  ), ty::mk_u8()),
+            "ctlz16"       => (0, vec!( ty::mk_u16() ), ty::mk_u16()),
+            "ctlz32"       => (0, vec!( ty::mk_u32() ), ty::mk_u32()),
+            "ctlz64"       => (0, vec!( ty::mk_u64() ), ty::mk_u64()),
+            "cttz8"        => (0, vec!( ty::mk_u8()  ), ty::mk_u8()),
+            "cttz16"       => (0, vec!( ty::mk_u16() ), ty::mk_u16()),
+            "cttz32"       => (0, vec!( ty::mk_u32() ), ty::mk_u32()),
+            "cttz64"       => (0, vec!( ty::mk_u64() ), ty::mk_u64()),
+            "bswap16"      => (0, vec!( ty::mk_u16() ), ty::mk_u16()),
+            "bswap32"      => (0, vec!( ty::mk_u32() ), ty::mk_u32()),
+            "bswap64"      => (0, vec!( ty::mk_u64() ), ty::mk_u64()),
 
             "volatile_load" =>
                 (1, vec!( ty::mk_imm_ptr(tcx, param(ccx, 0)) ), param(ccx, 0)),
index 17ea4d6b0b47563b089fbf29e0263e5be70e7afe..4571e6328cf24e056a1739b221e74af8a1cd9da7 100644 (file)
 /// Write a u32 into a vector, which must be 4 bytes long. The value is written in big-endian
 /// format.
 fn write_u32_be(dst: &mut[u8], input: u32) {
-    use std::cast::transmute;
     use std::mem::to_be32;
     assert!(dst.len() == 4);
     unsafe {
-        let x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
-        *x = to_be32(input as i32);
+        let x = dst.unsafe_mut_ref(0) as *mut _ as *mut u32;
+        *x = to_be32(input);
     }
 }
 
 /// Read a vector of bytes into a vector of u32s. The values are read in big-endian format.
 fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
-    use std::cast::transmute;
     use std::mem::to_be32;
     assert!(dst.len() * 4 == input.len());
     unsafe {
-        let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
-        let mut y: *i32 = transmute(input.unsafe_ref(0));
+        let mut x = dst.unsafe_mut_ref(0) as *mut _ as *mut u32;
+        let mut y = input.unsafe_ref(0) as *_ as *u32;
         for _ in range(0, dst.len()) {
             *x = to_be32(*y);
             x = x.offset(1);
index b893f5f693fa73beba94e24b5695b26c9d9591ea..280cd4bd592eef8795e41ea831a1c58f121fc8e4 100644 (file)
@@ -32,8 +32,8 @@
 /// Generic functions related to dealing with sockaddr things
 ////////////////////////////////////////////////////////////////////////////////
 
-pub fn htons(u: u16) -> u16 { mem::to_be16(u as i16) as u16 }
-pub fn ntohs(u: u16) -> u16 { mem::from_be16(u as i16) as u16 }
+pub fn htons(u: u16) -> u16 { mem::to_be16(u) }
+pub fn ntohs(u: u16) -> u16 { mem::from_be16(u) }
 
 pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
                         len: uint) -> ip::SocketAddr {
index 9b2df307b9947665c44a7858b85a43cd465c836a..0efa93011fc6687bf921986f636a46becd508613 100644 (file)
@@ -179,8 +179,8 @@ pub fn vuint_at(data: &[u8], start: uint) -> DecodeResult<Res> {
         ];
 
         unsafe {
-            let ptr = data.as_ptr().offset(start as int) as *i32;
-            let val = from_be32(*ptr) as u32;
+            let ptr = data.as_ptr().offset(start as int) as *u32;
+            let val = from_be32(*ptr);
 
             let i = (val >> 28u) as uint;
             let (shift, mask) = SHIFT_MASK_TABLE[i];
index 896ebcd6fb596dca87e2c3e27d58f23e387d78d6..175c7fe57b319c9cd0e7fa43edf084fd7be0b8c9 100644 (file)
@@ -394,26 +394,50 @@ fn visit_leave_fn(&mut self, purity: uint, proto: uint,
 
     pub fn roundf32(x: f32) -> f32;
     pub fn roundf64(x: f64) -> f64;
+}
+#[cfg(not(stage0))]
+extern "rust-intrinsic" {
+    pub fn ctpop8(x: u8) -> u8;
+    pub fn ctpop16(x: u16) -> u16;
+    pub fn ctpop32(x: u32) -> u32;
+    pub fn ctpop64(x: u64) -> u64;
+
+    pub fn ctlz8(x: u8) -> u8;
+    pub fn ctlz16(x: u16) -> u16;
+    pub fn ctlz32(x: u32) -> u32;
+    pub fn ctlz64(x: u64) -> u64;
+
+    pub fn cttz8(x: u8) -> u8;
+    pub fn cttz16(x: u16) -> u16;
+    pub fn cttz32(x: u32) -> u32;
+    pub fn cttz64(x: u64) -> u64;
+
+    pub fn bswap16(x: u16) -> u16;
+    pub fn bswap32(x: u32) -> u32;
+    pub fn bswap64(x: u64) -> u64;
+}
 
-    pub fn ctpop8(x: i8) -> i8;
-    pub fn ctpop16(x: i16) -> i16;
-    pub fn ctpop32(x: i32) -> i32;
-    pub fn ctpop64(x: i64) -> i64;
-
-    pub fn ctlz8(x: i8) -> i8;
-    pub fn ctlz16(x: i16) -> i16;
-    pub fn ctlz32(x: i32) -> i32;
-    pub fn ctlz64(x: i64) -> i64;
-
-    pub fn cttz8(x: i8) -> i8;
-    pub fn cttz16(x: i16) -> i16;
-    pub fn cttz32(x: i32) -> i32;
-    pub fn cttz64(x: i64) -> i64;
-
-    pub fn bswap16(x: i16) -> i16;
-    pub fn bswap32(x: i32) -> i32;
-    pub fn bswap64(x: i64) -> i64;
+// NOTE: remove this after a snap, and merge the extern block above
+macro_rules! stage0_hack {
+    ($( $u_ty:ty, $i_ty:ty => $($name:ident),*);*) => {
+        $(
+            $(
+                #[cfg(stage0)]
+                pub unsafe fn $name(x: $u_ty) -> $u_ty {
+                    extern "rust-intrinsic" { fn $name(x: $i_ty) -> $i_ty; }
+                    $name(x as $i_ty) as $u_ty
+                }
+            )*)*
+    }
+}
+stage0_hack! {
+    u8, i8 => ctpop8, ctlz8, cttz8;
+    u16, i16 => ctpop16, ctlz16, cttz16, bswap16;
+    u32, i32 => ctpop32, ctlz32, cttz32, bswap32;
+    u64, i64 => ctpop64, ctlz64, cttz64, bswap64
+}
 
+extern "rust-intrinsic" {
     pub fn i8_add_with_overflow(x: i8, y: i8) -> (i8, bool);
     pub fn i16_add_with_overflow(x: i16, y: i16) -> (i16, bool);
     pub fn i32_add_with_overflow(x: i32, y: i32) -> (i32, bool);
index b2202a13057495adea410f172d02f5436ab00c32..d8022b1e26c6d48df5040711d33d5d82ee8a77d6 100644 (file)
@@ -83,9 +83,9 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
     assert!(size <= 8u);
     match size {
       1u => f(&[n as u8]),
-      2u => f(unsafe { transmute::<i16, [u8, ..2]>(to_le16(n as i16)) }),
-      4u => f(unsafe { transmute::<i32, [u8, ..4]>(to_le32(n as i32)) }),
-      8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_le64(n as i64)) }),
+      2u => f(unsafe { transmute::<_, [u8, ..2]>(to_le16(n as u16)) }),
+      4u => f(unsafe { transmute::<_, [u8, ..4]>(to_le32(n as u32)) }),
+      8u => f(unsafe { transmute::<_, [u8, ..8]>(to_le64(n)) }),
       _ => {
 
         let mut bytes = vec!();
@@ -123,9 +123,9 @@ pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
     assert!(size <= 8u);
     match size {
       1u => f(&[n as u8]),
-      2u => f(unsafe { transmute::<i16, [u8, ..2]>(to_be16(n as i16)) }),
-      4u => f(unsafe { transmute::<i32, [u8, ..4]>(to_be32(n as i32)) }),
-      8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_be64(n as i64)) }),
+      2u => f(unsafe { transmute::<_, [u8, ..2]>(to_be16(n as u16)) }),
+      4u => f(unsafe { transmute::<_, [u8, ..4]>(to_be32(n as u32)) }),
+      8u => f(unsafe { transmute::<_, [u8, ..8]>(to_be64(n)) }),
       _ => {
         let mut bytes = vec!();
         let mut i = size;
@@ -166,7 +166,7 @@ pub fn u64_from_be_bytes(data: &[u8], start: uint, size: uint) -> u64 {
         let ptr = data.as_ptr().offset(start as int);
         let out = buf.as_mut_ptr();
         copy_nonoverlapping_memory(out.offset((8 - size) as int), ptr, size);
-        from_be64(*(out as *i64)) as u64
+        from_be64(*(out as *u64))
     }
 }
 
index deefb3fe2ed0ae6175efe8c3bd68302148e5d51a..282cfe517823bd9e2077b3756b713836527bcf14 100644 (file)
@@ -99,128 +99,128 @@ pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
     intrinsics::move_val_init(dst, src)
 }
 
-/// Convert an i16 to little endian from the target's endianness.
+/// Convert an u16 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: i16) -> i16 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: u16) -> u16 { x }
 
-/// Convert an i16 to little endian from the target's endianness.
+/// Convert an u16 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_le16(x: u16) -> u16 { unsafe { bswap16(x) } }
 
-/// Convert an i32 to little endian from the target's endianness.
+/// Convert an u32 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: i32) -> i32 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: u32) -> u32 { x }
 
-/// Convert an i32 to little endian from the target's endianness.
+/// Convert an u32 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_le32(x: u32) -> u32 { unsafe { bswap32(x) } }
 
-/// Convert an i64 to little endian from the target's endianness.
+/// Convert an u64 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: i64) -> i64 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: u64) -> u64 { x }
 
-/// Convert an i64 to little endian from the target's endianness.
+/// Convert an u64 to little endian from the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_le64(x: u64) -> u64 { unsafe { bswap64(x) } }
 
 
-/// Convert an i16 to big endian from the target's endianness.
+/// Convert an u16 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: u16) -> u16 { unsafe { bswap16(x) } }
 
-/// Convert an i16 to big endian from the target's endianness.
+/// Convert an u16 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_be16(x: i16) -> i16 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_be16(x: u16) -> u16 { x }
 
-/// Convert an i32 to big endian from the target's endianness.
+/// Convert an u32 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: u32) -> u32 { unsafe { bswap32(x) } }
 
-/// Convert an i32 to big endian from the target's endianness.
+/// Convert an u32 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_be32(x: i32) -> i32 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_be32(x: u32) -> u32 { x }
 
-/// Convert an i64 to big endian from the target's endianness.
+/// Convert an u64 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: u64) -> u64 { unsafe { bswap64(x) } }
 
-/// Convert an i64 to big endian from the target's endianness.
+/// Convert an u64 to big endian from the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn to_be64(x: i64) -> i64 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn to_be64(x: u64) -> u64 { x }
 
 
-/// Convert an i16 from little endian to the target's endianness.
+/// Convert an u16 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: i16) -> i16 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: u16) -> u16 { x }
 
-/// Convert an i16 from little endian to the target's endianness.
+/// Convert an u16 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_le16(x: u16) -> u16 { unsafe { bswap16(x) } }
 
-/// Convert an i32 from little endian to the target's endianness.
+/// Convert an u32 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: i32) -> i32 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: u32) -> u32 { x }
 
-/// Convert an i32 from little endian to the target's endianness.
+/// Convert an u32 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_le32(x: u32) -> u32 { unsafe { bswap32(x) } }
 
-/// Convert an i64 from little endian to the target's endianness.
+/// Convert an u64 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: i64) -> i64 { x }
+#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: u64) -> u64 { x }
 
-/// Convert an i64 from little endian to the target's endianness.
+/// Convert an u64 from little endian to the target's endianness.
 ///
 /// On little endian, this is a no-op.  On big endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_le64(x: u64) -> u64 { unsafe { bswap64(x) } }
 
 
-/// Convert an i16 from big endian to the target's endianness.
+/// Convert an u16 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: u16) -> u16 { unsafe { bswap16(x) } }
 
-/// Convert an i16 from big endian to the target's endianness.
+/// Convert an u16 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_be16(x: i16) -> i16 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_be16(x: u16) -> u16 { x }
 
-/// Convert an i32 from big endian to the target's endianness.
+/// Convert an u32 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: u32) -> u32 { unsafe { bswap32(x) } }
 
-/// Convert an i32 from big endian to the target's endianness.
+/// Convert an u32 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_be32(x: i32) -> i32 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_be32(x: u32) -> u32 { x }
 
-/// Convert an i64 from big endian to the target's endianness.
+/// Convert an u64 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
+#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: u64) -> u64 { unsafe { bswap64(x) } }
 
-/// Convert an i64 from big endian to the target's endianness.
+/// Convert an u64 from big endian to the target's endianness.
 ///
 /// On big endian, this is a no-op.  On little endian, the bytes are swapped.
-#[cfg(target_endian = "big")]    #[inline] pub fn from_be64(x: i64) -> i64 { x }
+#[cfg(target_endian = "big")]    #[inline] pub fn from_be64(x: u64) -> u64 { x }
 
 
 /**
index 42710a8b459d089e09d23fe57907972bb5bdea00..79827421f9222c37c743c302752c26eb2d3264fc 100644 (file)
 impl Bitwise for i16 {
     /// Returns the number of ones in the binary representation of the number.
     #[inline]
-    fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }
+    fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self as u16) as i16 } }
 
     /// Returns the number of leading zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self) } }
+    fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self as u16) as i16 } }
 
     /// Returns the number of trailing zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
+    fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self as u16) as i16 } }
 }
 
 impl CheckedAdd for i16 {
index 69d4b0639f732af609d60f60b80068480bfb8b85..97f03299b8765374016d878474c9e48bccc6228c 100644 (file)
 impl Bitwise for i32 {
     /// Returns the number of ones in the binary representation of the number.
     #[inline]
-    fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }
+    fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self as u32) as i32 } }
 
     /// Returns the number of leading zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self) } }
+    fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self as u32) as i32 } }
 
     /// Returns the number of trailing zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
+    fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self as u32) as i32 } }
 }
 
 impl CheckedAdd for i32 {
index 1f7066c25db3b649e228e1d949cc4096896f671a..00823aa22c28ec8d6139609b2b585bc4d50cd376 100644 (file)
 impl Bitwise for i64 {
     /// Returns the number of ones in the binary representation of the number.
     #[inline]
-    fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }
+    fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self as u64) as i64 } }
 
     /// Returns the number of leading zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self) } }
+    fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self as u64) as i64 } }
 
     /// Counts the number of trailing zeros.
     #[inline]
-    fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
+    fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self as u64) as i64 } }
 }
 
 impl CheckedAdd for i64 {
index 061ffddf2312e98fea2b4eb2f6b571937216c44c..2d349fa7f4f1da240fd750de8a1d96a8f93bddc1 100644 (file)
 impl Bitwise for i8 {
     /// Returns the number of ones in the binary representation of the number.
     #[inline]
-    fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
+    fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self as u8) as i8 } }
 
     /// Returns the number of leading zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
+    fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self as u8) as i8 } }
 
     /// Returns the number of trailing zeros in the in the binary representation
     /// of the number.
     #[inline]
-    fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
+    fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self as u8) as i8 } }
 }
 
 impl CheckedAdd for i8 {
index 2ac6780af4c1bd8ca0d7784285840afd20f96d35..559edd587fd7e0e18283fc350bc6e66b6f4063fc 100644 (file)
@@ -220,9 +220,9 @@ pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid {
                 data4: [0, ..8]
         };
 
-        fields.data1 = to_be32(d1 as i32) as u32;
-        fields.data2 = to_be16(d2 as i16) as u16;
-        fields.data3 = to_be16(d3 as i16) as u16;
+        fields.data1 = to_be32(d1);
+        fields.data2 = to_be16(d2);
+        fields.data3 = to_be16(d3);
         slice::bytes::copy_memory(fields.data4, d4);
 
         unsafe {
@@ -343,9 +343,9 @@ pub fn to_hyphenated_str(&self) -> ~str {
         unsafe {
             uf = transmute_copy(&self.bytes);
         }
-        uf.data1 = to_be32(uf.data1 as i32) as u32;
-        uf.data2 = to_be16(uf.data2 as i16) as u16;
-        uf.data3 = to_be16(uf.data3 as i16) as u16;
+        uf.data1 = to_be32(uf.data1);
+        uf.data2 = to_be16(uf.data2);
+        uf.data3 = to_be16(uf.data3);
         let s = format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-\
                          {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
             uf.data1,
index 80fea6d49321dd70fe1089ff2b8cd4804ce6090b..f5724fc1324d5725ea3e5653711c59e49eca35f2 100644 (file)
@@ -16,7 +16,7 @@
 use std::io::stdio::StdReader;
 use std::io::BufferedReader;
 use std::os;
-use std::intrinsics::cttz16;
+use std::num::Bitwise;
 
 // Computes a single solution to a given 9x9 sudoku
 //
@@ -187,9 +187,7 @@ fn next(&self) -> u8 {
         if (0u16 == val) {
             return 0u8;
         } else {
-            unsafe {
-                return cttz16(val as i16) as u8;
-            }
+            return val.trailing_zeros() as u8
         }
     }
 
index 164d16fe50397e348d9654411cef4304ee291343..e31b941f956eb6893f2a36fc06ab3235aad1531d 100644 (file)
@@ -1,4 +1,3 @@
-
 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 
 mod rusti {
     extern "rust-intrinsic" {
-        pub fn ctpop8(x: i8) -> i8;
-        pub fn ctpop16(x: i16) -> i16;
-        pub fn ctpop32(x: i32) -> i32;
-        pub fn ctpop64(x: i64) -> i64;
-
-        pub fn ctlz8(x: i8) -> i8;
-        pub fn ctlz16(x: i16) -> i16;
-        pub fn ctlz32(x: i32) -> i32;
-        pub fn ctlz64(x: i64) -> i64;
-
-        pub fn cttz8(x: i8) -> i8;
-        pub fn cttz16(x: i16) -> i16;
-        pub fn cttz32(x: i32) -> i32;
-        pub fn cttz64(x: i64) -> i64;
-
-        pub fn bswap16(x: i16) -> i16;
-        pub fn bswap32(x: i32) -> i32;
-        pub fn bswap64(x: i64) -> i64;
+        pub fn ctpop8(x: u8) -> u8;
+        pub fn ctpop16(x: u16) -> u16;
+        pub fn ctpop32(x: u32) -> u32;
+        pub fn ctpop64(x: u64) -> u64;
+
+        pub fn ctlz8(x: u8) -> u8;
+        pub fn ctlz16(x: u16) -> u16;
+        pub fn ctlz32(x: u32) -> u32;
+        pub fn ctlz64(x: u64) -> u64;
+
+        pub fn cttz8(x: u8) -> u8;
+        pub fn cttz16(x: u16) -> u16;
+        pub fn cttz32(x: u32) -> u32;
+        pub fn cttz64(x: u64) -> u64;
+
+        pub fn bswap16(x: u16) -> u16;
+        pub fn bswap32(x: u32) -> u32;
+        pub fn bswap64(x: u64) -> u64;
     }
 }
 
@@ -38,83 +37,83 @@ pub fn main() {
     unsafe {
         use rusti::*;
 
-        assert_eq!(ctpop8(0i8), 0i8);
-        assert_eq!(ctpop16(0i16), 0i16);
-        assert_eq!(ctpop32(0i32), 0i32);
-        assert_eq!(ctpop64(0i64), 0i64);
-
-        assert_eq!(ctpop8(1i8), 1i8);
-        assert_eq!(ctpop16(1i16), 1i16);
-        assert_eq!(ctpop32(1i32), 1i32);
-        assert_eq!(ctpop64(1i64), 1i64);
-
-        assert_eq!(ctpop8(10i8), 2i8);
-        assert_eq!(ctpop16(10i16), 2i16);
-        assert_eq!(ctpop32(10i32), 2i32);
-        assert_eq!(ctpop64(10i64), 2i64);
-
-        assert_eq!(ctpop8(100i8), 3i8);
-        assert_eq!(ctpop16(100i16), 3i16);
-        assert_eq!(ctpop32(100i32), 3i32);
-        assert_eq!(ctpop64(100i64), 3i64);
-
-        assert_eq!(ctpop8(-1i8), 8i8);
-        assert_eq!(ctpop16(-1i16), 16i16);
-        assert_eq!(ctpop32(-1i32), 32i32);
-        assert_eq!(ctpop64(-1i64), 64i64);
-
-        assert_eq!(ctlz8(0i8), 8i8);
-        assert_eq!(ctlz16(0i16), 16i16);
-        assert_eq!(ctlz32(0i32), 32i32);
-        assert_eq!(ctlz64(0i64), 64i64);
-
-        assert_eq!(ctlz8(1i8), 7i8);
-        assert_eq!(ctlz16(1i16), 15i16);
-        assert_eq!(ctlz32(1i32), 31i32);
-        assert_eq!(ctlz64(1i64), 63i64);
-
-        assert_eq!(ctlz8(10i8), 4i8);
-        assert_eq!(ctlz16(10i16), 12i16);
-        assert_eq!(ctlz32(10i32), 28i32);
-        assert_eq!(ctlz64(10i64), 60i64);
-
-        assert_eq!(ctlz8(100i8), 1i8);
-        assert_eq!(ctlz16(100i16), 9i16);
-        assert_eq!(ctlz32(100i32), 25i32);
-        assert_eq!(ctlz64(100i64), 57i64);
-
-        assert_eq!(cttz8(-1i8), 0i8);
-        assert_eq!(cttz16(-1i16), 0i16);
-        assert_eq!(cttz32(-1i32), 0i32);
-        assert_eq!(cttz64(-1i64), 0i64);
-
-        assert_eq!(cttz8(0i8), 8i8);
-        assert_eq!(cttz16(0i16), 16i16);
-        assert_eq!(cttz32(0i32), 32i32);
-        assert_eq!(cttz64(0i64), 64i64);
-
-        assert_eq!(cttz8(1i8), 0i8);
-        assert_eq!(cttz16(1i16), 0i16);
-        assert_eq!(cttz32(1i32), 0i32);
-        assert_eq!(cttz64(1i64), 0i64);
-
-        assert_eq!(cttz8(10i8), 1i8);
-        assert_eq!(cttz16(10i16), 1i16);
-        assert_eq!(cttz32(10i32), 1i32);
-        assert_eq!(cttz64(10i64), 1i64);
-
-        assert_eq!(cttz8(100i8), 2i8);
-        assert_eq!(cttz16(100i16), 2i16);
-        assert_eq!(cttz32(100i32), 2i32);
-        assert_eq!(cttz64(100i64), 2i64);
-
-        assert_eq!(cttz8(-1i8), 0i8);
-        assert_eq!(cttz16(-1i16), 0i16);
-        assert_eq!(cttz32(-1i32), 0i32);
-        assert_eq!(cttz64(-1i64), 0i64);
-
-        assert_eq!(bswap16(0x0A0Bi16), 0x0B0Ai16);
-        assert_eq!(bswap32(0x0ABBCC0Di32), 0x0DCCBB0Ai32);
-        assert_eq!(bswap64(0x0122334455667708i64), 0x0877665544332201i64);
+        assert_eq!(ctpop8(0u8), 0u8);
+        assert_eq!(ctpop16(0u16), 0u16);
+        assert_eq!(ctpop32(0u32), 0u32);
+        assert_eq!(ctpop64(0u64), 0u64);
+
+        assert_eq!(ctpop8(1u8), 1u8);
+        assert_eq!(ctpop16(1u16), 1u16);
+        assert_eq!(ctpop32(1u32), 1u32);
+        assert_eq!(ctpop64(1u64), 1u64);
+
+        assert_eq!(ctpop8(10u8), 2u8);
+        assert_eq!(ctpop16(10u16), 2u16);
+        assert_eq!(ctpop32(10u32), 2u32);
+        assert_eq!(ctpop64(10u64), 2u64);
+
+        assert_eq!(ctpop8(100u8), 3u8);
+        assert_eq!(ctpop16(100u16), 3u16);
+        assert_eq!(ctpop32(100u32), 3u32);
+        assert_eq!(ctpop64(100u64), 3u64);
+
+        assert_eq!(ctpop8(-1u8), 8u8);
+        assert_eq!(ctpop16(-1u16), 16u16);
+        assert_eq!(ctpop32(-1u32), 32u32);
+        assert_eq!(ctpop64(-1u64), 64u64);
+
+        assert_eq!(ctlz8(0u8), 8u8);
+        assert_eq!(ctlz16(0u16), 16u16);
+        assert_eq!(ctlz32(0u32), 32u32);
+        assert_eq!(ctlz64(0u64), 64u64);
+
+        assert_eq!(ctlz8(1u8), 7u8);
+        assert_eq!(ctlz16(1u16), 15u16);
+        assert_eq!(ctlz32(1u32), 31u32);
+        assert_eq!(ctlz64(1u64), 63u64);
+
+        assert_eq!(ctlz8(10u8), 4u8);
+        assert_eq!(ctlz16(10u16), 12u16);
+        assert_eq!(ctlz32(10u32), 28u32);
+        assert_eq!(ctlz64(10u64), 60u64);
+
+        assert_eq!(ctlz8(100u8), 1u8);
+        assert_eq!(ctlz16(100u16), 9u16);
+        assert_eq!(ctlz32(100u32), 25u32);
+        assert_eq!(ctlz64(100u64), 57u64);
+
+        assert_eq!(cttz8(-1u8), 0u8);
+        assert_eq!(cttz16(-1u16), 0u16);
+        assert_eq!(cttz32(-1u32), 0u32);
+        assert_eq!(cttz64(-1u64), 0u64);
+
+        assert_eq!(cttz8(0u8), 8u8);
+        assert_eq!(cttz16(0u16), 16u16);
+        assert_eq!(cttz32(0u32), 32u32);
+        assert_eq!(cttz64(0u64), 64u64);
+
+        assert_eq!(cttz8(1u8), 0u8);
+        assert_eq!(cttz16(1u16), 0u16);
+        assert_eq!(cttz32(1u32), 0u32);
+        assert_eq!(cttz64(1u64), 0u64);
+
+        assert_eq!(cttz8(10u8), 1u8);
+        assert_eq!(cttz16(10u16), 1u16);
+        assert_eq!(cttz32(10u32), 1u32);
+        assert_eq!(cttz64(10u64), 1u64);
+
+        assert_eq!(cttz8(100u8), 2u8);
+        assert_eq!(cttz16(100u16), 2u16);
+        assert_eq!(cttz32(100u32), 2u32);
+        assert_eq!(cttz64(100u64), 2u64);
+
+        assert_eq!(cttz8(-1u8), 0u8);
+        assert_eq!(cttz16(-1u16), 0u16);
+        assert_eq!(cttz32(-1u32), 0u32);
+        assert_eq!(cttz64(-1u64), 0u64);
+
+        assert_eq!(bswap16(0x0A0Bu16), 0x0B0Au16);
+        assert_eq!(bswap32(0x0ABBCC0Du32), 0x0DCCBB0Au32);
+        assert_eq!(bswap64(0x0122334455667708u64), 0x0877665544332201u64);
     }
 }