aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdbit/stdc_bit_floor.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdbit/stdc_bit_floor.c')
-rw-r--r--lib/libc/stdbit/stdc_bit_floor.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/libc/stdbit/stdc_bit_floor.c b/lib/libc/stdbit/stdc_bit_floor.c
new file mode 100644
index 000000000000..0a491d6ec7b3
--- /dev/null
+++ b/lib/libc/stdbit/stdc_bit_floor.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <limits.h>
+#include <stdbit.h>
+
+unsigned char
+stdc_bit_floor_uc(unsigned char x)
+{
+ if (x == 0)
+ return (0);
+
+ return (1U << (UINT_WIDTH - __builtin_clz(x) - 1));
+}
+
+unsigned short
+stdc_bit_floor_us(unsigned short x)
+{
+ if (x == 0)
+ return (0);
+
+ return (1U << (UINT_WIDTH - __builtin_clz(x) - 1));
+}
+
+unsigned int
+stdc_bit_floor_ui(unsigned int x)
+{
+ if (x == 0)
+ return (0);
+
+ return (1U << (UINT_WIDTH - __builtin_clz(x) - 1));
+}
+
+unsigned long
+stdc_bit_floor_ul(unsigned long x)
+{
+ if (x == 0)
+ return (0);
+
+ return (1UL << (ULONG_WIDTH - __builtin_clzl(x) - 1));
+}
+
+unsigned long long
+stdc_bit_floor_ull(unsigned long long x)
+{
+ if (x == 0)
+ return (0);
+
+ return (1ULL << (ULLONG_WIDTH - __builtin_clzll(x) - 1));
+}