public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: LIU Hao <lh_mouse@126.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: optimizer discards sign information
Date: Wed, 10 Apr 2024 18:17:12 +0800	[thread overview]
Message-ID: <5f93b034b741790021d568995abe46ea8a164e6b.camel@xry111.site> (raw)
In-Reply-To: <ceabca26-1adc-41bf-b38e-d59927d296e4@126.com>

On Wed, 2024-04-10 at 18:07 +0800, LIU Hao wrote:
> 在 2024-04-10 17:52, Xi Ruoyao via Gcc-help 写道:
> > No, the OP is still thinking it's a wrong-code.
> 
> Would you read the Subject please?
> 
> 
>      u32 x = a * b;
>      u64 r = x;
>      return r;
> 
> This is same as
> 
>      u32 x = (int) a * (int) b;
>      u64 r = x;
>      return r;
> 
> and
> 
>      return (u64)(u32) ((int) a * (int) b);
> 
> 
> The code requests an `int` be zero-extended to a `u64` (if the result is written to EAX then this is 
> no-op), but GCC performs sign extension anyway. Do you still consider it not a bug?

It is a missed-optimization, but not wrong code.

The code does not requests an *arbitrary* int to be zero-extended to a
u64.  It requests a value which can be proven as non-negative to be
zero-extended.  Thus doing a sign-extension may be sub-optimal
(depending on the context and the target feature etc), but not wrong.

u32 x = (int) a * (int) b;

Here a and b are u16, so both (int) a and (int) b are non-negative.  And
since signed-overflow is UB, x is non-negative too.  And for a non-
negative int x, (u64)x is just same as (u64)(u32)x.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2024-04-10 10:17 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-10  8:52 stefan
2024-04-10  9:16 ` Alexander Monakov
2024-04-10  9:19   ` Xi Ruoyao
2024-04-10  9:40     ` LIU Hao
2024-04-10  9:44       ` Xi Ruoyao
2024-04-10  9:51         ` LIU Hao
2024-04-10  9:52           ` Xi Ruoyao
2024-04-10 10:07             ` LIU Hao
2024-04-10 10:17               ` Xi Ruoyao [this message]
2024-04-10 10:03           ` AW: " stefan
2024-04-10 10:34             ` Xi Ruoyao
2024-04-10  9:24   ` stefan
2024-04-10  9:49     ` stefan
2024-04-10  9:54       ` Xi Ruoyao
2024-04-10  9:57       ` LIU Hao
2024-04-10 10:03         ` Xi Ruoyao
2024-04-10 11:52     ` David Brown
2024-04-10 14:25       ` Stefan Franke
2024-04-10 16:51         ` David Brown
2024-04-11  0:32         ` Oleg Endo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5f93b034b741790021d568995abe46ea8a164e6b.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=gcc-help@gcc.gnu.org \
    --cc=lh_mouse@126.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).