__builtin_clz

1,109 views
Skip to first unread message

Mike Reed

unread,
Apr 13, 2011, 3:28:56 PM4/13/11
to chromium-dev
Skia does this check today

#if (defined(__arm__) && !defined(__thumb__))
#define SkCLZ(x) __builtin_clz(x)
#endif

If it fails, we fall back on a C implementation.

Question: what is the safe way to detect that I can use __builtin_clz
on x86? Is it the same check on linux, mac, windows? I see that
windows has _BitScanReverse.

I've found some logic in tuklib_integer.h, but I don't know if I
should crib that, or look somewhere else for a canonical answer.

thanks,
mike

Nico Weber

unread,
Apr 13, 2011, 5:31:29 PM4/13/11
to re...@google.com, chromium-dev
builtins are something provided by the compiler, so you need to do
check for the compiler. clang for example happens to have a
__has_builtin pseudomacro for this, while GCC seems to just always
support __builtin_clz. So for OS X and GCC, you can do

#if (__GNUC__ && !__clang__)|| __has_builtin(__builtin_clz)
   #define SkCLZ(x)    __builtin_clz(x)
#else
...

(In practice, clang seems to always return the __builtin_clz built-in,
so you can probably just do |#if __GNUC__|.)

On MSVC, you'd use _BitScanReverse instead.

tuklib_integer.h looks too general, because we always use gccs newer
than 3.4 and don't support compilers other than MSVC and gcc/clang.

Nico

> --
> Chromium Developers mailing list: chromi...@chromium.org
> View archives, change email options, or unsubscribe:
>    http://groups.google.com/a/chromium.org/group/chromium-dev
>

Evan Martin

unread,
Apr 14, 2011, 4:31:40 PM4/14/11
to tha...@chromium.org, re...@google.com, chromium-dev
I saw some code in Clang that uses has_feature like this, perhaps
has_builtin should work the same way:

#ifdef __has_feature
# if __has_feature(blocks)
...

Reply all
Reply to author
Forward
0 new messages