[Builtins] Implement __builtin_clrsb to be compatible with gcc
authorCraig Topper <craig.topper@intel.com>
Wed, 8 Aug 2018 19:55:52 +0000 (19:55 +0000)
committerCraig Topper <craig.topper@intel.com>
Wed, 8 Aug 2018 19:55:52 +0000 (19:55 +0000)
commit0a4f6be4434c9c5d03d9e7ed46ed21af1fdd6a1a
tree0f9fe4abfb1b3259057c8bceb8ec9666783d6d8f
parent2a92a0efc7006c86da255041649412e429796e22
[Builtins] Implement __builtin_clrsb to be compatible with gcc

gcc defines an intrinsic called __builtin_clrsb which counts the number of extra sign bits on a number. This is equivalent to counting the number of leading zeros on a positive number or the number of leading ones on a negative number and subtracting one from the result. Since we can't count leading ones we need to invert negative numbers to count zeros.

This patch will cause the builtin to be expanded inline while gcc uses a call to a function like clrsbdi2 that is implemented in libgcc. But this is similar to what we already do for popcnt. And I don't think compiler-rt supports clrsbdi2.

Differential Revision: https://reviews.llvm.org/D50168

llvm-svn: 339282
clang/include/clang/Basic/Builtins.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/test/CodeGen/builtin_clrsb.c [new file with mode: 0644]