9 #ifndef __libutilxx__sella__util__CommonMacro_H__
10 #define __libutilxx__sella__util__CommonMacro_H__
17 #define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
22 #define PREFETCH_LOCALITY_NONE 0
23 #define PREFETCH_LOCALITY_LOW 1
24 #define PREFETCH_LOCALITY_MEDIUM 2
25 #define PREFETCH_LOCALITY_HIGH 3
34 #define STANDARD_TYPEDEF(x) \
35 typedef std::shared_ptr<x> shared; \
54 #define CAS(variable, read, write) __sync_bool_compare_and_swap((variable), (read), (write))
56 #define ATOMIC_INC(x) __sync_add_and_fetch(&(x), 1)
57 #define ATOMIC_DEC(x) __sync_dec_and_fetch(&(x), 1)
59 #if defined __GNUC__ && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
60 #define API_DEPRECATED __attribute__((__deprecated__))
62 #define API_DEPRECATED
67 #define LIKELY(x) __builtin_expect(!!(x), 1)
68 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
72 #define UNLIKELY(x) (x)
76 #define HOT_FUNCTION __attribute__((__hot__))
81 #define BSWAP64(x) __builtin_bswap64((x))
82 #define BSWAP32(x) __builtin_bswap32((x))
83 #define BSWAP16(x) __builtin_bswap16((x))
85 #include <bits/wordsize.h>
88 #define CLZ32(x) (((x) == 0) ? 32 : __builtin_clz(x))
89 #define CTZ32(x) (((x) == 0) ? 0 : __builtin_ctz(x))
90 #define CLZ64(x) (((x) == 0) ? 64 : __builtin_clzl(x))
91 #define CTZ64(x) (((x) == 0) ? 0 : __builtin_ctzl(x))
92 #define PARITY32(x) __builtin_parity(x)
93 #define PARITY64(x) __builtin_parityl(x)
94 #define POPCOUNT32(x) __builtin_popcount(x)
95 #define POPCOUNT64(x) __builtin_popcountl(x)
96 #elif __WORDSIZE == 32
97 #define CLZ32(x) (((x) == 0) ? 32 : __builtin_clzl(x))
98 #define CTZ32(x) (((x) == 0) ? 0 : __builtin_ctzl(x))
99 #define CLZ64(x) (((x) == 0) ? 64 : __builtin_clzll(x))
100 #define CTZ64(x) (((x) == 0) ? 0 : __builtin_ctzll(x))
101 #define PARITY32(x) __builtin_parityl(x)
102 #define PARITY64(x) __builtin_parityll(x)
103 #define POPCOUNT32(x) __builtin_popcountl(x)
104 #define POPCOUNT64(x) __builtin_popcountll(x)
106 #error "Invalid machine word size"
109 #define CEIL_16(x) (((x) & 0x000000000000000f) ? (16 + (((x) >> 4) << 4)) : (((x) > 0) ? (x) : 16))
110 #define CEIL_32(x) (((x) & 0x000000000000001f) ? (32 + (((x) >> 5) << 5)) : (((x) > 0) ? (x) : 32))
111 #define CEIL_64(x) (((x) & 0x000000000000003f) ? (64 + (((x) >> 6) << 6)) : (((x) > 0) ? (x) : 64))
112 #define CEIL_128(x) (((x) & 0x000000000000007f) ? (128 + (((x) >> 7) << 7)) : (((x) > 0) ? (x) : 128))
113 #define CEIL_256(x) (((x) & 0x00000000000000ff) ? (256 + (((x) >> 8) << 8)) : (((x) > 0) ? (x) : 256))
114 #define CEIL_512(x) (((x) & 0x00000000000001ff) ? (512 + (((x) >> 9) << 9)) : (((x) > 0) ? (x) : 512))
115 #define CEIL_1024(x) (((x) & 0x00000000000003ff) ? (1024 + (((x) >> 10) << 10)) : (((x) > 0) ? (x) : 1024))
116 #define CEIL_2048(x) (((x) & 0x00000000000007ff) ? (2048 + (((x) >> 11) << 11)) : (((x) > 0) ? (x) : 2048))
117 #define CEIL_4096(x) (((x) & 0x0000000000000fff) ? (4096 + (((x) >> 12) << 12)) : (((x) > 0) ? (x) : 4096))
119 #define SAFE_FREE(x) if (x != 0) { free(x); x = 0; }
120 #define SAFE_DELETE(x) if (x != 0) { delete x; x = 0; }
121 #define SAFE_DELETE_ARRAY(x) if (x != 0) { delete[] x; x = 0; }
123 #define SAFE_REGFREE(x) if (x->buffer != NULL) { regfree(x); bzero(x, sizeof(regex_t)); }
127 #define FD_COPY(d,s) memcpy((d), (s), sizeof(*(d)))
132 #define htonll(x) (x)
133 #define ntohll(x) (x)
134 #elif defined(__GNUC__) && defined(__GLIBC__)
135 #include <byteswap.h>
136 #define htonll(x) bswap_64(x)
137 #define ntohll(x) bswap_64(x)
139 #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
140 #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))