Skip to content

Commit

Permalink
Merge bitcoin#19403: build: improve __builtin_clz* detection
Browse files Browse the repository at this point in the history
9952242 build: improve builtin_clz* detection (fanquake)

Pull request description:

  Fixes bitcoin#19402.

  The way we currently test for `__builtin_clz*` support with `AC_CHECK_DECLS` does not work with Clang:
  ```bash
  configure:21492: clang++-10 -std=c++11 -c -g -O2  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS conftest.cpp >&5
  conftest.cpp:100:10: error: builtin functions must be directly called
    (void) __builtin_clz;
           ^
  1 error generated.
  ```

  This also removes the `__builtin_clz()` check, as we don't actually use it anywhere, and it's trvial to re-add detection if we do start using it at some point. If this is controversial then I'll add a test for it as well.

ACKs for top commit:
  sipa:
    ACK 9952242
  laanwj:
    ACK 9952242

Tree-SHA512: 695abb1a694a01a25aaa483b4fffa7d598842f2ba4fe8630fbed9ce5450b915c33bf34bb16ad16a16b702dd7c91ebf49fe509a2498b9e28254fe0ec5177bbac0
  • Loading branch information
laanwj authored and PastaPastaPasta committed Jun 27, 2021
1 parent 7f9b627 commit bebd4fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
12 changes: 0 additions & 12 deletions build_msvc/bitcoin_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,6 @@
don't. */
#define HAVE_DECL_STRNLEN 1

/* Define to 1 if you have the declaration of `__builtin_clz', and to 0 if you
don't. */
//#define HAVE_DECL___BUILTIN_CLZ 1

/* Define to 1 if you have the declaration of `__builtin_clzl', and to 0 if
you don't. */
//#define HAVE_DECL___BUILTIN_CLZL 1

/* Define to 1 if you have the declaration of `__builtin_clzll', and to 0 if
you don't. */
//#define HAVE_DECL___BUILTIN_CLZLL 1

/* Define to 1 if you have the <dlfcn.h> header file. */
/* #undef HAVE_DLFCN_H */

Expand Down
16 changes: 15 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,21 @@ AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
#include <byteswap.h>
#endif])

AC_CHECK_DECLS([__builtin_clz, __builtin_clzl, __builtin_clzll])
AC_MSG_CHECKING(for __builtin_clzl)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
(void) __builtin_clzl(0);
]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_BUILTIN_CLZL, 1, [Define this symbol if you have __builtin_clzl])],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING(for __builtin_clzll)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
(void) __builtin_clzll(0);
]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_BUILTIN_CLZLL, 1, [Define this symbol if you have __builtin_clzll])],
[ AC_MSG_RESULT(no)]
)

dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas)
AC_MSG_CHECKING(for mallopt M_ARENA_MAX)
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
uint64_t static inline CountBits(uint64_t x)
{
#if HAVE_DECL___BUILTIN_CLZL
#if HAVE_BUILTIN_CLZL
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
#endif
#if HAVE_DECL___BUILTIN_CLZLL
#if HAVE_BUILTIN_CLZLL
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
}
Expand Down

0 comments on commit bebd4fd

Please sign in to comment.