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