I have some code that uses gcc intrinsics. I would like to include code in case the intrinsic is missing. How can I do this?
#ifdef __builtin_ctzll
does not work.
I have some code that uses gcc intrinsics. I would like to include code in case the intrinsic is missing. How can I do this?
#ifdef __builtin_ctzll
does not work.
GCC now (for the last few years) supports __has_builtin
: It was introduced in GCC 10.1, May 7 2020. See the new features for that version here.
Also, thanks to @Charles comment, find the bug report here
With recent versions of clang it is now possible to check if builtin intrinsics exist using the __has_builtin()
macro e.g.
int popcount(int x)
{
#if __has_builtin(__builtin_popcount)
return __builtin_popcount(x);
#else
int count = 0;
for (; x != 0; x &= x - 1)
count++;
return count;
#endif
}
Let's hope GCC will also support __has_builtin()
in the future.
GCC now (for the last few years) supports __has_builtin
: It was introduced in GCC 10.1, May 7 2020. See the new features for that version here.
Also, thanks to @Charles comment, find the bug report here
The only thing that should work out of the box is to test the gcc version and hoping that this is consistently done on all architectures.
This is not guaranteed, though, I recently had a similar problem not with builtin functions but with __thread
for thread local storage. This is implemented on some architectures (linux) but not not on others (OS X, bsd?) and there was no way to find this out with a macro.
If you have gnu make you can do something similar to detect existence of a particular function in your Makefile:
__THREAD := $(shell echo '__thread int i;' | ${CC} ${CFLAGS} -xc -c -o /dev/null - 2> /dev/null || echo "NO")
ifeq (${__THREAD},NO)
${warning thread local storage (TLS) with '__thread' is not supported, switching to pthread_getkey}
CFLAGS += -D__GNUC_NO_TLS__
endif
This avoids to use more complex configuration utilities.
The #ifdef
directive checks whether __builtin_ctzll
is defined as a macro name, it won't help you determine if a __builtin_ctzll
function exists.
I'm not familiar enough with gcc builtins to help you more than this : how could the intrinsic be missing ?
© 2022 - 2024 — McMap. All rights reserved.