aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdbit/stdc_count_zeros.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdbit/stdc_count_zeros.c')
-rw-r--r--lib/libc/stdbit/stdc_count_zeros.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/libc/stdbit/stdc_count_zeros.c b/lib/libc/stdbit/stdc_count_zeros.c
new file mode 100644
index 000000000000..35db965afcb5
--- /dev/null
+++ b/lib/libc/stdbit/stdc_count_zeros.c
@@ -0,0 +1,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));
+}