Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libc/stdbit/stdc_first_leading_zero.c
96290 views
1
/*
2
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3
*
4
* SPDX-License-Identifier: BSD-2-Clause
5
*/
6
7
#include <limits.h>
8
#include <stdbit.h>
9
10
unsigned int
11
stdc_first_leading_zero_uc(unsigned char x)
12
{
13
const int offset = UINT_WIDTH - UCHAR_WIDTH;
14
15
if (x == UCHAR_MAX)
16
return (0);
17
18
return (__builtin_clz(~(unsigned int)x << offset) + 1);
19
}
20
21
unsigned int
22
stdc_first_leading_zero_us(unsigned short x)
23
{
24
const int offset = UINT_WIDTH - USHRT_WIDTH;
25
26
if (x == USHRT_MAX)
27
return (0);
28
29
return (__builtin_clz(~(unsigned int)x << offset) + 1);
30
}
31
32
unsigned int
33
stdc_first_leading_zero_ui(unsigned int x)
34
{
35
if (x == ~0U)
36
return (0);
37
38
return (__builtin_clz(~x) + 1);
39
}
40
41
unsigned int
42
stdc_first_leading_zero_ul(unsigned long x)
43
{
44
if (x == ~0UL)
45
return (0);
46
47
return (__builtin_clzl(~x) + 1);
48
}
49
50
unsigned int
51
stdc_first_leading_zero_ull(unsigned long long x)
52
{
53
if (x == ~0ULL)
54
return (0);
55
56
return (__builtin_clzll(~x) + 1);
57
}
58
59