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

* [Bug target/60788] Miscompilation of __builtin_clz with -mlzcnt
  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 ` 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
  2 siblings, 0 replies; 4+ messages in thread
From: linux at carewolf dot com @ 2014-04-08 16:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Allan Jensen <linux at carewolf dot com> ---
Sorry. The optimization has nothing to do with it, it just causes the constant
expressions used for testing to be evaluated at compile time.

The real issue is that the lzcnt instruction does not return the number of
leading zeroes, but the bitwidth minus the number of leading zeroes. So the
mapping of _builtin_clz to lzcnt is simply naïve and incorrect.
>From gcc-bugs-return-448548-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue Apr 08 16:31:29 2014
Return-Path: <gcc-bugs-return-448548-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 7130 invoked by alias); 8 Apr 2014 16:31:29 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 7080 invoked by uid 48); 8 Apr 2014 16:31:26 -0000
From: "murali.marimekala at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/60459] Crash seen in _Unwind_VRS_Pop() for ARM platform
Date: Tue, 08 Apr 2014 16:31:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: target
X-Bugzilla-Version: 4.2.1
X-Bugzilla-Keywords:
X-Bugzilla-Severity: critical
X-Bugzilla-Who: murali.marimekala at gmail dot com
X-Bugzilla-Status: WAITING
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: cc
Message-ID: <bug-60459-4-0aVh2wjLzY@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-60459-4@http.gcc.gnu.org/bugzilla/>
References: <bug-60459-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-04/txt/msg00568.txt.bz2
Content-length: 1380

http://gcc.gnu.org/bugzilla/show_bug.cgi?id`459

Murali <murali.marimekala at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |murali.marimekala at gmail dot com

--- Comment #5 from Murali <murali.marimekala at gmail dot com> ---
Hi Ramana,

I am working with Raghu and looking into the same crash issue.

Thanks for your suggestions till now. Due to some constraints , we cant move to
a new toolchain. We are looking forward to fix this issue in ARM9 toolchain
4.2.1.

Though we got a fix for this issue online, we need your expertise to port it to
our toolchain.

When we were searching for inputs, we came across following article.
https://sourceware.org/ml/libc-ports/2011-03/msg00045.html

In this article,  Ryosei Takagi was facing the same issue (at exact location)
and has provided a fix for it in __longjump.S.

This particular __longjump.S is not available in toolchain 4.2.1. I searched
for relevant code and couldnt find it.

Since we are not much familiar with this toolchain code, its taking more time
than expected to understand and port the code. So, need your valuable advise to
find the relevant location to port this fix.

If you need additional information, please let me know.

Thank you,
Murali


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

* [Bug target/60788] Miscompilation of __builtin_clz with -mlzcnt
  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
  2 siblings, 0 replies; 4+ messages in thread
From: hjl.tools at gmail dot com @ 2014-04-08 16:59 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from H.J. Lu <hjl.tools at gmail dot com> ---
LZCNT is encoded as F3 BSR:

   0:    f3 0f bd c0              lzcnt  %eax,%eax
   4:    0f bd c0                 bsr    %eax,%eax

Since your processor doesn't support LZCNT, F3 prefix is ignored
and f3 0f bd c0 is treated as BSR.  Before you use LZCNT, you need
to check if your processor support it or not.


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

* [Bug target/60788] Miscompilation of __builtin_clz with -mlzcnt
  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
  2 siblings, 0 replies; 4+ messages in thread
From: linux at carewolf dot com @ 2014-04-08 17:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Allan Jensen <linux at carewolf dot com> ---
Sorry for the confusion. I thought Intel had added it from Ivy Bridge, but it
was Haswell.


^ 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).