Testing for builtins/intrinsics
Asked Answered
S

4

10

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.

Sweltering answered 1/12, 2010 at 8:12 Comment(0)
S
4

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

Schertz answered 7/2, 2023 at 21:12 Comment(1)
Yes! I filed the bug report requesting this feature: gcc.gnu.org/bugzilla/show_bug.cgi?id=66970Sweltering
B
6

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.

Bruns answered 28/1, 2013 at 22:13 Comment(2)
Do you have any indication that this might be the case? For the time being your solution just blocks gcc from using the builtin. I'd guess that for at least 5 years after the feature would be introduce in gcc you'd still see versions around that wouldn't support it, some distributions are quite slow in adapting newer versions.Plural
The __has_builtin() macro has been mentioned in some threads of the GCC mailing list in January 2012 and this thread comments.gmane.org/gmane.comp.gcc.help/42610 is also promising. But I guess it is too late for GCC 4.8 so we will have to wait another year.Bruns
S
4

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

Schertz answered 7/2, 2023 at 21:12 Comment(1)
Yes! I filed the bug report requesting this feature: gcc.gnu.org/bugzilla/show_bug.cgi?id=66970Sweltering
P
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.

Plural answered 1/12, 2010 at 8:49 Comment(0)
P
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 ?

Photofinishing answered 1/12, 2010 at 8:22 Comment(1)
Not supported on a particular architecture, for example.Sweltering

© 2022 - 2024 — McMap. All rights reserved.