https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81015
Bug ID: 81015
Summary: Bad codegen for __builtin_clz(unsigned short)
Product: gcc
Version: 7.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: m101010a at gmail dot com
Target Milestone: ---
When compiling with "-O1 -mlzcnt", __builtin_clz applied to an unsigned short
generates the lzcntw instruction instead of lzcntl. Given the following source
code:
int main()
{
// volatile ensures the clz happens at runtime
volatile unsigned short bits_ = 1;
return __builtin_clz(bits_);
}
Compiling with no optimization produces a binary that correctly returns 31, as
does compiling it with gcc 6.3, but with gcc 7.1 and optimization the binary
returns 15 instead. This happens for both 64-bit and 32-bit code, for both C
and C++, and for -O1, -O2, and -O3.