xref: /freebsd/sys/contrib/ck/include/gcc/ck_cc.h (revision 9768746b)
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_GCC_CC_H
29 #define CK_GCC_CC_H
30 
31 #include <ck_md.h>
32 
33 #ifdef __SUNPRO_C
34 #define CK_CC_UNUSED
35 #define CK_CC_USED
36 #define CK_CC_IMM
37 #define CK_CC_IMM_U32
38 #else
39 #define CK_CC_UNUSED __attribute__((unused))
40 #define CK_CC_USED   __attribute__((used))
41 #define CK_CC_IMM "i"
42 
43 #define CK_CC_CONTAINER(F, T, M, N)					       \
44        CK_CC_INLINE static T *						       \
45        N(F *p)								       \
46        {								       \
47 									       \
48 	       return (T *)(void *)((char *)p - __builtin_offsetof(T, M));     \
49        }
50 
51 #if defined(__x86_64__) || defined(__x86__)
52 #define CK_CC_IMM_U32 "Z"
53 #define CK_CC_IMM_S32 "e"
54 #else
55 #define CK_CC_IMM_U32 CK_CC_IMM
56 #define CK_CC_IMM_S32 CK_CC_IMM
57 #endif /* __x86_64__ || __x86__ */
58 #endif
59 
60 #ifdef __OPTIMIZE__
61 #define CK_CC_INLINE CK_CC_UNUSED inline
62 #else
63 #define CK_CC_INLINE CK_CC_UNUSED
64 #endif
65 
66 #define CK_CC_FORCE_INLINE CK_CC_UNUSED __attribute__((always_inline)) inline
67 #define CK_CC_RESTRICT __restrict__
68 
69 /*
70  * Packed attribute.
71  */
72 #define CK_CC_PACKED __attribute__((packed))
73 
74 /*
75  * Weak reference.
76  */
77 #define CK_CC_WEAKREF __attribute__((weakref))
78 
79 /*
80  * Alignment attribute.
81  */
82 #define CK_CC_ALIGN(B) __attribute__((aligned(B)))
83 
84 /*
85  * Cache align.
86  */
87 #define CK_CC_CACHELINE CK_CC_ALIGN(CK_MD_CACHELINE)
88 
89 /*
90  * These are functions which should be avoided.
91  */
92 #ifdef __freestanding__
93 #pragma GCC poison malloc free
94 #endif
95 
96 /*
97  * Branch execution hints.
98  */
99 #define CK_CC_LIKELY(x) (__builtin_expect(!!(x), 1))
100 #define CK_CC_UNLIKELY(x) (__builtin_expect(!!(x), 0))
101 
102 /*
103  * Some compilers are overly strict regarding aliasing semantics.
104  * Unfortunately, in many cases it makes more sense to pay aliasing
105  * cost rather than overly expensive register spillage.
106  */
107 #define CK_CC_ALIASED __attribute__((__may_alias__))
108 
109 /*
110  * Compile-time typeof
111  */
112 #define CK_CC_TYPEOF(X, DEFAULT) __typeof__(X)
113 
114 /*
115  * Portability wrappers for bitwise operations.
116  */
117 #ifndef CK_MD_CC_BUILTIN_DISABLE
118 #define CK_F_CC_FFS
119 CK_CC_INLINE static int
120 ck_cc_ffs(unsigned int x)
121 {
122 
123 	return __builtin_ffsl(x);
124 }
125 
126 #define CK_F_CC_FFSL
127 CK_CC_INLINE static int
128 ck_cc_ffsl(unsigned long x)
129 {
130 
131 	return __builtin_ffsll(x);
132 }
133 
134 #define CK_F_CC_CTZ
135 CK_CC_INLINE static int
136 ck_cc_ctz(unsigned int x)
137 {
138 
139 	return __builtin_ctz(x);
140 }
141 
142 #define CK_F_CC_POPCOUNT
143 CK_CC_INLINE static int
144 ck_cc_popcount(unsigned int x)
145 {
146 
147 	return __builtin_popcount(x);
148 }
149 #endif /* CK_MD_CC_BUILTIN_DISABLE */
150 #endif /* CK_GCC_CC_H */
151