Skip to content

Commit 06db2de

Browse files
authored
Merge pull request #633 from tea/bswap
Implement __bswap[si]i2 intrinsics
2 parents eb6d2b6 + 3641761 commit 06db2de

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ rely on CI.
154154
- [ ] arm/unordsf2vfp.S
155155
- [x] ashldi3.c
156156
- [x] ashrdi3.c
157+
- [x] bswapdi2.c
158+
- [x] bswapsi2.c
159+
- [x] bswapti2.c
157160
- [x] comparedf2.c
158161
- [x] comparesf2.c
159162
- [x] divdf3.c

build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ fn configure_check_cfg() {
161161
"__ashlsi3",
162162
"__ashrdi3",
163163
"__ashrsi3",
164+
"__bswapsi2",
165+
"__bswapdi2",
166+
"__bswapti2",
164167
"__clzsi2",
165168
"__divdi3",
166169
"__divsi3",

src/int/bswap.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
intrinsics! {
2+
#[maybe_use_optimized_c_shim]
3+
#[avr_skip]
4+
/// Swaps bytes in 32-bit number
5+
pub extern "C" fn __bswapsi2(x: u32) -> u32 {
6+
x.swap_bytes()
7+
}
8+
9+
#[maybe_use_optimized_c_shim]
10+
#[avr_skip]
11+
/// Swaps bytes in 64-bit number
12+
pub extern "C" fn __bswapdi2(x: u64) -> u64 {
13+
x.swap_bytes()
14+
}
15+
16+
#[maybe_use_optimized_c_shim]
17+
#[avr_skip]
18+
/// Swaps bytes in 128-bit number
19+
pub extern "C" fn __bswapti2(x: u128) -> u128 {
20+
x.swap_bytes()
21+
}
22+
}

src/int/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod specialized_div_rem;
44

55
pub mod addsub;
66
mod big;
7+
pub mod bswap;
78
pub mod leading_zeros;
89
pub mod mul;
910
pub mod sdiv;

testcrate/tests/misc.rs

+34
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ fn leading_zeros() {
9292
})
9393
}
9494

95+
#[test]
96+
#[cfg(not(target_arch = "avr"))]
97+
fn bswap() {
98+
use compiler_builtins::int::bswap::{__bswapdi2, __bswapsi2};
99+
fuzz(N, |x: u32| {
100+
assert_eq!(x.swap_bytes(), __bswapsi2(x));
101+
});
102+
fuzz(N, |x: u64| {
103+
assert_eq!(x.swap_bytes(), __bswapdi2(x));
104+
});
105+
106+
assert_eq!(__bswapsi2(0x12345678u32), 0x78563412u32);
107+
assert_eq!(__bswapsi2(0x00000001u32), 0x01000000u32);
108+
assert_eq!(__bswapdi2(0x123456789ABCDEF0u64), 0xF0DEBC9A78563412u64);
109+
assert_eq!(__bswapdi2(0x0200000001000000u64), 0x0000000100000002u64);
110+
111+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
112+
{
113+
use compiler_builtins::int::bswap::__bswapti2;
114+
fuzz(N, |x: u128| {
115+
assert_eq!(x.swap_bytes(), __bswapti2(x));
116+
});
117+
118+
assert_eq!(
119+
__bswapti2(0x123456789ABCDEF013579BDF02468ACEu128),
120+
0xCE8A4602DF9B5713F0DEBC9A78563412u128
121+
);
122+
assert_eq!(
123+
__bswapti2(0x04000000030000000200000001000000u128),
124+
0x00000001000000020000000300000004u128
125+
);
126+
}
127+
}
128+
95129
// This is approximate because of issues related to
96130
// https://github.com/rust-lang/rust/issues/73920.
97131
// TODO how do we resolve this indeterminacy?

0 commit comments

Comments
 (0)