Debian Codegraph @

lib/usual/bits.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2009 Marko Kreen
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /** @file
18 * Bit arithmetics.
19 *
20 * - is_power_of_2
21 * - ffs, ffsl, ffsll
22 * - fls, flsl, flsll
23 * - rol16, rol32, rol64
24 * - ror16, ror32, ror64
25 */
26 #ifndef _USUAL_BITS_H_
27 #define _USUAL_BITS_H_
28
29 #include <usual/base.h>
30
31 #include <string.h>
32 #include <limits.h>
33
34 /** Checks if integer has only one bit set */
35 static inline bool is_power_of_2(unsigned int n)
36 {
37 return (n > 0) && !(n & (n - 1));
38 }
39
40 /*
41 * Single-eval and type-safe rol/ror
42 */
43
44 /** Rotate 16-bit int to left */
45 static inline uint16_t rol16(uint16_t v, int s)
46 {
47 return (v << s) | (v >> (16 - s));
48 }
49 /** Rotate 32-bit int to left */
50 static inline uint32_t rol32(uint32_t v, int s)
51 {
52 return (v << s) | (v >> (32 - s));
53 }
54 /** Rotate 64-bit int to left */
55 static inline uint64_t rol64(uint64_t v, int s)
56 {
57 return (v << s) | (v >> (64 - s));
58 }
59
60 /** Rotate 16-bit int to right */
61 static inline uint16_t ror16(uint16_t v, int s) { return rol16(v, 16 - s); }
62
63 /** Rotate 32-bit int to right */
64 static inline uint32_t ror32(uint32_t v, int s) { return rol32(v, 32 - s); }
65
66 /** Rotate 64-bit int to right */
67 static inline uint64_t ror64(uint64_t v, int s) { return rol64(v, 64 - s); }
68
69 /*
70 * fls(int)
71 * flsl(long)
72 * flsll(long long)
73 *
74 * find MSB bit set, 1-based ofs, 0 if arg == 0
75 */
76
77 #undef fls
78 #undef flsl
79 #undef flsll
80 #define fls(x) usual_fls(x)
81 #define flsl(x) usual_flsl(x)
82 #define flsll(x) usual_flsll(x)
83
84 #if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_clzll)
85 #define _USUAL_FLS_(sfx, type) \
86 return (x == 0) ? 0 : ((8*sizeof(type)) - __builtin_clz ## sfx(x))
87 #else
88 #define _USUAL_FLS_(sfx, type) \
89 unsigned type u = x; \
90 unsigned int bit; \
91 if (x == 0) return 0; \
92 /* count from smallest bit, assuming small values */ \
93 for (bit = 1; u > 1; bit++) u >>= 1; \
94 return bit
95 #endif
96
97 /** Find last (highest) set bit, 1-based offset, 0 if arg == 0 */
98 static inline int fls(int x)
99 {
100 _USUAL_FLS_(, int);
101 }
102
103 /** Find last (highest) set bit, 1-based offset, 0 if arg == 0 */
104 static inline int flsl(long x)
105 {
106 _USUAL_FLS_(l, long);
107 }
108
109 /** Find last (highest) set bit, 1-based offset, 0 if arg == 0 */
110 static inline int flsll(long long x)
111 {
112 _USUAL_FLS_(ll, long long);
113 }
114
115 #undef _USUAL_FLS_
116
117 /*
118 * ffs(int)
119 * ffsl(long)
120 * ffsll(long long)
121 *
122 * find LSB bit set, 1-based ofs, 0 if arg == 0
123 */
124
125 #undef ffs
126 #undef ffsl
127 #undef ffsll
128 #define ffs(x) usual_ffs(x)
129 #define ffsl(x) usual_ffsl(x)
130 #define ffsll(x) usual_ffsll(x)
131
132 #if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_ffsll)
133 #define _USUAL_FFS_(sfx, type) \
134 return __builtin_ffs ## sfx((unsigned type)(x))
135 #else
136 #define _USUAL_FFS_(sfx, type) \
137 unsigned int bit; \
138 unsigned type u = x; \
139 if (!x) return 0; \
140 /* count from smallest bit, assuming small values */ \
141 for (bit = 1; !(u & 1); bit++) u >>= 1; \
142 return bit
143 #endif
144
145 /** Find first (lowest) set bit, 1-based ofs, 0 if arg == 0 */
146 static inline int ffs(int x)
147 {
148 _USUAL_FFS_(, int);
149 }
150
151 /** Find first (lowest) set bit, 1-based ofs, 0 if arg == 0 */
152 static inline int ffsl(long x)
153 {
154 _USUAL_FFS_(l, long);
155 }
156
157 /** Find first (lowest) set bit, 1-based ofs, 0 if arg == 0 */
158 static inline int ffsll(long long x)
159 {
160 _USUAL_FFS_(ll, long long);
161 }
162
163 #undef _USUAL_FFS_
164
165 /*
166 * Multiply and check overflow.
167 */
168
169 #define _USUAL_MUL_SAFE_(type, max) \
170 type unsafe = (type)(1) << (sizeof(type) * 8/2); /* sqrt(max+1) */ \
171 if (a < unsafe && b < unsafe) \
172 goto safe; \
173 if (!a || !b) \
174 goto safe; \
175 if ((max / a) >= b) \
176 goto safe; \
177 return false; \
178 safe: \
179 *res_p = a * b; \
180 return true;
181
182 /** Multiply with overflow check for 'unsigned int' */
183 static inline bool safe_mul_uint(unsigned int *res_p, unsigned int a, unsigned int b)
184 {
185 _USUAL_MUL_SAFE_(unsigned int, UINT_MAX);
186 }
187
188 /** Multiply with overflow check for 'unsigned long' */
189 static inline bool safe_mul_ulong(unsigned long *res_p, unsigned long a, unsigned long b)
190 {
191 _USUAL_MUL_SAFE_(unsigned long, ULONG_MAX);
192 }
193
194 /** Multiply with overflow check for 'uint8_t' */
195 static inline bool safe_mul_uint8(uint8_t *res_p, uint8_t a, uint8_t b)
196 {
197 _USUAL_MUL_SAFE_(uint8_t, UINT8_MAX);
198 }
199
200 /** Multiply with overflow check for 'uint16_t' */
201 static inline bool safe_mul_uint16(uint16_t *res_p, uint16_t a, uint16_t b)
202 {
203 _USUAL_MUL_SAFE_(uint16_t, UINT16_MAX);
204 }
205
206 /** Multiply with overflow check for 'uint32_t' */
207 static inline bool safe_mul_uint32(uint32_t *res_p, uint32_t a, uint32_t b)
208 {
209 _USUAL_MUL_SAFE_(uint32_t, UINT32_MAX);
210 }
211
212 /** Multiply with overflow check for 'uint64_t' */
213 static inline bool safe_mul_uint64(uint64_t *res_p, uint64_t a, uint64_t b)
214 {
215 _USUAL_MUL_SAFE_(uint64_t, UINT64_MAX);
216 }
217
218 /** Multiply with overflow check for 'size_t' */
219 static inline bool safe_mul_size(size_t *res_p, size_t a, size_t b)
220 {
221 _USUAL_MUL_SAFE_(size_t, SIZE_MAX);
222 }
223
224 #undef _USUAL_MUL_SAFE_
225
226 #endif