1*aaceae98SRobert Mustacchi /*
2*aaceae98SRobert Mustacchi * This file and its contents are supplied under the terms of the
3*aaceae98SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
4*aaceae98SRobert Mustacchi * You may only use this file in accordance with the terms of version
5*aaceae98SRobert Mustacchi * 1.0 of the CDDL.
6*aaceae98SRobert Mustacchi *
7*aaceae98SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this
8*aaceae98SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at
9*aaceae98SRobert Mustacchi * http://www.illumos.org/license/CDDL.
10*aaceae98SRobert Mustacchi */
11*aaceae98SRobert Mustacchi
12*aaceae98SRobert Mustacchi /*
13*aaceae98SRobert Mustacchi * Copyright 2024 Oxide Computer Company
14*aaceae98SRobert Mustacchi */
15*aaceae98SRobert Mustacchi
16*aaceae98SRobert Mustacchi /*
17*aaceae98SRobert Mustacchi * This provides a standard implementation for the C23 stdbit.h non-generic
18*aaceae98SRobert Mustacchi * functions suitable for both libc and the kernel. These are implemented
19*aaceae98SRobert Mustacchi * generally leveraging compiler builtins which should not use the FPU.
20*aaceae98SRobert Mustacchi *
21*aaceae98SRobert Mustacchi * It's worth remembering that the 'long' type varies in our two environments:
22*aaceae98SRobert Mustacchi * ILP32 and LP64. As such, that's why we generally calculate type bit widths by
23*aaceae98SRobert Mustacchi * using the sizeof (type) * CHAR_BITS.
24*aaceae98SRobert Mustacchi */
25*aaceae98SRobert Mustacchi
26*aaceae98SRobert Mustacchi #include <sys/stdbit.h>
27*aaceae98SRobert Mustacchi #ifndef _KERNEL
28*aaceae98SRobert Mustacchi #include <limits.h>
29*aaceae98SRobert Mustacchi #else
30*aaceae98SRobert Mustacchi #include <sys/types.h>
31*aaceae98SRobert Mustacchi #endif
32*aaceae98SRobert Mustacchi
33*aaceae98SRobert Mustacchi /*
34*aaceae98SRobert Mustacchi * Count Leading Zeros functions. These leverage a builtin which is undefined at
35*aaceae98SRobert Mustacchi * zero. The builtin will promote everything to an unsigned int, therefore we
36*aaceae98SRobert Mustacchi * need to make sure to subtract resulting values there to make sure we're not
37*aaceae98SRobert Mustacchi * counting sign extension bits.
38*aaceae98SRobert Mustacchi */
39*aaceae98SRobert Mustacchi unsigned int
stdc_leading_zeros_uc(unsigned char uc)40*aaceae98SRobert Mustacchi stdc_leading_zeros_uc(unsigned char uc)
41*aaceae98SRobert Mustacchi {
42*aaceae98SRobert Mustacchi if (uc == 0) {
43*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned char));
44*aaceae98SRobert Mustacchi }
45*aaceae98SRobert Mustacchi
46*aaceae98SRobert Mustacchi return (__builtin_clz(uc) -
47*aaceae98SRobert Mustacchi (sizeof (unsigned int) - sizeof (unsigned char)) * CHAR_BIT);
48*aaceae98SRobert Mustacchi }
49*aaceae98SRobert Mustacchi
50*aaceae98SRobert Mustacchi unsigned int
stdc_leading_zeros_us(unsigned short us)51*aaceae98SRobert Mustacchi stdc_leading_zeros_us(unsigned short us)
52*aaceae98SRobert Mustacchi {
53*aaceae98SRobert Mustacchi if (us == 0) {
54*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned short));
55*aaceae98SRobert Mustacchi }
56*aaceae98SRobert Mustacchi
57*aaceae98SRobert Mustacchi return (__builtin_clz(us) -
58*aaceae98SRobert Mustacchi (sizeof (unsigned int) - sizeof (unsigned short)) * CHAR_BIT);
59*aaceae98SRobert Mustacchi }
60*aaceae98SRobert Mustacchi
61*aaceae98SRobert Mustacchi unsigned int
stdc_leading_zeros_ui(unsigned int ui)62*aaceae98SRobert Mustacchi stdc_leading_zeros_ui(unsigned int ui)
63*aaceae98SRobert Mustacchi {
64*aaceae98SRobert Mustacchi if (ui == 0) {
65*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned int));
66*aaceae98SRobert Mustacchi }
67*aaceae98SRobert Mustacchi
68*aaceae98SRobert Mustacchi return (__builtin_clz(ui));
69*aaceae98SRobert Mustacchi }
70*aaceae98SRobert Mustacchi
71*aaceae98SRobert Mustacchi unsigned int
stdc_leading_zeros_ul(unsigned long ul)72*aaceae98SRobert Mustacchi stdc_leading_zeros_ul(unsigned long ul)
73*aaceae98SRobert Mustacchi {
74*aaceae98SRobert Mustacchi if (ul == 0) {
75*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long));
76*aaceae98SRobert Mustacchi }
77*aaceae98SRobert Mustacchi
78*aaceae98SRobert Mustacchi return (__builtin_clzl(ul));
79*aaceae98SRobert Mustacchi }
80*aaceae98SRobert Mustacchi
81*aaceae98SRobert Mustacchi unsigned int
stdc_leading_zeros_ull(unsigned long long ull)82*aaceae98SRobert Mustacchi stdc_leading_zeros_ull(unsigned long long ull)
83*aaceae98SRobert Mustacchi {
84*aaceae98SRobert Mustacchi if (ull == 0) {
85*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long long));
86*aaceae98SRobert Mustacchi }
87*aaceae98SRobert Mustacchi
88*aaceae98SRobert Mustacchi return (__builtin_clzll(ull));
89*aaceae98SRobert Mustacchi }
90*aaceae98SRobert Mustacchi
91*aaceae98SRobert Mustacchi /*
92*aaceae98SRobert Mustacchi * Count Leading Ones functions. We simply invert these functions and then treat
93*aaceae98SRobert Mustacchi * it as a leading zeros problem.
94*aaceae98SRobert Mustacchi */
95*aaceae98SRobert Mustacchi unsigned int
stdc_leading_ones_uc(unsigned char uc)96*aaceae98SRobert Mustacchi stdc_leading_ones_uc(unsigned char uc)
97*aaceae98SRobert Mustacchi {
98*aaceae98SRobert Mustacchi return (stdc_leading_zeros_uc(~uc));
99*aaceae98SRobert Mustacchi }
100*aaceae98SRobert Mustacchi
101*aaceae98SRobert Mustacchi unsigned int
stdc_leading_ones_us(unsigned short us)102*aaceae98SRobert Mustacchi stdc_leading_ones_us(unsigned short us)
103*aaceae98SRobert Mustacchi {
104*aaceae98SRobert Mustacchi return (stdc_leading_zeros_us(~us));
105*aaceae98SRobert Mustacchi }
106*aaceae98SRobert Mustacchi
107*aaceae98SRobert Mustacchi unsigned int
stdc_leading_ones_ui(unsigned int ui)108*aaceae98SRobert Mustacchi stdc_leading_ones_ui(unsigned int ui)
109*aaceae98SRobert Mustacchi {
110*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ui(~ui));
111*aaceae98SRobert Mustacchi }
112*aaceae98SRobert Mustacchi
113*aaceae98SRobert Mustacchi unsigned int
stdc_leading_ones_ul(unsigned long ul)114*aaceae98SRobert Mustacchi stdc_leading_ones_ul(unsigned long ul)
115*aaceae98SRobert Mustacchi {
116*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ul(~ul));
117*aaceae98SRobert Mustacchi }
118*aaceae98SRobert Mustacchi
119*aaceae98SRobert Mustacchi unsigned int
stdc_leading_ones_ull(unsigned long long ull)120*aaceae98SRobert Mustacchi stdc_leading_ones_ull(unsigned long long ull)
121*aaceae98SRobert Mustacchi {
122*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ull(~ull));
123*aaceae98SRobert Mustacchi }
124*aaceae98SRobert Mustacchi
125*aaceae98SRobert Mustacchi /*
126*aaceae98SRobert Mustacchi * Count Trailing Zeros functions. These leverage a builtin check which is
127*aaceae98SRobert Mustacchi * undefined at zero. While the builtin promotes smaller values to an unsigned
128*aaceae98SRobert Mustacchi * int, we don't need to adjust the value like with count leading zeros.
129*aaceae98SRobert Mustacchi */
130*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_zeros_uc(unsigned char uc)131*aaceae98SRobert Mustacchi stdc_trailing_zeros_uc(unsigned char uc)
132*aaceae98SRobert Mustacchi {
133*aaceae98SRobert Mustacchi if (uc == 0) {
134*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned char));
135*aaceae98SRobert Mustacchi }
136*aaceae98SRobert Mustacchi
137*aaceae98SRobert Mustacchi return (__builtin_ctz(uc));
138*aaceae98SRobert Mustacchi }
139*aaceae98SRobert Mustacchi
140*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_zeros_us(unsigned short us)141*aaceae98SRobert Mustacchi stdc_trailing_zeros_us(unsigned short us)
142*aaceae98SRobert Mustacchi {
143*aaceae98SRobert Mustacchi if (us == 0) {
144*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned short));
145*aaceae98SRobert Mustacchi }
146*aaceae98SRobert Mustacchi
147*aaceae98SRobert Mustacchi return (__builtin_ctz(us));
148*aaceae98SRobert Mustacchi }
149*aaceae98SRobert Mustacchi
150*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_zeros_ui(unsigned int ui)151*aaceae98SRobert Mustacchi stdc_trailing_zeros_ui(unsigned int ui)
152*aaceae98SRobert Mustacchi {
153*aaceae98SRobert Mustacchi if (ui == 0) {
154*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned int));
155*aaceae98SRobert Mustacchi }
156*aaceae98SRobert Mustacchi
157*aaceae98SRobert Mustacchi return (__builtin_ctz(ui));
158*aaceae98SRobert Mustacchi }
159*aaceae98SRobert Mustacchi
160*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_zeros_ul(unsigned long ul)161*aaceae98SRobert Mustacchi stdc_trailing_zeros_ul(unsigned long ul)
162*aaceae98SRobert Mustacchi {
163*aaceae98SRobert Mustacchi if (ul == 0) {
164*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long));
165*aaceae98SRobert Mustacchi }
166*aaceae98SRobert Mustacchi
167*aaceae98SRobert Mustacchi return (__builtin_ctzl(ul));
168*aaceae98SRobert Mustacchi }
169*aaceae98SRobert Mustacchi
170*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_zeros_ull(unsigned long long ull)171*aaceae98SRobert Mustacchi stdc_trailing_zeros_ull(unsigned long long ull)
172*aaceae98SRobert Mustacchi {
173*aaceae98SRobert Mustacchi if (ull == 0) {
174*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long long));
175*aaceae98SRobert Mustacchi }
176*aaceae98SRobert Mustacchi
177*aaceae98SRobert Mustacchi return (__builtin_ctzll(ull));
178*aaceae98SRobert Mustacchi }
179*aaceae98SRobert Mustacchi
180*aaceae98SRobert Mustacchi /*
181*aaceae98SRobert Mustacchi * Count Trailing Ones functions. We treat these as just the inverse of the
182*aaceae98SRobert Mustacchi * leading zeros problem.
183*aaceae98SRobert Mustacchi */
184*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_ones_uc(unsigned char uc)185*aaceae98SRobert Mustacchi stdc_trailing_ones_uc(unsigned char uc)
186*aaceae98SRobert Mustacchi {
187*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_uc(~uc));
188*aaceae98SRobert Mustacchi }
189*aaceae98SRobert Mustacchi
190*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_ones_us(unsigned short us)191*aaceae98SRobert Mustacchi stdc_trailing_ones_us(unsigned short us)
192*aaceae98SRobert Mustacchi {
193*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_us(~us));
194*aaceae98SRobert Mustacchi }
195*aaceae98SRobert Mustacchi
196*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_ones_ui(unsigned int ui)197*aaceae98SRobert Mustacchi stdc_trailing_ones_ui(unsigned int ui)
198*aaceae98SRobert Mustacchi {
199*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ui(~ui));
200*aaceae98SRobert Mustacchi }
201*aaceae98SRobert Mustacchi
202*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_ones_ul(unsigned long ul)203*aaceae98SRobert Mustacchi stdc_trailing_ones_ul(unsigned long ul)
204*aaceae98SRobert Mustacchi {
205*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ul(~ul));
206*aaceae98SRobert Mustacchi }
207*aaceae98SRobert Mustacchi
208*aaceae98SRobert Mustacchi unsigned int
stdc_trailing_ones_ull(unsigned long long ull)209*aaceae98SRobert Mustacchi stdc_trailing_ones_ull(unsigned long long ull)
210*aaceae98SRobert Mustacchi {
211*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ull(~ull));
212*aaceae98SRobert Mustacchi }
213*aaceae98SRobert Mustacchi
214*aaceae98SRobert Mustacchi /*
215*aaceae98SRobert Mustacchi * First Leading Zero functions. We cannot use an inversed find first set here
216*aaceae98SRobert Mustacchi * because the builtin operates on signed integers. As this is looking for the
217*aaceae98SRobert Mustacchi * least significant zero, a different way to phrase this is how many leading
218*aaceae98SRobert Mustacchi * ones exist. That indicates the first zero index is that plus one as long as
219*aaceae98SRobert Mustacchi * we're not at the maximum unsigned integer value for the range, which we need
220*aaceae98SRobert Mustacchi * to special case as zero.
221*aaceae98SRobert Mustacchi */
222*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_zero_uc(unsigned char uc)223*aaceae98SRobert Mustacchi stdc_first_leading_zero_uc(unsigned char uc)
224*aaceae98SRobert Mustacchi {
225*aaceae98SRobert Mustacchi if (uc == UCHAR_MAX) {
226*aaceae98SRobert Mustacchi return (0);
227*aaceae98SRobert Mustacchi }
228*aaceae98SRobert Mustacchi
229*aaceae98SRobert Mustacchi return (stdc_leading_ones_uc(uc) + 1);
230*aaceae98SRobert Mustacchi }
231*aaceae98SRobert Mustacchi
232*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_zero_us(unsigned short us)233*aaceae98SRobert Mustacchi stdc_first_leading_zero_us(unsigned short us)
234*aaceae98SRobert Mustacchi {
235*aaceae98SRobert Mustacchi if (us == USHRT_MAX) {
236*aaceae98SRobert Mustacchi return (0);
237*aaceae98SRobert Mustacchi }
238*aaceae98SRobert Mustacchi
239*aaceae98SRobert Mustacchi return (stdc_leading_ones_us(us) + 1);
240*aaceae98SRobert Mustacchi }
241*aaceae98SRobert Mustacchi
242*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_zero_ui(unsigned int ui)243*aaceae98SRobert Mustacchi stdc_first_leading_zero_ui(unsigned int ui)
244*aaceae98SRobert Mustacchi {
245*aaceae98SRobert Mustacchi if (ui == UINT_MAX) {
246*aaceae98SRobert Mustacchi return (0);
247*aaceae98SRobert Mustacchi }
248*aaceae98SRobert Mustacchi
249*aaceae98SRobert Mustacchi return (stdc_leading_ones_ui(ui) + 1);
250*aaceae98SRobert Mustacchi }
251*aaceae98SRobert Mustacchi
252*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_zero_ul(unsigned long ul)253*aaceae98SRobert Mustacchi stdc_first_leading_zero_ul(unsigned long ul)
254*aaceae98SRobert Mustacchi {
255*aaceae98SRobert Mustacchi if (ul == ULONG_MAX) {
256*aaceae98SRobert Mustacchi return (0);
257*aaceae98SRobert Mustacchi }
258*aaceae98SRobert Mustacchi
259*aaceae98SRobert Mustacchi return (stdc_leading_ones_ul(ul) + 1);
260*aaceae98SRobert Mustacchi }
261*aaceae98SRobert Mustacchi
262*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_zero_ull(unsigned long long ull)263*aaceae98SRobert Mustacchi stdc_first_leading_zero_ull(unsigned long long ull)
264*aaceae98SRobert Mustacchi {
265*aaceae98SRobert Mustacchi if (ull == ULLONG_MAX) {
266*aaceae98SRobert Mustacchi return (0);
267*aaceae98SRobert Mustacchi }
268*aaceae98SRobert Mustacchi
269*aaceae98SRobert Mustacchi return (stdc_leading_ones_ull(ull) + 1);
270*aaceae98SRobert Mustacchi }
271*aaceae98SRobert Mustacchi
272*aaceae98SRobert Mustacchi /*
273*aaceae98SRobert Mustacchi * First Leading One functions. This is looking for the most significant one.
274*aaceae98SRobert Mustacchi * Like with finding the most significant zero, this can be phrased as counting
275*aaceae98SRobert Mustacchi * the number of leading zeroes and then adding one to get the index. Here we
276*aaceae98SRobert Mustacchi * need to special case zero rather than the maximum integer.
277*aaceae98SRobert Mustacchi */
278*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_one_uc(unsigned char uc)279*aaceae98SRobert Mustacchi stdc_first_leading_one_uc(unsigned char uc)
280*aaceae98SRobert Mustacchi {
281*aaceae98SRobert Mustacchi if (uc == 0) {
282*aaceae98SRobert Mustacchi return (0);
283*aaceae98SRobert Mustacchi }
284*aaceae98SRobert Mustacchi
285*aaceae98SRobert Mustacchi return (stdc_leading_zeros_uc(uc) + 1);
286*aaceae98SRobert Mustacchi }
287*aaceae98SRobert Mustacchi
288*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_one_us(unsigned short us)289*aaceae98SRobert Mustacchi stdc_first_leading_one_us(unsigned short us)
290*aaceae98SRobert Mustacchi {
291*aaceae98SRobert Mustacchi if (us == 0) {
292*aaceae98SRobert Mustacchi return (0);
293*aaceae98SRobert Mustacchi }
294*aaceae98SRobert Mustacchi
295*aaceae98SRobert Mustacchi return (stdc_leading_zeros_us(us) + 1);
296*aaceae98SRobert Mustacchi }
297*aaceae98SRobert Mustacchi
298*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_one_ui(unsigned int ui)299*aaceae98SRobert Mustacchi stdc_first_leading_one_ui(unsigned int ui)
300*aaceae98SRobert Mustacchi {
301*aaceae98SRobert Mustacchi if (ui == 0) {
302*aaceae98SRobert Mustacchi return (0);
303*aaceae98SRobert Mustacchi }
304*aaceae98SRobert Mustacchi
305*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ui(ui) + 1);
306*aaceae98SRobert Mustacchi }
307*aaceae98SRobert Mustacchi
308*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_one_ul(unsigned long ul)309*aaceae98SRobert Mustacchi stdc_first_leading_one_ul(unsigned long ul)
310*aaceae98SRobert Mustacchi {
311*aaceae98SRobert Mustacchi if (ul == 0) {
312*aaceae98SRobert Mustacchi return (0);
313*aaceae98SRobert Mustacchi }
314*aaceae98SRobert Mustacchi
315*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ul(ul) + 1);
316*aaceae98SRobert Mustacchi }
317*aaceae98SRobert Mustacchi
318*aaceae98SRobert Mustacchi unsigned int
stdc_first_leading_one_ull(unsigned long long ull)319*aaceae98SRobert Mustacchi stdc_first_leading_one_ull(unsigned long long ull)
320*aaceae98SRobert Mustacchi {
321*aaceae98SRobert Mustacchi if (ull == 0) {
322*aaceae98SRobert Mustacchi return (0);
323*aaceae98SRobert Mustacchi }
324*aaceae98SRobert Mustacchi
325*aaceae98SRobert Mustacchi return (stdc_leading_zeros_ull(ull) + 1);
326*aaceae98SRobert Mustacchi }
327*aaceae98SRobert Mustacchi
328*aaceae98SRobert Mustacchi /*
329*aaceae98SRobert Mustacchi * First Trailing Zero functions. These look for the least-significant zero. We
330*aaceae98SRobert Mustacchi * can do this in the same way we found the most-significant zero: count
331*aaceae98SRobert Mustacchi * trailing ones as that value + 1 is where the first trailing zero is. Again,
332*aaceae98SRobert Mustacchi * we need to avoid the maximum integer in each class.
333*aaceae98SRobert Mustacchi */
334*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_zero_uc(unsigned char uc)335*aaceae98SRobert Mustacchi stdc_first_trailing_zero_uc(unsigned char uc)
336*aaceae98SRobert Mustacchi {
337*aaceae98SRobert Mustacchi if (uc == UCHAR_MAX) {
338*aaceae98SRobert Mustacchi return (0);
339*aaceae98SRobert Mustacchi }
340*aaceae98SRobert Mustacchi
341*aaceae98SRobert Mustacchi return (stdc_trailing_ones_uc(uc) + 1);
342*aaceae98SRobert Mustacchi }
343*aaceae98SRobert Mustacchi
344*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_zero_us(unsigned short us)345*aaceae98SRobert Mustacchi stdc_first_trailing_zero_us(unsigned short us)
346*aaceae98SRobert Mustacchi {
347*aaceae98SRobert Mustacchi if (us == USHRT_MAX) {
348*aaceae98SRobert Mustacchi return (0);
349*aaceae98SRobert Mustacchi }
350*aaceae98SRobert Mustacchi
351*aaceae98SRobert Mustacchi return (stdc_trailing_ones_us(us) + 1);
352*aaceae98SRobert Mustacchi }
353*aaceae98SRobert Mustacchi
354*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_zero_ui(unsigned int ui)355*aaceae98SRobert Mustacchi stdc_first_trailing_zero_ui(unsigned int ui)
356*aaceae98SRobert Mustacchi {
357*aaceae98SRobert Mustacchi if (ui == UINT_MAX) {
358*aaceae98SRobert Mustacchi return (0);
359*aaceae98SRobert Mustacchi }
360*aaceae98SRobert Mustacchi
361*aaceae98SRobert Mustacchi return (stdc_trailing_ones_ui(ui) + 1);
362*aaceae98SRobert Mustacchi }
363*aaceae98SRobert Mustacchi
364*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_zero_ul(unsigned long ul)365*aaceae98SRobert Mustacchi stdc_first_trailing_zero_ul(unsigned long ul)
366*aaceae98SRobert Mustacchi {
367*aaceae98SRobert Mustacchi if (ul == ULONG_MAX) {
368*aaceae98SRobert Mustacchi return (0);
369*aaceae98SRobert Mustacchi }
370*aaceae98SRobert Mustacchi
371*aaceae98SRobert Mustacchi return (stdc_trailing_ones_ul(ul) + 1);
372*aaceae98SRobert Mustacchi }
373*aaceae98SRobert Mustacchi
374*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_zero_ull(unsigned long long ull)375*aaceae98SRobert Mustacchi stdc_first_trailing_zero_ull(unsigned long long ull)
376*aaceae98SRobert Mustacchi {
377*aaceae98SRobert Mustacchi if (ull == ULLONG_MAX) {
378*aaceae98SRobert Mustacchi return (0);
379*aaceae98SRobert Mustacchi }
380*aaceae98SRobert Mustacchi
381*aaceae98SRobert Mustacchi return (stdc_trailing_ones_ull(ull) + 1);
382*aaceae98SRobert Mustacchi }
383*aaceae98SRobert Mustacchi
384*aaceae98SRobert Mustacchi /*
385*aaceae98SRobert Mustacchi * First Trailing One functions. We do the same manipulation that we did with
386*aaceae98SRobert Mustacchi * trailing zeros. Again, here we need to special case zero values as there are
387*aaceae98SRobert Mustacchi * no ones there.
388*aaceae98SRobert Mustacchi */
389*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_one_uc(unsigned char uc)390*aaceae98SRobert Mustacchi stdc_first_trailing_one_uc(unsigned char uc)
391*aaceae98SRobert Mustacchi {
392*aaceae98SRobert Mustacchi if (uc == 0) {
393*aaceae98SRobert Mustacchi return (0);
394*aaceae98SRobert Mustacchi }
395*aaceae98SRobert Mustacchi
396*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_uc(uc) + 1);
397*aaceae98SRobert Mustacchi }
398*aaceae98SRobert Mustacchi
399*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_one_us(unsigned short us)400*aaceae98SRobert Mustacchi stdc_first_trailing_one_us(unsigned short us)
401*aaceae98SRobert Mustacchi {
402*aaceae98SRobert Mustacchi if (us == 0) {
403*aaceae98SRobert Mustacchi return (0);
404*aaceae98SRobert Mustacchi }
405*aaceae98SRobert Mustacchi
406*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_us(us) + 1);
407*aaceae98SRobert Mustacchi }
408*aaceae98SRobert Mustacchi
409*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_one_ui(unsigned int ui)410*aaceae98SRobert Mustacchi stdc_first_trailing_one_ui(unsigned int ui)
411*aaceae98SRobert Mustacchi {
412*aaceae98SRobert Mustacchi if (ui == 0) {
413*aaceae98SRobert Mustacchi return (0);
414*aaceae98SRobert Mustacchi }
415*aaceae98SRobert Mustacchi
416*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ui(ui) + 1);
417*aaceae98SRobert Mustacchi }
418*aaceae98SRobert Mustacchi
419*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_one_ul(unsigned long ul)420*aaceae98SRobert Mustacchi stdc_first_trailing_one_ul(unsigned long ul)
421*aaceae98SRobert Mustacchi {
422*aaceae98SRobert Mustacchi if (ul == 0) {
423*aaceae98SRobert Mustacchi return (0);
424*aaceae98SRobert Mustacchi }
425*aaceae98SRobert Mustacchi
426*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ul(ul) + 1);
427*aaceae98SRobert Mustacchi }
428*aaceae98SRobert Mustacchi
429*aaceae98SRobert Mustacchi unsigned int
stdc_first_trailing_one_ull(unsigned long long ull)430*aaceae98SRobert Mustacchi stdc_first_trailing_one_ull(unsigned long long ull)
431*aaceae98SRobert Mustacchi {
432*aaceae98SRobert Mustacchi if (ull == 0) {
433*aaceae98SRobert Mustacchi return (0);
434*aaceae98SRobert Mustacchi }
435*aaceae98SRobert Mustacchi
436*aaceae98SRobert Mustacchi return (stdc_trailing_zeros_ull(ull) + 1);
437*aaceae98SRobert Mustacchi }
438*aaceae98SRobert Mustacchi
439*aaceae98SRobert Mustacchi /*
440*aaceae98SRobert Mustacchi * Count Zeros and Count Ones functions. These can just defer to the popcnt
441*aaceae98SRobert Mustacchi * builtin. The Count Ones is simply the return value there. Count zeros is
442*aaceae98SRobert Mustacchi * going to be always our bit size minus the popcnt. We don't have to worry
443*aaceae98SRobert Mustacchi * about integer promotion here because promotion will only add 0s, not 1s for
444*aaceae98SRobert Mustacchi * unsigned values.
445*aaceae98SRobert Mustacchi */
446*aaceae98SRobert Mustacchi unsigned int
stdc_count_zeros_uc(unsigned char uc)447*aaceae98SRobert Mustacchi stdc_count_zeros_uc(unsigned char uc)
448*aaceae98SRobert Mustacchi {
449*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned char) - __builtin_popcount(uc));
450*aaceae98SRobert Mustacchi }
451*aaceae98SRobert Mustacchi
452*aaceae98SRobert Mustacchi unsigned int
stdc_count_zeros_us(unsigned short us)453*aaceae98SRobert Mustacchi stdc_count_zeros_us(unsigned short us)
454*aaceae98SRobert Mustacchi {
455*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned short) - __builtin_popcount(us));
456*aaceae98SRobert Mustacchi }
457*aaceae98SRobert Mustacchi
458*aaceae98SRobert Mustacchi unsigned int
stdc_count_zeros_ui(unsigned int ui)459*aaceae98SRobert Mustacchi stdc_count_zeros_ui(unsigned int ui)
460*aaceae98SRobert Mustacchi {
461*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned int) - __builtin_popcount(ui));
462*aaceae98SRobert Mustacchi }
463*aaceae98SRobert Mustacchi
464*aaceae98SRobert Mustacchi unsigned int
stdc_count_zeros_ul(unsigned long ul)465*aaceae98SRobert Mustacchi stdc_count_zeros_ul(unsigned long ul)
466*aaceae98SRobert Mustacchi {
467*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long) - __builtin_popcountl(ul));
468*aaceae98SRobert Mustacchi }
469*aaceae98SRobert Mustacchi
470*aaceae98SRobert Mustacchi unsigned int
stdc_count_zeros_ull(unsigned long long ull)471*aaceae98SRobert Mustacchi stdc_count_zeros_ull(unsigned long long ull)
472*aaceae98SRobert Mustacchi {
473*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long long) -
474*aaceae98SRobert Mustacchi __builtin_popcountll(ull));
475*aaceae98SRobert Mustacchi }
476*aaceae98SRobert Mustacchi
477*aaceae98SRobert Mustacchi unsigned int
stdc_count_ones_uc(unsigned char uc)478*aaceae98SRobert Mustacchi stdc_count_ones_uc(unsigned char uc)
479*aaceae98SRobert Mustacchi {
480*aaceae98SRobert Mustacchi return (__builtin_popcount(uc));
481*aaceae98SRobert Mustacchi }
482*aaceae98SRobert Mustacchi
483*aaceae98SRobert Mustacchi unsigned int
stdc_count_ones_us(unsigned short us)484*aaceae98SRobert Mustacchi stdc_count_ones_us(unsigned short us)
485*aaceae98SRobert Mustacchi {
486*aaceae98SRobert Mustacchi return (__builtin_popcount(us));
487*aaceae98SRobert Mustacchi }
488*aaceae98SRobert Mustacchi
489*aaceae98SRobert Mustacchi unsigned int
stdc_count_ones_ui(unsigned int ui)490*aaceae98SRobert Mustacchi stdc_count_ones_ui(unsigned int ui)
491*aaceae98SRobert Mustacchi {
492*aaceae98SRobert Mustacchi return (__builtin_popcount(ui));
493*aaceae98SRobert Mustacchi }
494*aaceae98SRobert Mustacchi
495*aaceae98SRobert Mustacchi unsigned int
stdc_count_ones_ul(unsigned long ul)496*aaceae98SRobert Mustacchi stdc_count_ones_ul(unsigned long ul)
497*aaceae98SRobert Mustacchi {
498*aaceae98SRobert Mustacchi return (__builtin_popcountl(ul));
499*aaceae98SRobert Mustacchi }
500*aaceae98SRobert Mustacchi
501*aaceae98SRobert Mustacchi unsigned int
stdc_count_ones_ull(unsigned long long ull)502*aaceae98SRobert Mustacchi stdc_count_ones_ull(unsigned long long ull)
503*aaceae98SRobert Mustacchi {
504*aaceae98SRobert Mustacchi return (__builtin_popcountll(ull));
505*aaceae98SRobert Mustacchi }
506*aaceae98SRobert Mustacchi
507*aaceae98SRobert Mustacchi /*
508*aaceae98SRobert Mustacchi * Single Bit Check functions. These are supposed to return true if they only
509*aaceae98SRobert Mustacchi * have a single 1 bit set. We simply implement this by calling the
510*aaceae98SRobert Mustacchi * corresponding count ones function and checking its return value. There is
511*aaceae98SRobert Mustacchi * probably a more clever algorithm out there.
512*aaceae98SRobert Mustacchi */
513*aaceae98SRobert Mustacchi bool
stdc_has_single_bit_uc(unsigned char uc)514*aaceae98SRobert Mustacchi stdc_has_single_bit_uc(unsigned char uc)
515*aaceae98SRobert Mustacchi {
516*aaceae98SRobert Mustacchi return (stdc_count_ones_uc(uc) == 1);
517*aaceae98SRobert Mustacchi }
518*aaceae98SRobert Mustacchi
519*aaceae98SRobert Mustacchi bool
stdc_has_single_bit_us(unsigned short us)520*aaceae98SRobert Mustacchi stdc_has_single_bit_us(unsigned short us)
521*aaceae98SRobert Mustacchi {
522*aaceae98SRobert Mustacchi return (stdc_count_ones_us(us) == 1);
523*aaceae98SRobert Mustacchi }
524*aaceae98SRobert Mustacchi
525*aaceae98SRobert Mustacchi bool
stdc_has_single_bit_ui(unsigned int ui)526*aaceae98SRobert Mustacchi stdc_has_single_bit_ui(unsigned int ui)
527*aaceae98SRobert Mustacchi {
528*aaceae98SRobert Mustacchi return (stdc_count_ones_ui(ui) == 1);
529*aaceae98SRobert Mustacchi }
530*aaceae98SRobert Mustacchi
531*aaceae98SRobert Mustacchi bool
stdc_has_single_bit_ul(unsigned long ul)532*aaceae98SRobert Mustacchi stdc_has_single_bit_ul(unsigned long ul)
533*aaceae98SRobert Mustacchi {
534*aaceae98SRobert Mustacchi return (stdc_count_ones_ul(ul) == 1);
535*aaceae98SRobert Mustacchi }
536*aaceae98SRobert Mustacchi
537*aaceae98SRobert Mustacchi bool
stdc_has_single_bit_ull(unsigned long long ull)538*aaceae98SRobert Mustacchi stdc_has_single_bit_ull(unsigned long long ull)
539*aaceae98SRobert Mustacchi {
540*aaceae98SRobert Mustacchi return (stdc_count_ones_ull(ull) == 1);
541*aaceae98SRobert Mustacchi }
542*aaceae98SRobert Mustacchi
543*aaceae98SRobert Mustacchi /*
544*aaceae98SRobert Mustacchi * Bit Width functions. This is asking us to calculate 1 + floor(log2(val)).
545*aaceae98SRobert Mustacchi * When we are taking the floor of this, then we can simply calculate this as
546*aaceae98SRobert Mustacchi * finding the first leading one. Because the first leading one logic uses the
547*aaceae98SRobert Mustacchi * standard's 'most-significant' index logic, we then have to subtract the
548*aaceae98SRobert Mustacchi * corresponding size.
549*aaceae98SRobert Mustacchi */
550*aaceae98SRobert Mustacchi unsigned int
stdc_bit_width_uc(unsigned char uc)551*aaceae98SRobert Mustacchi stdc_bit_width_uc(unsigned char uc)
552*aaceae98SRobert Mustacchi {
553*aaceae98SRobert Mustacchi if (uc == 0) {
554*aaceae98SRobert Mustacchi return (0);
555*aaceae98SRobert Mustacchi }
556*aaceae98SRobert Mustacchi
557*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned char) + 1 -
558*aaceae98SRobert Mustacchi stdc_first_leading_one_uc(uc));
559*aaceae98SRobert Mustacchi }
560*aaceae98SRobert Mustacchi
561*aaceae98SRobert Mustacchi unsigned int
stdc_bit_width_us(unsigned short us)562*aaceae98SRobert Mustacchi stdc_bit_width_us(unsigned short us)
563*aaceae98SRobert Mustacchi {
564*aaceae98SRobert Mustacchi if (us == 0) {
565*aaceae98SRobert Mustacchi return (0);
566*aaceae98SRobert Mustacchi }
567*aaceae98SRobert Mustacchi
568*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned short) + 1 -
569*aaceae98SRobert Mustacchi stdc_first_leading_one_us(us));
570*aaceae98SRobert Mustacchi }
571*aaceae98SRobert Mustacchi
572*aaceae98SRobert Mustacchi unsigned int
stdc_bit_width_ui(unsigned int ui)573*aaceae98SRobert Mustacchi stdc_bit_width_ui(unsigned int ui)
574*aaceae98SRobert Mustacchi {
575*aaceae98SRobert Mustacchi if (ui == 0) {
576*aaceae98SRobert Mustacchi return (0);
577*aaceae98SRobert Mustacchi }
578*aaceae98SRobert Mustacchi
579*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned int) + 1 -
580*aaceae98SRobert Mustacchi stdc_first_leading_one_ui(ui));
581*aaceae98SRobert Mustacchi }
582*aaceae98SRobert Mustacchi
583*aaceae98SRobert Mustacchi unsigned int
stdc_bit_width_ul(unsigned long ul)584*aaceae98SRobert Mustacchi stdc_bit_width_ul(unsigned long ul)
585*aaceae98SRobert Mustacchi {
586*aaceae98SRobert Mustacchi if (ul == 0) {
587*aaceae98SRobert Mustacchi return (0);
588*aaceae98SRobert Mustacchi }
589*aaceae98SRobert Mustacchi
590*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long) + 1 -
591*aaceae98SRobert Mustacchi stdc_first_leading_one_ul(ul));
592*aaceae98SRobert Mustacchi }
593*aaceae98SRobert Mustacchi
594*aaceae98SRobert Mustacchi unsigned int
stdc_bit_width_ull(unsigned long long ull)595*aaceae98SRobert Mustacchi stdc_bit_width_ull(unsigned long long ull)
596*aaceae98SRobert Mustacchi {
597*aaceae98SRobert Mustacchi if (ull == 0) {
598*aaceae98SRobert Mustacchi return (0);
599*aaceae98SRobert Mustacchi }
600*aaceae98SRobert Mustacchi
601*aaceae98SRobert Mustacchi return (CHAR_BIT * sizeof (unsigned long long) + 1 -
602*aaceae98SRobert Mustacchi stdc_first_leading_one_ull(ull));
603*aaceae98SRobert Mustacchi }
604*aaceae98SRobert Mustacchi
605*aaceae98SRobert Mustacchi /*
606*aaceae98SRobert Mustacchi * Bit Floor functions. These are trying to find the smallest power of two that
607*aaceae98SRobert Mustacchi * is not greater than the value specified. We can use the bit width, subtract
608*aaceae98SRobert Mustacchi * one, and then shift. This is defined by the spec such that a value of 0
609*aaceae98SRobert Mustacchi * returns 0.
610*aaceae98SRobert Mustacchi */
611*aaceae98SRobert Mustacchi unsigned char
stdc_bit_floor_uc(unsigned char uc)612*aaceae98SRobert Mustacchi stdc_bit_floor_uc(unsigned char uc)
613*aaceae98SRobert Mustacchi {
614*aaceae98SRobert Mustacchi if (uc == 0) {
615*aaceae98SRobert Mustacchi return (0);
616*aaceae98SRobert Mustacchi }
617*aaceae98SRobert Mustacchi
618*aaceae98SRobert Mustacchi return (1U << (stdc_bit_width_uc(uc) - 1));
619*aaceae98SRobert Mustacchi }
620*aaceae98SRobert Mustacchi
621*aaceae98SRobert Mustacchi unsigned short
stdc_bit_floor_us(unsigned short us)622*aaceae98SRobert Mustacchi stdc_bit_floor_us(unsigned short us)
623*aaceae98SRobert Mustacchi {
624*aaceae98SRobert Mustacchi if (us == 0) {
625*aaceae98SRobert Mustacchi return (0);
626*aaceae98SRobert Mustacchi }
627*aaceae98SRobert Mustacchi
628*aaceae98SRobert Mustacchi return (1U << (stdc_bit_width_us(us) - 1));
629*aaceae98SRobert Mustacchi }
630*aaceae98SRobert Mustacchi
631*aaceae98SRobert Mustacchi unsigned int
stdc_bit_floor_ui(unsigned int ui)632*aaceae98SRobert Mustacchi stdc_bit_floor_ui(unsigned int ui)
633*aaceae98SRobert Mustacchi {
634*aaceae98SRobert Mustacchi if (ui == 0) {
635*aaceae98SRobert Mustacchi return (0);
636*aaceae98SRobert Mustacchi }
637*aaceae98SRobert Mustacchi
638*aaceae98SRobert Mustacchi return (1U << (stdc_bit_width_ui(ui) - 1));
639*aaceae98SRobert Mustacchi }
640*aaceae98SRobert Mustacchi
641*aaceae98SRobert Mustacchi unsigned long
stdc_bit_floor_ul(unsigned long ul)642*aaceae98SRobert Mustacchi stdc_bit_floor_ul(unsigned long ul)
643*aaceae98SRobert Mustacchi {
644*aaceae98SRobert Mustacchi if (ul == 0) {
645*aaceae98SRobert Mustacchi return (0);
646*aaceae98SRobert Mustacchi }
647*aaceae98SRobert Mustacchi
648*aaceae98SRobert Mustacchi return (1UL << (stdc_bit_width_ul(ul) - 1));
649*aaceae98SRobert Mustacchi }
650*aaceae98SRobert Mustacchi
651*aaceae98SRobert Mustacchi unsigned long long
stdc_bit_floor_ull(unsigned long long ull)652*aaceae98SRobert Mustacchi stdc_bit_floor_ull(unsigned long long ull)
653*aaceae98SRobert Mustacchi {
654*aaceae98SRobert Mustacchi if (ull == 0) {
655*aaceae98SRobert Mustacchi return (0);
656*aaceae98SRobert Mustacchi }
657*aaceae98SRobert Mustacchi
658*aaceae98SRobert Mustacchi return (1ULL << (stdc_bit_width_ull(ull) - 1));
659*aaceae98SRobert Mustacchi }
660*aaceae98SRobert Mustacchi
661*aaceae98SRobert Mustacchi /*
662*aaceae98SRobert Mustacchi * Bit Ceiling functions. These are meant to return the next power of two that
663*aaceae98SRobert Mustacchi * is greater than the value. If the value cannot fit, then it is supposed to
664*aaceae98SRobert Mustacchi * return 0. Whenever we have a value greater than the signed maximum, then
665*aaceae98SRobert Mustacchi * we're going to end up having to return zero. We don't have explicit checks
666*aaceae98SRobert Mustacchi * for the value being representable because we're using shifts which by
667*aaceae98SRobert Mustacchi * definition will shift in zero values and then integer rules will cause the
668*aaceae98SRobert Mustacchi * value to be truncated.
669*aaceae98SRobert Mustacchi *
670*aaceae98SRobert Mustacchi * However, there is a slight challenge with this assumption. It is undefined
671*aaceae98SRobert Mustacchi * behavior to shift a value by more bits than its bit width. For example, the
672*aaceae98SRobert Mustacchi * bit width of the unsigned char 0xf0 is 8. 1 << 8 for an unsigned char is
673*aaceae98SRobert Mustacchi * undefined (while integer promotion rules do come into effect you can see this
674*aaceae98SRobert Mustacchi * at higher values). As a result, there are two ways to deal with this. We can
675*aaceae98SRobert Mustacchi * either make it so we never shift by the maximum value of bits by doing 2 <<
676*aaceae98SRobert Mustacchi * (width - 1), or we can use an if statement to explicitly check for these
677*aaceae98SRobert Mustacchi * values. For now, we do the former (reducing the branch predictor's burden),
678*aaceae98SRobert Mustacchi * which also means that instead of checking just for zero, we need to check for
679*aaceae98SRobert Mustacchi * 1 as well so we don't underflow the bit shift quantity!
680*aaceae98SRobert Mustacchi *
681*aaceae98SRobert Mustacchi * When a value is an exact power of two, then it by definition fits, so we
682*aaceae98SRobert Mustacchi * always subtract one from the input value to make sure we end up getting it to
683*aaceae98SRobert Mustacchi * fit. This results in us only needing to special case zero.
684*aaceae98SRobert Mustacchi */
685*aaceae98SRobert Mustacchi unsigned char
stdc_bit_ceil_uc(unsigned char uc)686*aaceae98SRobert Mustacchi stdc_bit_ceil_uc(unsigned char uc)
687*aaceae98SRobert Mustacchi {
688*aaceae98SRobert Mustacchi if (uc <= 1) {
689*aaceae98SRobert Mustacchi return (1);
690*aaceae98SRobert Mustacchi }
691*aaceae98SRobert Mustacchi
692*aaceae98SRobert Mustacchi return (2U << (stdc_bit_width_uc(uc - 1) - 1));
693*aaceae98SRobert Mustacchi }
694*aaceae98SRobert Mustacchi
695*aaceae98SRobert Mustacchi unsigned short
stdc_bit_ceil_us(unsigned short us)696*aaceae98SRobert Mustacchi stdc_bit_ceil_us(unsigned short us)
697*aaceae98SRobert Mustacchi {
698*aaceae98SRobert Mustacchi if (us <= 1) {
699*aaceae98SRobert Mustacchi return (1);
700*aaceae98SRobert Mustacchi }
701*aaceae98SRobert Mustacchi
702*aaceae98SRobert Mustacchi return (2U << (stdc_bit_width_us(us - 1) - 1));
703*aaceae98SRobert Mustacchi }
704*aaceae98SRobert Mustacchi
705*aaceae98SRobert Mustacchi unsigned int
stdc_bit_ceil_ui(unsigned int ui)706*aaceae98SRobert Mustacchi stdc_bit_ceil_ui(unsigned int ui)
707*aaceae98SRobert Mustacchi {
708*aaceae98SRobert Mustacchi if (ui <= 1) {
709*aaceae98SRobert Mustacchi return (1);
710*aaceae98SRobert Mustacchi }
711*aaceae98SRobert Mustacchi
712*aaceae98SRobert Mustacchi return (2U << (stdc_bit_width_ui(ui - 1) - 1));
713*aaceae98SRobert Mustacchi }
714*aaceae98SRobert Mustacchi
715*aaceae98SRobert Mustacchi unsigned long
stdc_bit_ceil_ul(unsigned long ul)716*aaceae98SRobert Mustacchi stdc_bit_ceil_ul(unsigned long ul)
717*aaceae98SRobert Mustacchi {
718*aaceae98SRobert Mustacchi if (ul <= 1) {
719*aaceae98SRobert Mustacchi return (1);
720*aaceae98SRobert Mustacchi }
721*aaceae98SRobert Mustacchi
722*aaceae98SRobert Mustacchi return (2UL << (stdc_bit_width_ul(ul - 1) - 1));
723*aaceae98SRobert Mustacchi }
724*aaceae98SRobert Mustacchi
725*aaceae98SRobert Mustacchi unsigned long long
stdc_bit_ceil_ull(unsigned long long ull)726*aaceae98SRobert Mustacchi stdc_bit_ceil_ull(unsigned long long ull)
727*aaceae98SRobert Mustacchi {
728*aaceae98SRobert Mustacchi if (ull <= 1) {
729*aaceae98SRobert Mustacchi return (1);
730*aaceae98SRobert Mustacchi }
731*aaceae98SRobert Mustacchi
732*aaceae98SRobert Mustacchi return (2ULL << (stdc_bit_width_ull(ull - 1) - 1));
733*aaceae98SRobert Mustacchi }
734