db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / compat / bswap.h
blobf0b7e93cc2c784b1e488dd96107deaff7b8f596d
1 #ifndef _COMPAT_BSWAP_H_
2 #define _COMPAT_BSWAP_H_
4 #if defined(__GNUC__)
5 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))
6 #define __HAS_BUILTIN_BSWAP16
7 #endif
8 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))
9 #define __HAS_BUILTIN_BSWAP32
10 #define __HAS_BUILTIN_BSWAP64
11 #endif
12 #endif
14 #if defined(__clang__)
15 #if (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 2))
16 #define __HAS_BUILTIN_BSWAP16
17 #endif
18 #if (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 0))
19 #define __HAS_BUILTIN_BSWAP32
20 #define __HAS_BUILTIN_BSWAP64
21 #endif
22 #endif
24 #ifdef __HAS_BUILTIN_BSWAP16
25 #define bswap16(x) __builtin_bswap16(x)
26 #else
27 #include <stdint.h>
28 static inline uint16_t bswap16(uint16_t x)
30 return x << 8 | x >> 8;
32 #endif
34 #ifdef __HAS_BUILTIN_BSWAP32
35 #define bswap32(x) __builtin_bswap32(x)
36 #else
37 #include <stdint.h>
38 static inline uint32_t bswap32(uint32_t x)
40 return x >> 24 | (x >> 8 & 0xff00) | (x << 8 & 0xff0000) | x << 24;
42 #endif
44 #ifdef __HAS_BUILTIN_BSWAP64
45 #define bswap64(x) __builtin_bswap64(x)
46 #else
47 #include <stdint.h>
48 static inline uint64_t bswap64(uint64_t x)
50 return ((uint64_t)bswap32(x)) << 32 | bswap32(x >> 32);
52 #endif
54 #endif