Skip to content

Commit 1173415

Browse files
AdamNiedereralexcrichton
authored andcommitted
Add bswap (rust-lang#257)
1 parent 2d77311 commit 1173415

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

coresimd/src/x86/i586/bswap.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#[cfg(test)]
2+
use stdsimd_test::assert_instr;
3+
4+
/// Return an integer with the reversed byte order of x
5+
#[inline(always)]
6+
#[cfg_attr(test, assert_instr(bswap))]
7+
pub unsafe fn _bswap(x: i32) -> i32 {
8+
bswap_i32(x)
9+
}
10+
11+
/// Return an integer with the reversed byte order of x
12+
#[inline(always)]
13+
#[cfg_attr(test, assert_instr(bswap))]
14+
pub unsafe fn _bswap64(x: i64) -> i64 {
15+
bswap_i64(x)
16+
}
17+
18+
#[allow(improper_ctypes)]
19+
extern "C" {
20+
#[link_name = "llvm.bswap.i64"]
21+
fn bswap_i64(x: i64) -> i64;
22+
#[link_name = "llvm.bswap.i32"]
23+
fn bswap_i32(x: i32) -> i32;
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn test_bswap() {
32+
unsafe {
33+
assert_eq!(_bswap(0x0EADBE0F), 0x0FBEAD0E);
34+
assert_eq!(_bswap(0x00000000), 0x00000000);
35+
}
36+
}
37+
38+
#[test]
39+
fn test_bswap64() {
40+
unsafe {
41+
assert_eq!(_bswap64(0x0EADBEEFFADECA0E), 0x0ECADEFAEFBEAD0E);
42+
assert_eq!(_bswap64(0x0000000000000000), 0x0000000000000000);
43+
}
44+
}
45+
}

coresimd/src/x86/i586/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
pub use self::cpuid::*;
44
pub use self::xsave::*;
55

6+
pub use self::bswap::*;
7+
68
pub use self::sse::*;
79
pub use self::sse2::*;
810
pub use self::sse3::*;
@@ -22,6 +24,8 @@ pub use self::tbm::*;
2224
mod cpuid;
2325
mod xsave;
2426

27+
mod bswap;
28+
2529
mod sse;
2630
mod sse2;
2731
mod sse3;

0 commit comments

Comments
 (0)