Add bswap (#257)

This commit is contained in:
Adam Niederer 2017-12-31 01:57:04 -05:00 committed by Alex Crichton
parent 5ca8c0aa93
commit 9141a063c9
2 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,45 @@
#[cfg(test)]
use stdsimd_test::assert_instr;
/// Return an integer with the reversed byte order of x
#[inline(always)]
#[cfg_attr(test, assert_instr(bswap))]
pub unsafe fn _bswap(x: i32) -> i32 {
bswap_i32(x)
}
/// Return an integer with the reversed byte order of x
#[inline(always)]
#[cfg_attr(test, assert_instr(bswap))]
pub unsafe fn _bswap64(x: i64) -> i64 {
bswap_i64(x)
}
#[allow(improper_ctypes)]
extern "C" {
#[link_name = "llvm.bswap.i64"]
fn bswap_i64(x: i64) -> i64;
#[link_name = "llvm.bswap.i32"]
fn bswap_i32(x: i32) -> i32;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bswap() {
unsafe {
assert_eq!(_bswap(0x0EADBE0F), 0x0FBEAD0E);
assert_eq!(_bswap(0x00000000), 0x00000000);
}
}
#[test]
fn test_bswap64() {
unsafe {
assert_eq!(_bswap64(0x0EADBEEFFADECA0E), 0x0ECADEFAEFBEAD0E);
assert_eq!(_bswap64(0x0000000000000000), 0x0000000000000000);
}
}
}

View file

@ -3,6 +3,8 @@
pub use self::cpuid::*;
pub use self::xsave::*;
pub use self::bswap::*;
pub use self::sse::*;
pub use self::sse2::*;
pub use self::sse3::*;
@ -22,6 +24,8 @@ pub use self::tbm::*;
mod cpuid;
mod xsave;
mod bswap;
mod sse;
mod sse2;
mod sse3;