public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bob Plantz <plantz@cds1.net>
To: John Fine <johnsfine@verizon.net>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: unsigned int multiply, x86-64
Date: Tue, 15 Apr 2008 23:41:00 -0000	[thread overview]
Message-ID: <1208286294.6327.40.camel@localhost> (raw)
In-Reply-To: <4804E2EB.5090903@verizon.net>


On Tue, 2008-04-15 at 13:16 -0400, John Fine wrote:
> When x and y are both unsigned int (x*y) is also an unsigned int.
> (long int)(x*y) means first compute the unsigned in (x*y) then promote 
> it to long int.
> To get the answer you want, you need to promote one of the arguments of 
> the multiply before multiplying.  I don't know whether the optimizer 
> will figure out to do the 32 bit multiply you want and store the 64 bit 
> result or whether it would do a 64 bit multiply.

Thank you, John, for your remarks.

I knew about multiplying first, then the promotion, but it slipped my
mind. So I tried:
   unsigned int x;
   unsigned long int y;

   printf("Enter two integers: ");
   scanf("%u %lu", &x, &y);

   y = x * y;

In this case 32-bit * 64-bit -> 96-bit result. But the compiler does:
        movl    -4(%rbp), %eax     # load x
        mov     %eax, %edx             # promote to 64-bit (zeroes high
32 bits)
        movq    -16(%rbp), %rax   # load y (64-bit value)
        imulq   %rdx, %rax             # truncate high-order 32 bits of
result
        movq    %rax, -16(%rbp)

I've also tried using 64-bit for all the ints. Basically, if the result
is wider than the widest int in the multiplication, there is overflow.

My thought is that this is a place where assembly language is needed if
the high-order bits need to be preserved. At least one needs to check
the CF and OF. (They get set to one if there is signed multiply
overflow.)

I guess I need to look this up in the C standard. I suspect that it's
okay to ignore multiply overflow.

> Also, are you sure (long int) is 64 bit?  I thought it was just 32.

According to the ABI a long int in 32-bit mode is 32 bits, but in 64-bit
mode it is 64 bits. The code above verifies that a long int is 64 bits
in 64-bit mode.

I'm learning why converting to 64-bit is not as simple as I first
thought. :-)

-- Bob


  reply	other threads:[~2008-04-15 19:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-15 17:15 Bob Plantz
2008-04-15 17:17 ` Andrew Haley
2008-04-16  9:48   ` Bob Plantz
2008-04-15 19:05 ` John Fine
2008-04-15 23:41   ` Bob Plantz [this message]
2008-04-16 12:02     ` Andrew Haley

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=1208286294.6327.40.camel@localhost \
    --to=plantz@cds1.net \
    --cc=gcc-help@gcc.gnu.org \
    --cc=johnsfine@verizon.net \
    /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).