mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "'15330273260@189.cn'" <15330273260@189.cn>,
	Jani Nikula <jani.nikula@linux.intel.com>,
	Li Yi <liyi@loongson.cn>
Cc: Thomas Zimmermann <tzimmermann@suse.de>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"loongson-kernel@lists.loongnix.cn" 
	<loongson-kernel@lists.loongnix.cn>
Subject: RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Date: Mon, 22 May 2023 12:07:23 +0000	[thread overview]
Message-ID: <f25545e96bc1458eb5bffc1505255e68@AcuMS.aculab.com> (raw)
In-Reply-To: <7f81c053-ba60-a4d2-23d6-d0f032acbcff@189.cn>

From: 15330273260@189.cn <15330273260@189.cn>
> 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)

  reply	other threads:[~2023-05-22 12:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 17:30 Sui Jingfeng
2023-05-17 10:59 ` David Laight
2023-05-17 18:08   ` Sui Jingfeng
2023-05-22 11:29     ` Jani Nikula
2023-05-22 11:55       ` Sui Jingfeng
2023-05-22 12:07         ` David Laight [this message]
2023-05-22 12:13         ` Jani Nikula
2023-05-22 12:53           ` Sui Jingfeng
2023-05-22 15:01             ` Jani Nikula
2023-05-23  3:55               ` Sui Jingfeng
2023-05-23  4:26       ` Sui Jingfeng
2023-05-23  8:12         ` Sui Jingfeng
2023-05-23  8:50         ` David Laight
2023-05-23  9:55           ` Sui Jingfeng
  -- strict thread matches above, loose matches on Subject: below --
2023-05-16 16:59 Sui Jingfeng
2023-05-16 17:06 ` Thomas Zimmermann
2023-05-16 17:13   ` Sui Jingfeng

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=f25545e96bc1458eb5bffc1505255e68@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=15330273260@189.cn \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liyi@loongson.cn \
    --cc=loongson-kernel@lists.loongnix.cn \
    --cc=tzimmermann@suse.de \
    /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

all inboxes | Powered by JetHome®