Signed-unsigned comparison question

2019-03-07 Thread Zoltan Kocsi
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,0xu]?

Thanks,

Zoltan


Re: Signed-unsigned comparison question

2019-03-07 Thread Zoltan Kocsi
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  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,0xu]?
> 
> Thanks,
> 
> Zoltan