9

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.

4

3 に答える 3

5

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.

于 2013-01-28T22:13:12.317 に答える
3

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.

于 2010-12-01T08:49:24.027 に答える
2

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 ?

于 2010-12-01T08:22:39.273 に答える