From: Bob Plantz <plantz@cds1.net>
To: gcc-help <gcc-help@gcc.gnu.org>
Subject: unsigned int multiply, x86-64
Date: Tue, 15 Apr 2008 17:15:00 -0000 [thread overview]
Message-ID: <1208279125.6327.7.camel@localhost> (raw)
I wrote some C code to multiply two unsigned (32-bit) ints and store the
result in a 64-bit unsigned int.
int main(void)
{
unsigned int x, y;
unsigned long int z;
printf("Enter two integers: ");
scanf("%u %u", &x, &y);
z = (long int)(x * y);
printf("%u * %u = %lu\n", x , y, z);
exit(0);
}
For the multiply, gcc produces (with -O0):
movl -4(%rbp), %edx
movl -8(%rbp), %eax
imull %edx, %eax
mov %eax, %eax
movq %rax, -16(%rbp)
First, it uses signed multiply (imull). Second, it only keeps the
low-order 32 bits of the result.
I would do something like:
movl -4(%rbp), %edx
movl -8(%rbp), %eax # zeros high-order 32 bits
mull %edx # 64-bit result in edx:eax
shlq $32, %rdx # shift to high-order
addq %rdx, %rax # combine into 64-bit result
movq %rax, -16(%rbp)
Am I missing something here?
My "application" is that I'm writing a book and want to make sure I have
a clear understanding so I don't say stupid things.
-- Bob
next reply other threads:[~2008-04-15 17:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-15 17:15 Bob Plantz [this message]
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
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=1208279125.6327.7.camel@localhost \
--to=plantz@cds1.net \
--cc=gcc-help@gcc.gnu.org \
/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).