http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60788

            Bug ID: 60788
           Summary: Miscompilation of __builtin_clz with -mlzcnt
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: linux at carewolf dot com

Created attachment 32567
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=32567&action=edit
Test case

If you compile the attached program with -O0 and -mlzcnt on x86, it will
produce wrong results. As long as optimization is greater than 0 or the lzcnt
instruction is not available, it will produce correct results.

Somehow log2 is returned for __builtin_clz instead of leading zero count. It
almost looks like a wrong optimization except it only happens without
optimizations.

$ /opt/gcc/bin/g++-4.9 lzcnt_tst.cpp -O1 -g -mlzcnt -o lzcnt_tst
$ ./lzcnt_tst 
nextPowerOfTwo(0) = 0
nextPowerOfTwo(1) = 1
nextPowerOfTwo(2) = 2
nextPowerOfTwo(17) = 32
$ /opt/gcc/bin/g++-4.9 lzcnt_tst.cpp -O0 -g -mlzcnt -o lzcnt_tst
$ ./lzcnt_tst 
nextPowerOfTwo(0) = 0
nextPowerOfTwo(1) = 1
nextPowerOfTwo(2) = 1
nextPowerOfTwo(17) = 268435456
$ /opt/gcc/bin/g++-4.9 lzcnt_tst.cpp -O0 -o lzcnt_tst
$ ./lzcnt_tst 
nextPowerOfTwo(0) = 0
nextPowerOfTwo(1) = 1
nextPowerOfTwo(2) = 2
nextPowerOfTwo(17) = 32

Reply via email to