]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ck/include/ck_cc.h
Merge compiler-rt r291274.
[FreeBSD/FreeBSD.git] / sys / contrib / ck / include / ck_cc.h
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 #ifndef CK_F_CC_FFS
108 #define CK_F_CC_FFS
109 CK_CC_INLINE static int
110 ck_cc_ffs(unsigned int x)
111 {
112         unsigned int i;
113
114         if (x == 0)
115                 return 0;
116
117         for (i = 1; (x & 1) == 0; i++, x >>= 1);
118
119         return i;
120 }
121 #endif
122
123 #ifndef CK_F_CC_CLZ
124 #define CK_F_CC_CLZ
125 #include <ck_limits.h>
126
127 CK_CC_INLINE static int
128 ck_cc_clz(unsigned int x)
129 {
130         unsigned int count, i;
131
132         for (count = 0, i = sizeof(unsigned int) * CHAR_BIT; i > 0; count++) {
133                 unsigned int bit = 1U << --i;
134
135                 if (x & bit)
136                         break;
137         }
138
139         return count;
140 }
141 #endif
142
143 #ifndef CK_F_CC_CTZ
144 #define CK_F_CC_CTZ
145 CK_CC_INLINE static int
146 ck_cc_ctz(unsigned int x)
147 {
148         unsigned int i;
149
150         if (x == 0)
151                 return 0;
152
153         for (i = 0; (x & 1) == 0; i++, x >>= 1);
154
155         return i;
156 }
157 #endif
158
159 #ifndef CK_F_CC_POPCOUNT
160 #define CK_F_CC_POPCOUNT
161 CK_CC_INLINE static int
162 ck_cc_popcount(unsigned int x)
163 {
164         unsigned int acc;
165
166         for (acc = 0; x != 0; x >>= 1)
167                 acc += x & 1;
168
169         return acc;
170 }
171 #endif
172
173
174 #ifdef __cplusplus
175 #define CK_CPP_CAST(type, arg) static_cast<type>(arg)
176 #else
177 #define CK_CPP_CAST(type, arg) arg
178 #endif
179
180 #endif /* CK_CC_H */