public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* GCC has problems with 64-bit multiplication
@ 2007-02-08 14:03 Hans Petter Selasky
  2007-02-08 15:03 ` Graham Stott
  0 siblings, 1 reply; 4+ messages in thread
From: Hans Petter Selasky @ 2007-02-08 14:03 UTC (permalink / raw)
  To: gcc-bugs

Test program:

#include <stdio.h>
#include <sys/types.h>      

int main() {

        int32_t a = 0x40000000;
        int16_t b = 0x4000;
        int64_t c = a * b;

        printf("0x%016llx\n", c);

        return 0;
}

%cc test.c
%./a.out
0x0000000000000000

%gcc --version
gcc (GCC) 3.4.6 [FreeBSD] 20060305
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Generated assembly code:

 8048514:       c7 45 fc 00 00 00 40    movl   $0x40000000,0xfffffffc(%ebp)
 804851b:       66 c7 45 fa 00 40       movw   $0x4000,0xfffffffa(%ebp)
 8048521:       0f bf 45 fa             movswl 0xfffffffa(%ebp),%eax
 8048525:       0f af 45 fc             imul   0xfffffffc(%ebp),%eax
XXX this instruction is probably the bug:
 8048529:       99                      cltd   
 804852a:       89 45 f0                mov    %eax,0xfffffff0(%ebp)
 804852d:       89 55 f4                mov    %edx,0xfffffff4(%ebp)
 8048530:       83 ec 04                sub    $0x4,%esp
 8048533:       ff 75 f4                pushl  0xfffffff4(%ebp)
 8048536:       ff 75 f0                pushl  0xfffffff0(%ebp)
 8048539:       68 cb 85 04 08          push   $0x80485cb
 804853e:       e8 49 fe ff ff          call   804838c <_init+0x24>
 8048543:       83 c4 10                add    $0x10,%esp

--HPS


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC has problems with 64-bit multiplication
  2007-02-08 14:03 GCC has problems with 64-bit multiplication Hans Petter Selasky
@ 2007-02-08 15:03 ` Graham Stott
  2007-02-08 16:21   ` Hans Petter Selasky
  2007-02-26 14:35   ` Hans Petter Selasky
  0 siblings, 2 replies; 4+ messages in thread
From: Graham Stott @ 2007-02-08 15:03 UTC (permalink / raw)
  To: Hans Petter Selasky, gcc-bugs

All,

Not a bug in GCC the result is correct as you've only asked for a 32-bit
multiply.

--- Hans Petter Selasky <hselasky@c2i.net> wrote:

> Test program:
> 
> #include <stdio.h>
> #include <sys/types.h>      
> 
> int main() {
> 
>         int32_t a = 0x40000000;
>         int16_t b = 0x4000;
>         int64_t c = a * b;
                      ^^^^^ this is a 32-bit multiply with the result widened
to 64-bit,
>         printf("0x%016llx\n", c);
> 
>         return 0;
> }
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC has problems with 64-bit multiplication
  2007-02-08 15:03 ` Graham Stott
@ 2007-02-08 16:21   ` Hans Petter Selasky
  2007-02-26 14:35   ` Hans Petter Selasky
  1 sibling, 0 replies; 4+ messages in thread
From: Hans Petter Selasky @ 2007-02-08 16:21 UTC (permalink / raw)
  To: Graham Stott; +Cc: gcc-bugs

On Thursday 08 February 2007 16:02, Graham Stott wrote:
> All,
>
> Not a bug in GCC the result is correct as you've only asked for a 32-bit
> multiply.

Ok, thanks. But really I don't want to do "((int64_t)a) * b", which I know 
works, hence that is very much slower than "a * b" on intel processors, hence 
the "mull" instruction will be used instead of "imul".

I found the following option by googling:
-mwide-multiply multiplies of 32 bits are 64

Is there an __attribute__() that I can use that will enable this for one 
multiplication, and not all ? Or something similar ?

>
> --- Hans Petter Selasky <hselasky@c2i.net> wrote:
> > Test program:
> >
> > #include <stdio.h>
> > #include <sys/types.h>
> >
> > int main() {
> >
> >         int32_t a = 0x40000000;
> >         int16_t b = 0x4000;
> >         int64_t c = a * b;
>
>                       ^^^^^ this is a 32-bit multiply with the result
> widened to 64-bit,
>
> >         printf("0x%016llx\n", c);
> >
> >         return 0;
> > }

--HPS


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC has problems with 64-bit multiplication
  2007-02-08 15:03 ` Graham Stott
  2007-02-08 16:21   ` Hans Petter Selasky
@ 2007-02-26 14:35   ` Hans Petter Selasky
  1 sibling, 0 replies; 4+ messages in thread
From: Hans Petter Selasky @ 2007-02-26 14:35 UTC (permalink / raw)
  To: Graham Stott; +Cc: gcc-bugs

On Thursday 08 February 2007 16:02, Graham Stott wrote:
> All,
>
> Not a bug in GCC the result is correct as you've only asked for a 32-bit
> multiply.

Hi again,

The problem was not that "mull" is used, but that "gcc 3.4.6" generates highly 
un-optimized code when I for example multiply a 16-bit integer by a 64-bit 
integer.

It actually generates code where three multiplications are used instead of 
two. That has a speed impact.

Code:

#include <stdio.h>
#include <sys/types.h>

int main() {

        int64_t temp;
        int16_t x;

        temp *= x;

        printf("%lld\n", temp);

        return 0;
}

objdump -D ./a.out

 8048507:       89 4d e8                mov    %ecx,0xffffffe8(%ebp)
 804850a:       31 c0                   xor    %eax,%eax
 804850c:       89 55 ec                mov    %edx,0xffffffec(%ebp)
 804850f:       f7 65 e8                mull   0xffffffe8(%ebp)
 8048512:       6b 7d ec 00             imul   $0x0,0xffffffec(%ebp),%edi
 8048516:       89 d6                   mov    %edx,%esi
 8048518:       89 c1                   mov    %eax,%ecx
 804851a:       01 fe                   add    %edi,%esi
 804851c:       6b 45 e8 00             imul   $0x0,0xffffffe8(%ebp),%eax
 8048520:       83 e4 f0                and    $0xfffffff0,%esp
 8048523:       83 ec 14                sub    $0x14,%esp
 8048526:       8d 1c 06                lea    (%esi,%eax,1),%ebx

--HPS


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-02-26 14:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-08 14:03 GCC has problems with 64-bit multiplication Hans Petter Selasky
2007-02-08 15:03 ` Graham Stott
2007-02-08 16:21   ` Hans Petter Selasky
2007-02-26 14:35   ` Hans Petter Selasky

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).