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