Use the unsigned integer types for bitwise intrinsics.

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.
This commit is contained in:
Huon Wilson 2014-04-14 20:04:14 +10:00 committed by Alex Crichton
parent 93dc555188
commit 54ec04f1c1
15 changed files with 217 additions and 198 deletions

View File

@ -26,10 +26,10 @@ use super::{IoResult, retry, keep_going};
#[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 {

View File

@ -4143,21 +4143,21 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
"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)),

View File

@ -20,23 +20,21 @@ use serialize::hex::ToHex;
/// 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);

View File

@ -32,8 +32,8 @@ use uvll;
/// 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 {

View File

@ -179,8 +179,8 @@ pub mod reader {
];
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];

View File

@ -394,26 +394,50 @@ extern "rust-intrinsic" {
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 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: u8) -> u8;
pub fn ctlz16(x: u16) -> u16;
pub fn ctlz32(x: u32) -> u32;
pub fn ctlz64(x: u64) -> u64;
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: u8) -> u8;
pub fn cttz16(x: u16) -> u16;
pub fn cttz32(x: u32) -> u32;
pub fn cttz64(x: u64) -> u64;
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: u16) -> u16;
pub fn bswap32(x: u32) -> u32;
pub fn bswap64(x: u64) -> u64;
}
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);

View 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))
}
}

View 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 }
/**

View File

@ -28,17 +28,17 @@ int_module!(i16, 16)
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 {

View File

@ -28,17 +28,17 @@ int_module!(i32, 32)
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 {

View File

@ -30,16 +30,16 @@ int_module!(i64, 64)
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 {

View File

@ -28,17 +28,17 @@ int_module!(i8, 8)
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 {

View File

@ -220,9 +220,9 @@ impl 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 @@ impl Uuid {
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,

View File

@ -16,7 +16,7 @@ use std::io;
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 @@ impl Colors {
if (0u16 == val) {
return 0u8;
} else {
unsafe {
return cttz16(val as i16) as u8;
}
return val.trailing_zeros() as u8
}
}

View 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.
@ -13,24 +12,24 @@
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 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: i8) -> i8;
pub fn ctlz16(x: i16) -> i16;
pub fn ctlz32(x: i32) -> i32;
pub fn ctlz64(x: i64) -> i64;
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: i8) -> i8;
pub fn cttz16(x: i16) -> i16;
pub fn cttz32(x: i32) -> i32;
pub fn cttz64(x: i64) -> i64;
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: i16) -> i16;
pub fn bswap32(x: i32) -> i32;
pub fn bswap64(x: i64) -> i64;
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(0u8), 0u8);
assert_eq!(ctpop16(0u16), 0u16);
assert_eq!(ctpop32(0u32), 0u32);
assert_eq!(ctpop64(0u64), 0u64);
assert_eq!(ctpop8(1i8), 1i8);
assert_eq!(ctpop16(1i16), 1i16);
assert_eq!(ctpop32(1i32), 1i32);
assert_eq!(ctpop64(1i64), 1i64);
assert_eq!(ctpop8(1u8), 1u8);
assert_eq!(ctpop16(1u16), 1u16);
assert_eq!(ctpop32(1u32), 1u32);
assert_eq!(ctpop64(1u64), 1u64);
assert_eq!(ctpop8(10i8), 2i8);
assert_eq!(ctpop16(10i16), 2i16);
assert_eq!(ctpop32(10i32), 2i32);
assert_eq!(ctpop64(10i64), 2i64);
assert_eq!(ctpop8(10u8), 2u8);
assert_eq!(ctpop16(10u16), 2u16);
assert_eq!(ctpop32(10u32), 2u32);
assert_eq!(ctpop64(10u64), 2u64);
assert_eq!(ctpop8(100i8), 3i8);
assert_eq!(ctpop16(100i16), 3i16);
assert_eq!(ctpop32(100i32), 3i32);
assert_eq!(ctpop64(100i64), 3i64);
assert_eq!(ctpop8(100u8), 3u8);
assert_eq!(ctpop16(100u16), 3u16);
assert_eq!(ctpop32(100u32), 3u32);
assert_eq!(ctpop64(100u64), 3u64);
assert_eq!(ctpop8(-1i8), 8i8);
assert_eq!(ctpop16(-1i16), 16i16);
assert_eq!(ctpop32(-1i32), 32i32);
assert_eq!(ctpop64(-1i64), 64i64);
assert_eq!(ctpop8(-1u8), 8u8);
assert_eq!(ctpop16(-1u16), 16u16);
assert_eq!(ctpop32(-1u32), 32u32);
assert_eq!(ctpop64(-1u64), 64u64);
assert_eq!(ctlz8(0i8), 8i8);
assert_eq!(ctlz16(0i16), 16i16);
assert_eq!(ctlz32(0i32), 32i32);
assert_eq!(ctlz64(0i64), 64i64);
assert_eq!(ctlz8(0u8), 8u8);
assert_eq!(ctlz16(0u16), 16u16);
assert_eq!(ctlz32(0u32), 32u32);
assert_eq!(ctlz64(0u64), 64u64);
assert_eq!(ctlz8(1i8), 7i8);
assert_eq!(ctlz16(1i16), 15i16);
assert_eq!(ctlz32(1i32), 31i32);
assert_eq!(ctlz64(1i64), 63i64);
assert_eq!(ctlz8(1u8), 7u8);
assert_eq!(ctlz16(1u16), 15u16);
assert_eq!(ctlz32(1u32), 31u32);
assert_eq!(ctlz64(1u64), 63u64);
assert_eq!(ctlz8(10i8), 4i8);
assert_eq!(ctlz16(10i16), 12i16);
assert_eq!(ctlz32(10i32), 28i32);
assert_eq!(ctlz64(10i64), 60i64);
assert_eq!(ctlz8(10u8), 4u8);
assert_eq!(ctlz16(10u16), 12u16);
assert_eq!(ctlz32(10u32), 28u32);
assert_eq!(ctlz64(10u64), 60u64);
assert_eq!(ctlz8(100i8), 1i8);
assert_eq!(ctlz16(100i16), 9i16);
assert_eq!(ctlz32(100i32), 25i32);
assert_eq!(ctlz64(100i64), 57i64);
assert_eq!(ctlz8(100u8), 1u8);
assert_eq!(ctlz16(100u16), 9u16);
assert_eq!(ctlz32(100u32), 25u32);
assert_eq!(ctlz64(100u64), 57u64);
assert_eq!(cttz8(-1i8), 0i8);
assert_eq!(cttz16(-1i16), 0i16);
assert_eq!(cttz32(-1i32), 0i32);
assert_eq!(cttz64(-1i64), 0i64);
assert_eq!(cttz8(-1u8), 0u8);
assert_eq!(cttz16(-1u16), 0u16);
assert_eq!(cttz32(-1u32), 0u32);
assert_eq!(cttz64(-1u64), 0u64);
assert_eq!(cttz8(0i8), 8i8);
assert_eq!(cttz16(0i16), 16i16);
assert_eq!(cttz32(0i32), 32i32);
assert_eq!(cttz64(0i64), 64i64);
assert_eq!(cttz8(0u8), 8u8);
assert_eq!(cttz16(0u16), 16u16);
assert_eq!(cttz32(0u32), 32u32);
assert_eq!(cttz64(0u64), 64u64);
assert_eq!(cttz8(1i8), 0i8);
assert_eq!(cttz16(1i16), 0i16);
assert_eq!(cttz32(1i32), 0i32);
assert_eq!(cttz64(1i64), 0i64);
assert_eq!(cttz8(1u8), 0u8);
assert_eq!(cttz16(1u16), 0u16);
assert_eq!(cttz32(1u32), 0u32);
assert_eq!(cttz64(1u64), 0u64);
assert_eq!(cttz8(10i8), 1i8);
assert_eq!(cttz16(10i16), 1i16);
assert_eq!(cttz32(10i32), 1i32);
assert_eq!(cttz64(10i64), 1i64);
assert_eq!(cttz8(10u8), 1u8);
assert_eq!(cttz16(10u16), 1u16);
assert_eq!(cttz32(10u32), 1u32);
assert_eq!(cttz64(10u64), 1u64);
assert_eq!(cttz8(100i8), 2i8);
assert_eq!(cttz16(100i16), 2i16);
assert_eq!(cttz32(100i32), 2i32);
assert_eq!(cttz64(100i64), 2i64);
assert_eq!(cttz8(100u8), 2u8);
assert_eq!(cttz16(100u16), 2u16);
assert_eq!(cttz32(100u32), 2u32);
assert_eq!(cttz64(100u64), 2u64);
assert_eq!(cttz8(-1i8), 0i8);
assert_eq!(cttz16(-1i16), 0i16);
assert_eq!(cttz32(-1i32), 0i32);
assert_eq!(cttz64(-1i64), 0i64);
assert_eq!(cttz8(-1u8), 0u8);
assert_eq!(cttz16(-1u16), 0u16);
assert_eq!(cttz32(-1u32), 0u32);
assert_eq!(cttz64(-1u64), 0u64);
assert_eq!(bswap16(0x0A0Bi16), 0x0B0Ai16);
assert_eq!(bswap32(0x0ABBCC0Di32), 0x0DCCBB0Ai32);
assert_eq!(bswap64(0x0122334455667708i64), 0x0877665544332201i64);
assert_eq!(bswap16(0x0A0Bu16), 0x0B0Au16);
assert_eq!(bswap32(0x0ABBCC0Du32), 0x0DCCBB0Au32);
assert_eq!(bswap64(0x0122334455667708u64), 0x0877665544332201u64);
}
}