Skip to content

Commit a395186

Browse files
committed
Provide support for ctz
1 parent 4d170fa commit a395186

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

XUtils.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,17 @@ double sumPositiveValues(const double* array, size_t count) {
371371
}
372372
return sum;
373373
}
374+
375+
#if !defined(HAVE_BUILTIN_CTZ)
376+
// map a bit value mod 37 to its position
377+
static const uint8_t mod37BitPosition[] = {
378+
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4,
379+
7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5,
380+
20, 8, 19, 18
381+
};
382+
383+
/* Returns the number of trailing zero bits */
384+
unsigned int countTrailingZeros(unsigned int x) {
385+
return mod37BitPosition[(-x & x) % 37];
386+
}
387+
#endif

XUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ int compareRealNumbers(double a, double b);
131131
nonnegative. */
132132
double sumPositiveValues(const double* array, size_t count);
133133

134+
/* Returns the number of trailing zero bits */
135+
#if defined(HAVE_BUILTIN_CTZ)
136+
static inline unsigned int countTrailingZeros(unsigned int x) {
137+
return !x ? 32 : __builtin_ctz(x);
138+
}
139+
#else
140+
unsigned int countTrailingZeros(unsigned int x);
141+
#endif
142+
134143
/* IEC unit prefixes */
135144
static const char unitPrefixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };
136145

configure.ac

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ AC_LINK_IFELSE([
273273
[AC_MSG_RESULT(no)
274274
AC_MSG_ERROR([can not find required macros: NAN, isgreater() and isgreaterequal()])])
275275

276+
AC_MSG_CHECKING(for __builtin_ctz)
277+
AC_COMPILE_IFELSE([
278+
AC_LANG_PROGRAM([], [[__builtin_ctz(1); /* Supported in GCC 3.4 or later */]])],
279+
[AC_DEFINE([HAVE_BUILTIN_CTZ], 1, [Define to 1 if the compiler supports '__builtin_ctz' function.])
280+
AC_MSG_RESULT(yes)],
281+
AC_MSG_RESULT(no))
282+
276283
# ----------------------------------------------------------------------
277284

278285

0 commit comments

Comments
 (0)