]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Detect ffsll presence in a target system
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 6 Oct 2022 22:33:17 +0000 (23:33 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 6 Oct 2022 22:33:17 +0000 (23:33 +0100)
CMakeLists.txt
config.h.in
src/libutil/printf.c

index 16f5c90151f0629e03106463651679dbe2048f94..571a09166990c219148ceb66cab3785b3efc80bd 100644 (file)
@@ -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)
index 6d1c03f371e400945968214e6f2651c550d72fe0..af4897140366c84816eb094ba68e0a37f3213406 100644 (file)
@@ -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
index 8eaab0c4021fc874cbe4ac86decc27451b20b98b..50d9d8b37518caa384fb4854c60c0d6592d98219 100644 (file)
@@ -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) {