summaryrefslogtreecommitdiff
path: root/lib/libc/stdbit/stdc_count_zeros.c
blob: 35db965afcb5b7e95910cfbab786baccdc1d31fc (plain)
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
/*
 * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <limits.h>
#include <stdbit.h>

unsigned int
stdc_count_zeros_uc(unsigned char x)
{
	return (__builtin_popcount(x ^ UCHAR_MAX));
}

unsigned int
stdc_count_zeros_us(unsigned short x)
{
	return (__builtin_popcount(x ^ USHRT_MAX));
}

unsigned int
stdc_count_zeros_ui(unsigned int x)
{
	return (__builtin_popcount(~x));
}

unsigned int
stdc_count_zeros_ul(unsigned long x)
{
	return (__builtin_popcountl(~x));
}

unsigned int
stdc_count_zeros_ull(unsigned long long x)
{
	return (__builtin_popcountll(~x));
}