Skip to content

Commit 292cc08

Browse files
committed
Added ARM specific instructions to be able to compile in ARM hosts (Mac)
1 parent 74e86d3 commit 292cc08

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22
#include <cstdint>
3-
/*
43

5-
This file contains implementatios or altername names for
6-
ARM GCC compiler that don't work in x86_64
4+
/*
5+
* This file contains implementations or alternate names for
6+
* ARM GCC intrinsics that don't work on x86_64 / host platforms.
7+
*/
78

8-
*/
99
static inline uint32_t __RBIT(uint32_t val) {
1010
// 1. Hardware Byte Swap (Optimization: handles the large movements)
1111
// MSVC uses _byteswap_ulong, GCC/Clang uses __builtin_bswap32
@@ -17,19 +17,39 @@ static inline uint32_t __RBIT(uint32_t val) {
1717

1818
// 2. Swap Nibbles (within bytes)
1919
// 0xF0 = 1111 0000 -> shifts to 0000 1111
20-
val = ((val & 0xF0F0F0F0) >> 4) | ((val & 0x0F0F0F0F) << 4);
20+
val = ((val & 0xF0F0F0F0u) >> 4) | ((val & 0x0F0F0F0Fu) << 4);
2121

2222
// 3. Swap Bit-Pairs (within nibbles)
2323
// 0xCC = 1100 1100 -> shifts to 0011 0011
24-
val = ((val & 0xCCCCCCCC) >> 2) | ((val & 0x33333333) << 2);
24+
val = ((val & 0xCCCCCCCCu) >> 2) | ((val & 0x33333333u) << 2);
2525

2626
// 4. Swap Single Bits (within pairs)
2727
// 0xAA = 1010 1010 -> shifts to 0101 0101
28-
val = ((val & 0xAAAAAAAA) >> 1) | ((val & 0x55555555) << 1);
28+
val = ((val & 0xAAAAAAAAu) >> 1) | ((val & 0x55555555u) << 1);
2929

3030
return val;
31-
}
32-
#define __CLZ __builtin_clz
33-
#define __COMPILER_BARRIER() asm volatile("" ::: "memory")
34-
#define __DSB() __asm__ volatile ("mfence" ::: "memory");
35-
#define __ISB() __asm__ volatile ("lfence" ::: "memory");
31+
}
32+
33+
#define __CLZ __builtin_clz
34+
#define __COMPILER_BARRIER() asm volatile("" ::: "memory")
35+
36+
// Architecture-specific definitions for barrier intrinsics used in mocks
37+
#if defined(__x86_64__) || defined(_M_X64)
38+
39+
// Host x86_64
40+
# define __DSB() __asm__ volatile("mfence" ::: "memory")
41+
# define __ISB() __asm__ volatile("lfence" ::: "memory")
42+
43+
#elif defined(__aarch64__) || defined(_M_ARM64)
44+
45+
// Host ARM64
46+
# define __DSB() __asm__ volatile("dmb ish" ::: "memory")
47+
# define __ISB() __asm__ volatile("isb" ::: "memory")
48+
49+
#else
50+
51+
// Any other host architecture: compiler barrier only
52+
# define __DSB() __COMPILER_BARRIER()
53+
# define __ISB() __COMPILER_BARRIER()
54+
55+
#endif

0 commit comments

Comments
 (0)