]> wimlib.net Git - wimlib/blob - include/wimlib/compiler-gcc.h
wimlib.h: document added behavior of WIMLIB_ADD_FLAG_WIMBOOT
[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 prefetchr(addr)         __builtin_prefetch((addr), 0)
23 #define prefetchw(addr)         __builtin_prefetch((addr), 1)
24 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
25 #  define _cold_attribute       __attribute__((cold))
26 #endif
27 #define _malloc_attribute       __attribute__((malloc))
28 #define inline                  inline __attribute__((always_inline))
29 #define noinline                __attribute__((noinline))
30
31 /* Newer gcc supports __BYTE_ORDER__.  Older gcc doesn't.  */
32 #ifdef __BYTE_ORDER__
33 #  define CPU_IS_BIG_ENDIAN     (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
34 #elif defined(HAVE_CONFIG_H)
35 #  include "config.h"
36 #  ifdef WORDS_BIGENDIAN
37 #    define CPU_IS_BIG_ENDIAN 1
38 #  else
39 #    define CPU_IS_BIG_ENDIAN 0
40 #  endif
41 #endif
42
43 #if defined(__x86_64__) || defined(__i386__)
44 #  define UNALIGNED_ACCESS_SPEED 3
45 #elif defined(__ARM_FEATURE_UNALIGNED) && (__ARM_FEATURE_UNALIGNED == 1)
46 #  define UNALIGNED_ACCESS_SPEED 2
47 #else
48 #  define UNALIGNED_ACCESS_SPEED 0
49 #endif
50
51 #define typeof     __typeof__
52
53 #ifndef min
54 #  define min(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
55                         (_a < _b) ? _a : _b; })
56 #endif
57
58 #ifndef max
59 #  define max(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
60                         (_a > _b) ? _a : _b; })
61 #endif
62
63 #ifndef swap
64 #  define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
65 #endif
66
67 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
68 #  define compiler_bswap32 __builtin_bswap32
69 #  define compiler_bswap64 __builtin_bswap64
70 #endif
71
72 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
73 #  define compiler_bswap16 __builtin_bswap16
74 #endif
75
76 #define compiler_fls32(n)       (31 - __builtin_clz(n))
77 #define compiler_fls64(n)       (63 - __builtin_clzll(n))
78 #define compiler_ffs32(n)       __builtin_ctz(n)
79 #define compiler_ffs64(n)       __builtin_ctzll(n)
80
81 #endif /* _WIMLIB_COMPILER_GCC_H */