1 /*
2 * Copyright 2009-2015 Samy Al Bahra.
3 * Copyright 2014 Paul Khuong.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifndef CK_CC_H
29 #define CK_CC_H
30
31 #if defined(__GNUC__) || defined(__SUNPRO_C)
32 #include "gcc/ck_cc.h"
33 #endif
34
35 #ifndef CK_CC_RESTRICT
36 #define CK_CC_RESTRICT
37 #endif
38
39 #ifndef CK_CC_INLINE
40 #define CK_CC_INLINE inline
41 #endif
42
43 #ifndef CK_CC_FORCE_INLINE
44 #define CK_CC_FORCE_INLINE inline
45 #endif
46
47 #define CK_CC_DECONST_PTR(X) ((void *)(uintptr_t)(X))
48
49 /*
50 * Container function.
51 * This relies on (compiler) implementation-defined behavior.
52 */
53 #define CK_CC_CONTAINER(F, T, M, N) \
54 CK_CC_INLINE static T * \
55 N(F *p) \
56 { \
57 F *n = p; \
58 return (T *)(void *)(((char *)n) - ((size_t)&((T *)0)->M)); \
59 }
60
61 #define CK_CC_PAD(x) union { char pad[x]; }
62
63 #ifndef CK_CC_ALIASED
64 #define CK_CC_ALIASED
65 #endif
66
67 #ifndef CK_CC_UNUSED
68 #define CK_CC_UNUSED
69 #endif
70
71 #ifndef CK_CC_USED
72 #define CK_CC_USED
73 #endif
74
75 #ifndef CK_CC_IMM
76 #define CK_CC_IMM
77 #endif
78
79 #ifndef CK_CC_PACKED
80 #define CK_CC_PACKED
81 #endif
82
83 #ifndef CK_CC_WEAKREF
84 #define CK_CC_WEAKREF
85 #endif
86
87 #ifndef CK_CC_ALIGN
88 #define CK_CC_ALIGN(X)
89 #endif
90
91 #ifndef CK_CC_CACHELINE
92 #define CK_CC_CACHELINE
93 #endif
94
95 #ifndef CK_CC_LIKELY
96 #define CK_CC_LIKELY(x) x
97 #endif
98
99 #ifndef CK_CC_UNLIKELY
100 #define CK_CC_UNLIKELY(x) x
101 #endif
102
103 #ifndef CK_CC_TYPEOF
104 #define CK_CC_TYPEOF(X, DEFAULT) (DEFAULT)
105 #endif
106
107 #define CK_F_CC_FFS_G(L, T) \
108 CK_CC_INLINE static int \
109 ck_cc_##L(T v) \
110 { \
111 unsigned int i; \
112 \
113 if (v == 0) \
114 return 0; \
115 \
116 for (i = 1; (v & 1) == 0; i++, v >>= 1); \
117 return i; \
118 }
119
120 #ifndef CK_F_CC_FFS
121 #define CK_F_CC_FFS
CK_F_CC_FFS_G(ffs,unsigned int)122 CK_F_CC_FFS_G(ffs, unsigned int)
123 #endif /* CK_F_CC_FFS */
124
125 #ifndef CK_F_CC_FFSL
126 #define CK_F_CC_FFSL
127 CK_F_CC_FFS_G(ffsl, unsigned long)
128 #endif /* CK_F_CC_FFSL */
129
130 #ifndef CK_F_CC_FFSLL
131 #define CK_F_CC_FFSLL
132 CK_F_CC_FFS_G(ffsll, unsigned long long)
133 #endif /* CK_F_CC_FFSLL */
134
135 #undef CK_F_CC_FFS_G
136
137 #ifndef CK_F_CC_CTZ
138 #define CK_F_CC_CTZ
139 CK_CC_INLINE static int
140 ck_cc_ctz(unsigned int x)
141 {
142 unsigned int i;
143
144 if (x == 0)
145 return 0;
146
147 for (i = 0; (x & 1) == 0; i++, x >>= 1);
148 return i;
149 }
150 #endif
151
152 #ifndef CK_F_CC_POPCOUNT
153 #define CK_F_CC_POPCOUNT
154 CK_CC_INLINE static int
ck_cc_popcount(unsigned int x)155 ck_cc_popcount(unsigned int x)
156 {
157 unsigned int acc;
158
159 for (acc = 0; x != 0; x >>= 1)
160 acc += x & 1;
161
162 return acc;
163 }
164 #endif
165
166
167 #ifdef __cplusplus
168 #define CK_CPP_CAST(type, arg) static_cast<type>(arg)
169 #else
170 #define CK_CPP_CAST(type, arg) arg
171 #endif
172
173 #endif /* CK_CC_H */
174