public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Signed-unsigned comparison question
@ 2019-03-07 23:40 Zoltan Kocsi
  2019-03-07 23:53 ` Zoltan Kocsi
  0 siblings, 1 reply; 2+ messages in thread
From: Zoltan Kocsi @ 2019-03-07 23:40 UTC (permalink / raw)
  To: gcc

Gcc 8.2.0 (arm-none-eabi) throws a warning on the following construct:

uint32_t a;
uint16_t b;

if ( a > b ) ...

compaining that a signed integer is compared against an unsigned.
Of course, it is correct, as 'b' was promoted to int.

But shouldn't it be smart enough to know that (int) b is restricted to
the range of [0,65535] which it can safely compare against the range of
[0,0xffffffffu]?

Thanks,

Zoltan

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

* Re: Signed-unsigned comparison question
  2019-03-07 23:40 Signed-unsigned comparison question Zoltan Kocsi
@ 2019-03-07 23:53 ` Zoltan Kocsi
  0 siblings, 0 replies; 2+ messages in thread
From: Zoltan Kocsi @ 2019-03-07 23:53 UTC (permalink / raw)
  To: gcc

Correction:

The construct gcc complains about is not

if ( a < b ) ...

but

if ( a < b - ( b >> 2 ) ) ...

but still the same applies. The RHS of the > operator can never be
negative or have an overflow on 32 bits.

On Fri, 8 Mar 2019 10:40:06 +1100
Zoltan Kocsi <zoltan@bendor.com.au> wrote:

> Gcc 8.2.0 (arm-none-eabi) throws a warning on the following construct:
> 
> uint32_t a;
> uint16_t b;
> 
> if ( a > b ) ...
> 
> compaining that a signed integer is compared against an unsigned.
> Of course, it is correct, as 'b' was promoted to int.
> 
> But shouldn't it be smart enough to know that (int) b is restricted to
> the range of [0,65535] which it can safely compare against the range
> of [0,0xffffffffu]?
> 
> Thanks,
> 
> Zoltan

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

end of thread, other threads:[~2019-03-07 23:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-07 23:40 Signed-unsigned comparison question Zoltan Kocsi
2019-03-07 23:53 ` Zoltan Kocsi

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