include/prism/compiler/bswap.h
Line | Count | Source |
1 | | /** |
2 | | * @file compiler/bswap.h |
3 | | */ |
4 | | #ifndef PRISM_COMPILER_BSWAP_H |
5 | | #define PRISM_COMPILER_BSWAP_H |
6 | | |
7 | | /** |
8 | | * Byte swap utilities for 32-bit and 64-bit integers. |
9 | | */ |
10 | | #if defined(__GNUC__) || defined(__clang__) |
11 | 0 | #define pm_bswap32(x_) __builtin_bswap32((x_)) |
12 | 2.41k | #define pm_bswap64(x_) __builtin_bswap64((x_)) |
13 | | #elif defined(_MSC_VER) |
14 | | #include <stdlib.h> |
15 | | |
16 | | #define pm_bswap32(x_) _byteswap_ulong((x_)) |
17 | | #define pm_bswap64(x_) _byteswap_uint64((x_)) |
18 | | #else |
19 | | #include <stdint.h> |
20 | | |
21 | | static uint32_t |
22 | | pm_bswap32(uint32_t x) { |
23 | | return ( |
24 | | ((x >> 24) & 0x000000FFUL) | |
25 | | ((x << 24) & 0xFF000000UL) | |
26 | | ((x >> 8) & 0x0000FF00UL) | |
27 | | ((x << 8) & 0x00FF0000UL) |
28 | | ); |
29 | | } |
30 | | |
31 | | static uint64_t |
32 | | pm_bswap64(uint64_t x) { |
33 | | return ( |
34 | | ((x >> 56) & 0x00000000000000FFULL) | |
35 | | ((x << 56) & 0xFF00000000000000ULL) | |
36 | | ((x >> 40) & 0x000000000000FF00ULL) | |
37 | | ((x << 40) & 0x00FF000000000000ULL) | |
38 | | ((x >> 24) & 0x0000000000FF0000ULL) | |
39 | | ((x << 24) & 0x0000FF0000000000ULL) | |
40 | | ((x >> 8) & 0x00000000FF000000ULL) | |
41 | | ((x << 8) & 0x000000FF00000000ULL) |
42 | | ); |
43 | | } |
44 | | #endif |
45 | | |
46 | | #endif |