libutil++  1.9.3
 All Classes Functions Variables
CommonMacro.h
1 /*
2 ** libutil++
3 ** $Id: CommonMacro.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__util__CommonMacro_H__
10 #define __libutilxx__sella__util__CommonMacro_H__
11 
17 #define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
18 
19 #define PREFETCH_RO 0
20 #define PREFETCH_RW 1
21 
22 #define PREFETCH_LOCALITY_NONE 0
23 #define PREFETCH_LOCALITY_LOW 1
24 #define PREFETCH_LOCALITY_MEDIUM 2
25 #define PREFETCH_LOCALITY_HIGH 3
26 
34 #define STANDARD_TYPEDEF(x) \
35  typedef std::shared_ptr<x> shared; \
36  typedef x * ptr;
37 
54 #define CAS(variable, read, write) __sync_bool_compare_and_swap((variable), (read), (write))
55 
56 #define ATOMIC_INC(x) __sync_add_and_fetch(&(x), 1)
57 #define ATOMIC_DEC(x) __sync_dec_and_fetch(&(x), 1)
58 
59 #if defined __GNUC__ && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
60  #define API_DEPRECATED __attribute__((__deprecated__))
61 #else
62  #define API_DEPRECATED
63 #endif
64 
65 #if (__GNUC__ >= 3)
66  #if !defined(LIKELY)
67  #define LIKELY(x) __builtin_expect(!!(x), 1) /* '!!' converts any !=0 to a 1 */
68  #define UNLIKELY(x) __builtin_expect(!!(x), 0)
69  #endif
70 #else
71  #define LIKELY(x) (x)
72  #define UNLIKELY(x) (x)
73 #endif
74 
75 #ifndef NDEBUG
76  #define HOT_FUNCTION __attribute__((__hot__))
77 #else
78  #define HOT_FUNCTION
79 #endif
80 
81 #define BSWAP64(x) __builtin_bswap64((x))
82 #define BSWAP32(x) __builtin_bswap32((x))
83 #define BSWAP16(x) __builtin_bswap16((x))
84 
85 #include <bits/wordsize.h>
86 
87 #if __WORDSIZE == 64
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)
105 #else
106  #error "Invalid machine word size"
107 #endif
108 
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))
118 
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; }
122 #include <regex.h>
123 #define SAFE_REGFREE(x) if (x->buffer != NULL) { regfree(x); bzero(x, sizeof(regex_t)); }
124 
125 #ifndef FD_COPY
126  #include <string.h>
127  #define FD_COPY(d,s) memcpy((d), (s), sizeof(*(d)))
128 #endif
129 
130 #include <endian.h>
131 #ifdef _BIG_ENDIAN
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)
138 #else
139  #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
140  #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
141 #endif
142 
143 #endif
144 
145 /*
146 ** vim: noet ts=3 sw=3
147 */