]> wimlib.net Git - wimlib/blob - include/wimlib/compiler-gcc.h
46983308ec8f6b2e1bae224d2b971e4bccdd361b
[wimlib] / include / wimlib / compiler-gcc.h
1 /*
2  * compiler-gcc.h
3  *
4  * The author dedicates this file to the public domain.
5  * You can do whatever you want with this file.
6  */
7
8 #ifndef _WIMLIB_COMPILER_GCC_H
9 #define _WIMLIB_COMPILER_GCC_H
10
11 #ifdef __WIN32__
12 #  define WIMLIBAPI __declspec(dllexport)
13 #else
14 #  define WIMLIBAPI __attribute__((visibility("default")))
15 #endif
16
17 #define _packed_attribute       __attribute__((packed))
18 #define _aligned_attribute(n)   __attribute__((aligned(n)))
19 #define _may_alias_attribute    __attribute__((may_alias))
20 #define likely(expr)            __builtin_expect(!!(expr), 1)
21 #define unlikely(expr)          __builtin_expect(!!(expr), 0)
22 #define prefetch(addr)          __builtin_prefetch(addr)
23 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
24 #  define _cold_attribute       __attribute__((cold))
25 #endif
26 #define _malloc_attribute       __attribute__((malloc))
27 #define inline                  inline __attribute__((always_inline))
28 #define noinline                __attribute__((noinline))
29
30 #define CPU_IS_BIG_ENDIAN       (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
31
32 #if defined(__x86_64__) || defined(__i386__)
33 #  define UNALIGNED_ACCESS_SPEED 3
34 #elif defined(__ARM_FEATURE_UNALIGNED) && (__ARM_FEATURE_UNALIGNED == 1)
35 #  define UNALIGNED_ACCESS_SPEED 2
36 #else
37 #  define UNALIGNED_ACCESS_SPEED 0
38 #endif
39
40 #define typeof     __typeof__
41
42 #ifndef min
43 #  define min(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
44                         (_a < _b) ? _a : _b; })
45 #endif
46
47 #ifndef max
48 #  define max(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
49                         (_a > _b) ? _a : _b; })
50 #endif
51
52 #ifndef swap
53 #  define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
54 #endif
55
56 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
57 #  define compiler_bswap32 __builtin_bswap32
58 #  define compiler_bswap64 __builtin_bswap64
59 #endif
60
61 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
62 #  define compiler_bswap16 __builtin_bswap16
63 #endif
64
65 #define compiler_fls32(n)       (31 - __builtin_clz(n))
66 #define compiler_fls64(n)       (63 - __builtin_clzll(n))
67 #define compiler_ffs32(n)       __builtin_ctz(n)
68 #define compiler_ffs64(n)       __builtin_ctzll(n)
69
70 #endif /* _WIMLIB_COMPILER_GCC_H */