From: Xi Ruoyao <xry111@xry111.site>
To: stefan@franke.ms, gcc-help@gcc.gnu.org
Subject: Re: AW: optimizer discards sign information
Date: Wed, 10 Apr 2024 18:34:05 +0800 [thread overview]
Message-ID: <4b94a06692efdf1b758991b7d5bb9f90279f9f89.camel@xry111.site> (raw)
In-Reply-To: <01a301da8b2e$4805d830$d8118890$@franke.ms>
On Wed, 2024-04-10 at 12:03 +0200, stefan@franke.ms wrote:
> Yes, there is an overflow when the value gets assigned to x
>
> u32 x = *a * *b;
>
> And after that line of code, x is a valid unsigned int, no matter what
> value was assigned. And the compiler must not throw away that
> unsignedness.
If *a * *b does not overflow, x is a valid unsigned int. But if *a * *b
overflows, x is not valid, at all. Its type does not matter.
> Also an add can overflow:
>
> u64 faa(int a, int b) {
> u32 x = a + b;
> u64 r = x;
>
> And in this case the optimizer doesn't discard the variable x
Only an overflow on *arithmetic operation* is undefined behavior. An
overflow on *conversion* is not. In fact an "overflow on conversion" is
even not referred as "overflow" in the standard.
So in this case if a and b are both -1, x *must* be 0xfffffffdU, r
*must* be 0xfffffffdULL, and there's no undefined behavior. So it will
be incorrect (i.e. violating the standard, not "different from what a
person thinks") to use a signed extension here, and the compiler does
not do that.
But for
u16 a, b;
u32 x = (int)a * (int)b;
(int)a and (int)b must be non-negative, and since an overflow on
multiplication is UB, (int)a * (int)b must be non-negative too. So it's
valid (i.e. allowed by the standard, not "doing exactly what a person
thinks") to use a signed extension (though maybe it's not optimal, and
we may have a missed-optimization here).
--
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University
next prev parent reply other threads:[~2024-04-10 10:34 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
2024-04-10 10:03 ` AW: " stefan
2024-04-10 10:34 ` Xi Ruoyao [this message]
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=4b94a06692efdf1b758991b7d5bb9f90279f9f89.camel@xry111.site \
--to=xry111@xry111.site \
--cc=gcc-help@gcc.gnu.org \
--cc=stefan@franke.ms \
/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).