Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for builtins/intrinsics

Tags:

c

gcc

intrinsics

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.

like image 507
Charles Avatar asked Dec 01 '10 08:12

Charles


1 Answers

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.

like image 165
Linoliumz Avatar answered Sep 28 '22 11:09

Linoliumz