summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@nginx.com>2023-09-05 02:58:10 +0200
committerAlejandro Colomar <alx@kernel.org>2023-12-19 15:02:42 +0100
commitf9761247794fce2de3726f3de9871a8ec24dcef3 (patch)
tree693297a7308820c3789dc9006785577cf3aaf5ad
parent4d84fe7e8e9453555e6e12f3c62bcf661fbdc9b4 (diff)
downloadunit-f9761247794fce2de3726f3de9871a8ec24dcef3.tar.gz
unit-f9761247794fce2de3726f3de9871a8ec24dcef3.tar.bz2
Libunit: added bit functions.
These are based on C23's <stdbit.h>. Signed-off-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--auto/make2
-rw-r--r--src/nxt_unit_bit.h64
2 files changed, 66 insertions, 0 deletions
diff --git a/auto/make b/auto/make
index 88c4fb2b..a2fd5df2 100644
--- a/auto/make
+++ b/auto/make
@@ -406,6 +406,7 @@ libunit-install: $NXT_BUILD_DIR/lib/$NXT_LIB_UNIT_STATIC
test -d \$(DESTDIR)$NXT_INCLUDEDIR \
|| install -d \$(DESTDIR)$NXT_INCLUDEDIR
install -p -m u=rw,go=r src/nxt_unit.h \
+ src/nxt_unit_bit.h \
src/nxt_unit_cdefs.h \
src/nxt_unit_field.h \
src/nxt_unit_request.h \
@@ -424,6 +425,7 @@ libunit-uninstall:
rm -f \$(DESTDIR)$NXT_PKGCONFIGDIR/unit.pc
@rmdir -p \$(DESTDIR)$NXT_PKGCONFIGDIR 2>/dev/null || true
rm -f \$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit.h \
+ \$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_bit.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_cdefs.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_field.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_request.h \
diff --git a/src/nxt_unit_bit.h b/src/nxt_unit_bit.h
new file mode 100644
index 00000000..6b43b7c0
--- /dev/null
+++ b/src/nxt_unit_bit.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2022-2023, Alejandro Colomar <alx@kernel.org>
+ * Copyright (C) NGINX, Inc.
+ */
+
+#ifndef _NXT_UNIT_BIT_H_INCLUDED_
+#define _NXT_UNIT_BIT_H_INCLUDED_
+
+
+#include <limits.h>
+
+
+/* C23 <stdbit.h> stuff */
+nxt_inline unsigned long nxt_bit_ceil_ul(unsigned long x);
+nxt_inline unsigned long nxt_bit_ceil_wrap_ul(unsigned long x);
+nxt_inline int nxt_bit_width_ul(unsigned long x);
+nxt_inline int nxt_leading_zeros_ul(unsigned long x);
+nxt_inline int nxt_trailing_zeros_ul(unsigned long x);
+
+
+nxt_inline unsigned long
+nxt_bit_ceil_ul(unsigned long x)
+{
+ return 1 + (ULONG_MAX >> nxt_leading_zeros_ul(x));
+}
+
+
+nxt_inline unsigned long
+nxt_bit_ceil_wrap_ul(unsigned long x)
+{
+ return (x == 0) ? 0 : nxt_bit_ceil_ul(x);
+}
+
+
+nxt_inline int
+nxt_bit_width_ul(unsigned long x)
+{
+ return (x == 0) ? 0 : nxt_trailing_zeros_ul(nxt_bit_ceil_ul(x));
+}
+
+
+nxt_inline int
+nxt_leading_zeros_ul(unsigned long x)
+{
+ if (x == 0) {
+ return sizeof(unsigned long) * CHAR_BIT;
+ }
+
+ return __builtin_clzl(x);
+}
+
+
+nxt_inline int
+nxt_trailing_zeros_ul(unsigned long x)
+{
+ if (x == 0) {
+ return sizeof(unsigned long) * CHAR_BIT;
+ }
+
+ return __builtin_ctzl(x);
+}
+
+
+#endif /* _NXT_UNIT_BIT_H_INCLUDED_ */