public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/60788] New: Miscompilation of __builtin_clz with -mlzcnt
@ 2014-04-08 16:18 linux at carewolf dot com
  2014-04-08 16:27 ` [Bug target/60788] " linux at carewolf dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: linux at carewolf dot com @ 2014-04-08 16:18 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-04-08 17:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-08 16:18 [Bug target/60788] New: Miscompilation of __builtin_clz with -mlzcnt linux at carewolf dot com
2014-04-08 16:27 ` [Bug target/60788] " linux at carewolf dot com
2014-04-08 16:59 ` hjl.tools at gmail dot com
2014-04-08 17:52 ` linux at carewolf dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).