RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast

From: David Laight
Date: Mon May 22 2023 - 08:07:55 EST


From: 15330273260@xxxxxx <15330273260@xxxxxx>
> Sent: 22 May 2023 12:56
...
> > I'll bet most people will be surprised to see what this prints:
> >
> > #include <stdio.h>
> > #include <stdint.h>
> >
> > int main(void)
> > {
> > uint16_t x = 0xffff;
> > uint16_t y = 0xffff;
> > uint64_t z = x * y;
> >
> > printf("0x%016lx\n", z);
> > printf("%ld\n", z);
>
> Here, please replace the "%ld\n" with the "%lu\n", then you will see the
> difference.
>
> you are casting the variable 'z' to signed value,  "%d" is for printing
> signed value, and "%u" is for printing unsigned value.

That makes very little difference on 2's compliment systems.
They both display the contents of the variable.

> Your simple code explained exactly why you are still in confusion,
>
> that is u16 * u16  can yield a negative value if you use the int as the
> return type. Because it overflowed.

There is no 'return type', the type of 'u16 * u16' is signed int.
When 'signed int' is promoted/cast to u64 it is first sign extended
to 64 bits.

You can get what you want/expect by either forcing an unsigned multiply
or by explicitly casting the result of the multiply to u32.
So the product in 'z = (x + 0u) * y' is 'unsigned int' it gets
promoted to int64_t (ie a signed type) and then converted to
unsigned.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)