xref: /freebsd/sys/contrib/ck/include/ck_cc.h (revision 5d3e7166)
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 #ifndef CK_CC_CONTAINER
54 #define CK_CC_CONTAINER(F, T, M, N)						\
55 	CK_CC_INLINE static T *							\
56 	N(F *p)									\
57 	{									\
58 		F *n = p;							\
59 		return (T *)(void *)(((char *)n) - ((size_t)&((T *)0)->M));	\
60 	}
61 #endif
62 
63 #define CK_CC_PAD(x) union { char pad[x]; }
64 
65 #ifndef CK_CC_ALIASED
66 #define CK_CC_ALIASED
67 #endif
68 
69 #ifndef CK_CC_UNUSED
70 #define CK_CC_UNUSED
71 #endif
72 
73 #ifndef CK_CC_USED
74 #define CK_CC_USED
75 #endif
76 
77 #ifndef CK_CC_IMM
78 #define CK_CC_IMM
79 #endif
80 
81 #ifndef CK_CC_PACKED
82 #define CK_CC_PACKED
83 #endif
84 
85 #ifndef CK_CC_WEAKREF
86 #define CK_CC_WEAKREF
87 #endif
88 
89 #ifndef CK_CC_ALIGN
90 #define CK_CC_ALIGN(X)
91 #endif
92 
93 #ifndef CK_CC_CACHELINE
94 #define CK_CC_CACHELINE
95 #endif
96 
97 #ifndef CK_CC_LIKELY
98 #define CK_CC_LIKELY(x) x
99 #endif
100 
101 #ifndef CK_CC_UNLIKELY
102 #define CK_CC_UNLIKELY(x) x
103 #endif
104 
105 #ifndef CK_CC_TYPEOF
106 #define CK_CC_TYPEOF(X, DEFAULT) (DEFAULT)
107 #endif
108 
109 #define CK_F_CC_FFS_G(L, T)				\
110 CK_CC_INLINE static int					\
111 ck_cc_##L(T v)						\
112 {							\
113 	unsigned int i;					\
114 							\
115 	if (v == 0)					\
116 		return 0;				\
117 							\
118 	for (i = 1; (v & 1) == 0; i++, v >>= 1);	\
119 	return i;					\
120 }
121 
122 #ifndef CK_F_CC_FFS
123 #define CK_F_CC_FFS
124 CK_F_CC_FFS_G(ffs, unsigned int)
125 #endif /* CK_F_CC_FFS */
126 
127 #ifndef CK_F_CC_FFSL
128 #define CK_F_CC_FFSL
129 CK_F_CC_FFS_G(ffsl, unsigned long)
130 #endif /* CK_F_CC_FFSL */
131 
132 #ifndef CK_F_CC_FFSLL
133 #define CK_F_CC_FFSLL
134 CK_F_CC_FFS_G(ffsll, unsigned long long)
135 #endif /* CK_F_CC_FFSLL */
136 
137 #undef CK_F_CC_FFS_G
138 
139 #ifndef CK_F_CC_CTZ
140 #define CK_F_CC_CTZ
141 CK_CC_INLINE static int
142 ck_cc_ctz(unsigned int x)
143 {
144 	unsigned int i;
145 
146 	if (x == 0)
147 		return 0;
148 
149 	for (i = 0; (x & 1) == 0; i++, x >>= 1);
150 	return i;
151 }
152 #endif
153 
154 #ifndef CK_F_CC_POPCOUNT
155 #define CK_F_CC_POPCOUNT
156 CK_CC_INLINE static int
157 ck_cc_popcount(unsigned int x)
158 {
159 	unsigned int acc;
160 
161 	for (acc = 0; x != 0; x >>= 1)
162 		acc += x & 1;
163 
164 	return acc;
165 }
166 #endif
167 
168 
169 #ifdef __cplusplus
170 #define CK_CPP_CAST(type, arg) static_cast<type>(arg)
171 #else
172 #define CK_CPP_CAST(type, arg) arg
173 #endif
174 
175 #endif /* CK_CC_H */
176