Skip to content

Commit

Permalink
Provide support for ctz
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBE committed May 22, 2024
1 parent 4d170fa commit a395186
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions XUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,17 @@ double sumPositiveValues(const double* array, size_t count) {
}
return sum;
}

#if !defined(HAVE_BUILTIN_CTZ)
// map a bit value mod 37 to its position
static const uint8_t mod37BitPosition[] = {
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4,
7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5,
20, 8, 19, 18
};

/* Returns the number of trailing zero bits */
unsigned int countTrailingZeros(unsigned int x) {
return mod37BitPosition[(-x & x) % 37];
}
#endif
9 changes: 9 additions & 0 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ int compareRealNumbers(double a, double b);
nonnegative. */
double sumPositiveValues(const double* array, size_t count);

/* Returns the number of trailing zero bits */
#if defined(HAVE_BUILTIN_CTZ)
static inline unsigned int countTrailingZeros(unsigned int x) {
return !x ? 32 : __builtin_ctz(x);
}
#else
unsigned int countTrailingZeros(unsigned int x);
#endif

/* IEC unit prefixes */
static const char unitPrefixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };

Expand Down
7 changes: 7 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ AC_LINK_IFELSE([
[AC_MSG_RESULT(no)
AC_MSG_ERROR([can not find required macros: NAN, isgreater() and isgreaterequal()])])

AC_MSG_CHECKING(for __builtin_ctz)
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([], [[__builtin_ctz(1); /* Supported in GCC 3.4 or later */]])],
[AC_DEFINE([HAVE_BUILTIN_CTZ], 1, [Define to 1 if the compiler supports '__builtin_ctz' function.])
AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))

# ----------------------------------------------------------------------


Expand Down

0 comments on commit a395186

Please sign in to comment.