Skip to content

Commit f763f0e

Browse files
committed
feat: add an __RBIT impl for x86_64
1 parent eb8094c commit f763f0e

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

Inc/MockedDrivers/compiler_specific.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ This file contains implementatios or altername names for
66
ARM GCC compiler that don't work in x86_64
77
88
*/
9-
#define __RBIT
9+
static inline uint32_t __RBIT(uint32_t val) {
10+
// 1. Hardware Byte Swap (Optimization: handles the large movements)
11+
// MSVC uses _byteswap_ulong, GCC/Clang uses __builtin_bswap32
12+
#if defined(_MSC_VER)
13+
val = _byteswap_ulong(val);
14+
#else
15+
val = __builtin_bswap32(val);
16+
#endif
17+
18+
// 2. Swap Nibbles (within bytes)
19+
// 0xF0 = 1111 0000 -> shifts to 0000 1111
20+
val = ((val & 0xF0F0F0F0) >> 4) | ((val & 0x0F0F0F0F) << 4);
21+
22+
// 3. Swap Bit-Pairs (within nibbles)
23+
// 0xCC = 1100 1100 -> shifts to 0011 0011
24+
val = ((val & 0xCCCCCCCC) >> 2) | ((val & 0x33333333) << 2);
25+
26+
// 4. Swap Single Bits (within pairs)
27+
// 0xAA = 1010 1010 -> shifts to 0101 0101
28+
val = ((val & 0xAAAAAAAA) >> 1) | ((val & 0x55555555) << 1);
29+
30+
return val;
31+
}
1032
#define __CLZ __builtin_clz
1133
#define __COMPILER_BARRIER() asm volatile("" ::: "memory")
1234
#define __DSB() __asm__ volatile ("mfence" ::: "memory");

0 commit comments

Comments
 (0)