1 /*-------------------------------------------------------------------------
2 stdbitint.h - ISO CX 7.18 Bit and byte utilitites <stdbit.h>
4 Copyright (C) 2022, Philipp Klaus Krause, krauseph@informatik.uni-freiburg.de
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
32 #ifndef __SIZE_T_DEFINED
33 #define __SIZE_T_DEFINED
34 typedef unsigned int size_t;
37 #ifndef __SPECIFIED_WIDTH_INTEGER_TYPES_DEFINED
38 #define __SPECIFIED_WIDTH_INTEGER_TYPES_DEFINED
42 typedef signed char int8_t;
43 typedef short int int16_t;
44 typedef long int int32_t;
45 #ifdef __SDCC_LONGLONG
46 typedef long long int int64_t;
50 typedef unsigned char uint8_t;
51 typedef unsigned short int uint16_t;
52 typedef unsigned long int uint32_t;
53 #ifdef __SDCC_LONGLONG
54 typedef unsigned long long int uint64_t;
60 typedef signed char int_least8_t;
61 typedef short int int_least16_t;
62 typedef long int int_least32_t;
63 #ifdef __SDCC_LONGLONG
64 typedef long long int int_least64_t;
68 typedef unsigned char uint_least8_t;
69 typedef unsigned short int uint_least16_t;
70 typedef unsigned long int uint_least32_t;
71 #ifdef __SDCC_LONGLONG
72 typedef unsigned long long int uint_least64_t;
78 typedef signed char int_fast8_t;
79 typedef int int_fast16_t;
80 typedef long int int_fast32_t;
81 #ifdef __SDCC_LONGLONG
82 typedef long long int int_fast64_t;
86 typedef unsigned char uint_fast8_t;
87 typedef unsigned int uint_fast16_t;
88 typedef unsigned long int uint_fast32_t;
89 #ifdef __SDCC_LONGLONG
90 typedef unsigned long long int uint_fast64_t;
93 #endif // __SPECIFIED_WIDTH_INTEGER_TYPES_DEFINED
97 #define __STDC_ENDIAN_LITTLE__ 0
98 #define __STDC_ENDIAN_BIG__ 1
100 #if defined(__SDCC_hc08) || defined(__SDCC_s08) || defined(__SDCC_stm8)
101 #define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__
103 #define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
106 #if __SDCC_BITINT_MAXWIDTH >= 64
107 #define __typewidth(x) _Generic((x), \
108 unsigned _BitInt(1) : 1, \
109 signed _BitInt(2) : 2, \
110 unsigned _BitInt(2) : 2, \
111 signed _BitInt(3) : 3, \
112 unsigned _BitInt(3) : 3, \
113 signed _BitInt(4) : 4, \
114 unsigned _BitInt(4) : 4, \
115 signed _BitInt(5) : 5, \
116 unsigned _BitInt(5) : 5, \
117 signed _BitInt(6) : 6, \
118 unsigned _BitInt(7) : 7, \
119 signed _BitInt(8) : 8, \
120 unsigned _BitInt(9) : 9, \
121 signed _BitInt(10) : 10, \
122 unsigned _BitInt(10) : 10, \
123 signed _BitInt(11) : 11, \
124 unsigned _BitInt(11) : 11, \
125 signed _BitInt(12) : 12, \
126 unsigned _BitInt(12) : 12, \
127 signed _BitInt(13) : 13, \
128 unsigned _BitInt(13) : 13, \
129 signed _BitInt(14) : 14, \
130 unsigned _BitInt(14) : 14, \
131 signed _BitInt(15) : 15, \
132 unsigned _BitInt(15) : 15, \
133 signed _BitInt(16) : 16, \
134 unsigned _BitInt(16) : 16, \
135 signed _BitInt(17) : 17, \
136 unsigned _BitInt(17) : 17, \
137 signed _BitInt(18) : 18, \
138 unsigned _BitInt(18) : 18, \
139 signed _BitInt(19) : 19, \
140 unsigned _BitInt(19) : 19, \
141 signed _BitInt(20) : 20, \
142 unsigned _BitInt(20) : 20, \
143 signed _BitInt(21) : 21, \
144 unsigned _BitInt(21) : 21, \
145 signed _BitInt(22) : 22, \
146 unsigned _BitInt(22) : 22, \
147 signed _BitInt(23) : 23, \
148 unsigned _BitInt(23) : 23, \
149 signed _BitInt(24) : 24, \
150 unsigned _BitInt(24) : 24, \
151 signed _BitInt(25) : 25, \
152 unsigned _BitInt(25) : 25, \
153 signed _BitInt(26) : 26, \
154 unsigned _BitInt(26) : 26, \
155 signed _BitInt(27) : 27, \
156 unsigned _BitInt(27) : 27, \
157 signed _BitInt(28) : 28, \
158 unsigned _BitInt(28) : 28, \
159 signed _BitInt(29) : 29, \
160 unsigned _BitInt(29) : 29, \
161 signed _BitInt(30) : 30, \
162 unsigned _BitInt(30) : 30, \
163 signed _BitInt(31) : 31, \
164 unsigned _BitInt(31) : 31, \
165 signed _BitInt(32) : 32, \
166 unsigned _BitInt(32) : 32, \
167 signed _BitInt(33) : 33, \
168 unsigned _BitInt(33) : 33, \
169 signed _BitInt(34) : 34, \
170 unsigned _BitInt(34) : 34, \
171 signed _BitInt(35) : 35, \
172 unsigned _BitInt(35) : 35, \
173 signed _BitInt(36) : 36, \
174 unsigned _BitInt(36) : 36, \
175 signed _BitInt(37) : 37, \
176 unsigned _BitInt(37) : 37, \
177 signed _BitInt(38) : 38, \
178 unsigned _BitInt(38) : 38, \
179 signed _BitInt(39) : 39, \
180 unsigned _BitInt(39) : 39, \
181 signed _BitInt(40) : 40, \
182 unsigned _BitInt(40) : 40, \
183 signed _BitInt(41) : 41, \
184 unsigned _BitInt(41) : 41, \
185 signed _BitInt(42) : 42, \
186 unsigned _BitInt(42) : 42, \
187 signed _BitInt(43) : 43, \
188 unsigned _BitInt(43) : 43, \
189 signed _BitInt(44) : 44, \
190 unsigned _BitInt(44) : 44, \
191 signed _BitInt(45) : 45, \
192 unsigned _BitInt(45) : 45, \
193 signed _BitInt(46) : 46, \
194 unsigned _BitInt(46) : 46, \
195 signed _BitInt(47) : 47, \
196 unsigned _BitInt(47) : 47, \
197 signed _BitInt(48) : 48, \
198 unsigned _BitInt(48) : 48, \
199 signed _BitInt(49) : 49, \
200 unsigned _BitInt(49) : 49, \
201 signed _BitInt(50) : 50, \
202 unsigned _BitInt(50) : 50, \
203 signed _BitInt(51) : 51, \
204 unsigned _BitInt(51) : 51, \
205 signed _BitInt(52) : 52, \
206 unsigned _BitInt(52) : 52, \
207 signed _BitInt(53) : 53, \
208 unsigned _BitInt(53) : 53, \
209 signed _BitInt(54) : 54, \
210 unsigned _BitInt(54) : 54, \
211 signed _BitInt(55) : 55, \
212 unsigned _BitInt(55) : 55, \
213 signed _BitInt(56) : 16, \
214 unsigned _BitInt(56) : 56, \
215 signed _BitInt(57) : 57, \
216 unsigned _BitInt(57) : 57, \
217 signed _BitInt(58) : 58, \
218 unsigned _BitInt(58) : 58, \
219 signed _BitInt(59) : 59, \
220 unsigned _BitInt(59) : 59, \
221 signed _BitInt(60) : 60, \
222 unsigned _BitInt(60) : 60, \
223 signed _BitInt(61) : 61, \
224 unsigned _BitInt(61) : 61, \
225 signed _BitInt(62) : 62, \
226 unsigned _BitInt(62) : 62, \
227 signed _BitInt(63) : 63, \
228 unsigned _BitInt(63) : 63, \
229 default: (sizeof(x) * CHAR_BIT))
231 #define __typewidth(x) (sizeof(x) * CHAR_BIT)
234 // C2X 7.18.3 Count Trailing Ones
235 int_fast8_t __stdc_count_leading_zeros(unsigned long long value
, uint_fast8_t width
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
236 #define stdc_count_leading_zeros(value) __stdc_count_leading_zeros((value), __typewidth(value))
237 #define stdc_count_leading_zerosuc(value) ((int)(stdc_count_leading_zeros((unsigned char)(value)))
238 #define stdc_count_leading_zerosus(value) ((int)(stdc_count_leading_zeros((unsigned short)(value)))
239 #define stdc_count_leading_zerosui(value) ((int)(stdc_count_leading_zeros((unsigned int)(value)))
240 #define stdc_count_leading_zerosul(value) ((int)(stdc_count_leading_zeros((unsigned long)(value)))
241 #define stdc_count_leading_zerosull(value) ((int)(stdc_count_leading_zeros((unsigned long long)(value)))
243 // C2X 7.18.4 Count Leading Ones
244 #define stdc_count_leading_ones(value) __stdc_count_leading_zeros(~(value), __typewidth(value))
245 #define stdc_count_leading_onesuc(value) ((int)(stdc_count_leading_ones((unsigned char)(value)))
246 #define stdc_count_leading_onesus(value) ((int)(stdc_count_leading_ones((unsigned short)(value)))
247 #define stdc_count_leading_onesui(value) ((int)(stdc_count_leading_ones((unsigned int)(value)))
248 #define stdc_count_leading_onesul(value) ((int)(stdc_count_leading_ones((unsigned long)(value)))
249 #define stdc_count_leading_onesull(value) ((int)(stdc_count_leading_ones((unsigned long long)(value)))
251 // C2X 7.18.6 Count Trailing Ones
252 int_fast8_t __stdc_count_trailing_onesull(unsigned long long value
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
253 #define stdc_count_trailing_ones(value) __stdc_count_trailing_onesull(value) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
254 #define stdc_count_trailing_onesuc(value) ((int)(stdc_count_trailing_ones((unsigned char)(value)))
255 #define stdc_count_trailing_onesus(value) ((int)(stdc_count_trailing_ones((unsigned short)(value)))
256 #define stdc_count_trailing_onesui(value) ((int)(stdc_count_trailing_ones((unsigned int)(value)))
257 #define stdc_count_trailing_onesul(value) ((int)(stdc_count_trailing_ones((unsigned long)(value)))
258 #define stdc_count_trailing_onesull(value) ((int)(stdc_count_trailing_ones((unsigned long long)(value)))
260 // C2X 7.18.5 Count Trailing Zeros
261 #define stdc_count_trailing_zeros(value) stdc_count_trailing_ones((typeof(value))~(value))
262 #define stdc_count_trailing_zerosuc(value) ((int)(stdc_count_trailing_ones((unsigned char)(~(value))))
263 #define stdc_count_trailing_zerosus(value) ((int)(stdc_count_trailing_ones((unsigned short)(~(value))))
264 #define stdc_count_trailing_zerosui(value) ((int)(stdc_count_trailing_ones((unsigned int)(~(value))))
265 #define stdc_count_trailing_zerosul(value) ((int)(stdc_count_trailing_ones((unsigned long)(~(value))))
266 #define stdc_count_trailing_zerosull(value) ((int)(stdc_count_trailing_ones((unsigned long long)(~(value))))
268 // C2X 7.18.8 First Leading One
269 int_fast8_t __stdc_first_leading_one(unsigned long long value
, uint_fast8_t width
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
270 #define stdc_first_leading_one(value) __stdc_first_leading_one((value), __typewidth(value))
271 #define stdc_first_leading_oneuc(value) ((int)(stdc_first_leading_one((unsigned char)(~(value))))
272 #define stdc_first_leading_oneus(value) ((int)(stdc_first_leading_one((unsigned short)(~(value))))
273 #define stdc_first_leading_oneui(value) ((int)(stdc_first_leading_one((unsigned int)(~(value))))
274 #define stdc_first_leading_oneul(value) ((int)(stdc_first_leading_one((unsigned long)(~(value))))
275 #define stdc_first_leading_oneull(value) ((int)(stdc_first_leading_one((unsigned long long)(~(value))))
277 // C2X 7.18.7 First Leading Zero
278 #define stdc_first_leading_zero(value) __stdc_first_leading_one(~(value), __typewidth(value))
279 #define stdc_first_leading_zerouc(value) ((int)(stdc_first_leading_zero((unsigned char)(~(value))))
280 #define stdc_first_leading_zerous(value) ((int)(stdc_first_leading_zero((unsigned short)(~(value))))
281 #define stdc_first_leading_zeroui(value) ((int)(stdc_first_leading_zero((unsigned int)(~(value))))
282 #define stdc_first_leading_zeroul(value) ((int)(stdc_first_leading_zero((unsigned long)(~(value))))
283 #define stdc_first_leading_zeroull(value) ((int)(stdc_first_leading_zero((unsigned long long)(~(value))))
285 // C2X 7.18.10 First Trailing One
286 int_fast8_t __stdc_first_trailing_oneull(unsigned long long value
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
287 #define stdc_first_trailing_one(value) __stdc_first_trailing_oneull(value) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
288 #define stdc_first_trailing_oneuc(value) ((int)(stdc_first_trailing_one((unsigned char)(value)))
289 #define stdc_first_trailing_oneus(value) ((int)(stdc_first_trailing_one((unsigned short)(value)))
290 #define stdc_first_trailing_oneui(value) ((int)(stdc_first_trailing_one((unsigned int)(value)))
291 #define stdc_first_trailing_oneul(value) ((int)(stdc_first_trailing_one((unsigned long)(value)))
292 #define stdc_first_trailing_oneull(value) ((int)(stdc_first_trailing_one((unsigned long long)(value)))
294 // C2X 7.18.9 First Trailing Zero
295 #define stdc_first_trailing_zero(value) __stdc_first_trailing_oneull((unsigned long long)~(value)) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
296 #define stdc_first_trailing_zerouc(value) ((int)(stdc_first_trailing_zero((unsigned char)(value)))
297 #define stdc_first_trailing_zerous(value) ((int)(stdc_first_trailing_zero((unsigned short)(value)))
298 #define stdc_first_trailing_zeroui(value) ((int)(stdc_first_trailing_zero((unsigned int)(value)))
299 #define stdc_first_trailing_zeroul(value) ((int)(stdc_first_trailing_zero((unsigned long)(value)))
300 #define stdc_first_trailing_zeroull(value) ((int)(stdc_first_trailing_zero((unsigned long long)(value)))
302 // C2X 7.18.11 Count Ones
303 int_fast8_t __stdc_count_onesull(unsigned long long value
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
304 #define stdc_count_ones(value) __stdc_count_onesull(value) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
305 #define stdc_count_onesuc(value) ((int)(stdc_count_ones((unsigned char)(value)))
306 #define stdc_count_onesus(value) ((int)(stdc_count_ones((unsigned short)(value)))
307 #define stdc_count_onesui(value) ((int)(stdc_count_ones((unsigned int)(value)))
308 #define stdc_count_onesul(value) ((int)(stdc_count_ones((unsigned long)(value)))
309 #define stdc_count_onesull(value) ((int)(stdc_count_ones((unsigned long long)(value)))
311 // C2X 7.18.12 Count Zeros
312 #define stdc_count_zeros(value) stdc_count_ones((typeof(value))~(value))
313 #define stdc_count_zerosuc(value) ((int)(stdc_count_ones((unsigned char)(~(value))))
314 #define stdc_count_zerosus(value) ((int)(stdc_count_ones((unsigned short)(~(value))))
315 #define stdc_count_zerosui(value) ((int)(stdc_count_ones((unsigned int)(~(value))))
316 #define stdc_count_zerosul(value) ((int)(stdc_count_ones((unsigned long)(~(value))))
317 #define stdc_count_zerosull(value) ((int)(stdc_count_ones((unsigned long long)(~(value))))
319 // C2X 7.18.13 Single-bit Check
320 #define stdc_has_single_bit(value) ((_Bool)(stdc_count_ones(value) == 1))
321 #define stdc_has_single_bituc(value) stdc_has_single_bit((unsigned char)(value))
322 #define stdc_has_single_bitus(value) stdc_has_single_bit((unsigned short)(value))
323 #define stdc_has_single_bitui(value) stdc_has_single_bit((unsigned int)(value))
324 #define stdc_has_single_bitul(value) stdc_has_single_bit((unsigned long)(value))
325 #define stdc_has_single_bitull(value) stdc_has_single_bit((unsigned long long)(value))
327 // C2X 7.18.14 Bit Width
328 int_fast8_t __stdc_bit_widthull(unsigned long long value
); // Todo: Use _BitInt(8) here once all ports support it, so we avoid integer promotion for some cases.
329 #define stdc_bit_width(value) __stdc_bit_widthull(value) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
330 #define stdc_bit_widthuc(value) ((int)(stdc_bit_width((unsigned char)(value)))
331 #define stdc_bit_widthus(value) ((int)(stdc_bit_width((unsigned short)(value)))
332 #define stdc_bit_widthui(value) ((int)(stdc_bit_width((unsigned int)(value)))
333 #define stdc_bit_widthul(value) ((int)(stdc_bit_width((unsigned long)(value)))
334 #define stdc_bit_widthull(value) ((int)(stdc_bit_width((unsigned long long)(value)))
336 // C2X 7.18.15 Bit Floor
337 #define stdc_bit_floor(value) ((typeof(value))(1ull << stdc_bit_width(value)) >> 1)
338 #define stdc_bit_flooruc(value) ((unsigned char)(1 << stdc_bit_width(value)((unsigned char)(value))) >> 1)
339 #define stdc_bit_floorus(value) ((unsigned short)(1 << stdc_bit_width((unsigned short)(value))) >> 1)
340 #define stdc_bit_floorui(value) (1u << stdc_bit_width((unsigned int)(value) >> 1)
341 #define stdc_bit_floorul(value) (1ul << stdc_bit_width((unsigned long)(value) >> 1)
342 #define stdc_bit_floorull(value) (1ull << stdc_bit_width((unsigned long long)(value) >> 1)
344 // C2X 7.18.16 Bit Ceiling
345 unsigned long long __stdc_bit_ceilull(unsigned long long value
);
346 #define stdc_bit_ceil(value) ((typeof(value))(__stdc_bit_ceilull(value))) // Todo: Use some speed-optimized variants here later. Via _Generic or sizeof.
347 #define stdc_bit_ceiluc(value) (stdc_bit_ceil(value)((unsigned char)(value))))
348 #define stdc_bit_ceilus(value) (stdc_bit_ceil(value)((unsigned short)(value))))
349 #define stdc_bit_ceilui(value) (stdc_bit_ceil(value)((unsigned int)(value))))
350 #define stdc_bit_ceilul(value) (stdc_bit_ceil(value)((unsigned long)(value))))
351 #define stdc_bit_ceilull(value) (stdc_bit_ceil(value)((unsigned long long)(value))))