commit a30002b: [Minor] Detect ffsll presence in a target system

Vsevolod Stakhov vsevolod at rspamd.com
Thu Oct 6 22:35:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-10-06 23:33:17 +0100
URL: https://github.com/rspamd/rspamd/commit/a30002b1c83196a621aa6d6bd24b91d39f312ca2 (HEAD -> master)

[Minor] Detect ffsll presence in a target system

---
 CMakeLists.txt       |  1 +
 config.h.in          |  1 +
 src/libutil/printf.c | 32 +++++++++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 16f5c9015..571a09166 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -361,6 +361,7 @@ CHECK_SYMBOL_EXISTS(open_memstream "stdio.h" HAVE_OPENMEMSTREAM)
 CHECK_SYMBOL_EXISTS(fmemopen "stdio.h" HAVE_FMEMOPEN)
 CHECK_SYMBOL_EXISTS(clock_getcpuclockid "sys/types.h;time.h" HAVE_CLOCK_GETCPUCLOCKID)
 CHECK_SYMBOL_EXISTS(RUSAGE_SELF "sys/types.h;sys/resource.h" HAVE_RUSAGE_SELF)
+CHECK_SYMBOL_EXISTS(ffsll "strings.h" HAVE_FFSLL)
 
 IF(ENABLE_PCRE2 MATCHES "ON")
 	IF(HAVE_PCRE_JIT)
diff --git a/config.h.in b/config.h.in
index 6d1c03f37..af4897140 100644
--- a/config.h.in
+++ b/config.h.in
@@ -25,6 +25,7 @@
 #cmakedefine HAVE_FCNTL_H        1
 #cmakedefine HAVE_FETCH_H        1
 #cmakedefine HAVE_FIPS_MODE      1
+#cmakedefine HAVE_FFSLL          1
 #cmakedefine HAVE_FLOCK          1
 #cmakedefine HAVE_FPATHCONF      1
 #cmakedefine HAVE_GETPAGESIZE    1
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index 8eaab0c40..50d9d8b37 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -349,6 +349,36 @@ rspamd_uint64_print (guint64 in, gchar *out)
 	return ndigits;
 }
 
+static inline int
+rspamd_ffsll(long long n)
+{
+#ifdef __has_builtin
+# if __has_builtin(__builtin_ffsll)
+	return __builtin_ffsll(n);
+# elif __has_builtin(__builtin_ctzll)
+	if (n == 0) {
+		return 0;
+	}
+
+	return __builtin_ctzll(n) + 1;
+# endif
+#endif /* __has_builtin */
+
+#ifdef HAVE_FFSL
+	return ffsl(n);
+#else
+	if (n == 0) {
+		return 0;
+	}
+
+	int bit;
+	for (bit = 1; !(n & 1); bit++) {
+		n = ((unsigned long long) n) >> 1;
+	}
+	return bit;
+#endif
+}
+
 static gchar *
 rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
 					  guint hexadecimal, guint binary, guint width)
@@ -383,7 +413,7 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
 		len = (temp + sizeof (temp)) - p;
 	}
 	else if (binary > 0) {
-		int first_bit = MIN(sizeof(temp), ffsll(ui64));
+		int first_bit = MIN(sizeof(temp), rspamd_ffsll(ui64));
 
 		p = temp + sizeof(temp);
 		for (int i = 0; i <= first_bit; i ++, ui64 >>= 1) {


More information about the Commits mailing list