public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/14224] New: GCC generates bad assembly for integer division
@ 2004-02-20 11:38 terpstra at ito dot tu-darmstadt dot de
2004-02-20 16:23 ` [Bug target/14224] GCC generates pessimizes code " pinskia at gcc dot gnu dot org
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: terpstra at ito dot tu-darmstadt dot de @ 2004-02-20 11:38 UTC (permalink / raw)
To: gcc-bugs
When dividing a 64 bit integer by a 32 bit integer (normal division or
remainder), gcc uses __umoddi3 even though the intel instruction 'div' can do
this natively.
On my machine, this bad assembly has a speed impact of at least 1.5* by simply
replacing the call with 'div' and no further optimizations applied.
These types of operations (multiplication and then modulus division) are very
common in cryptography and coding theory. It is quite possible that fixing this
bug would greatly improve gcc's performance in these areas.
I have confirmed that this bug applies to versions 3.5, 3.4, and 2.95.4 in
addition to the 3.3.3 against which it is filed.
The example program is:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int i = 0;
uint32_t x = atol(argv[1]), y = atol(argv[2]), z = 0;
for (i = 0; i < 1000000000; ++i)
{
z += ((uint64_t)x*(uint64_t)y) % 3221225473UL;
++x; ++y;
}
printf("%d\n", z);
return 0;
}
Compile with gcc -O2 foo.c -o foo (version 3.3.3).
Then generate the assembly gcc -O2 foo.c -o foo.s and apply this patch:
47,54c47,64
< movl %eax, (%esp)
< xorl %eax, %eax
< movl %edx, 4(%esp)
< movl $-1073741823, %edx
< movl %edx, 8(%esp)
< movl %eax, 12(%esp)
< call __umoddi3
< addl %eax, -16(%ebp)
---
>
> // stuff from gcc:
> // movl %eax, (%esp)
> // xorl %eax, %eax
> // movl %edx, 4(%esp)
> // movl $-1073741823, %edx
> // movl %edx, 8(%esp)
> // movl %eax, 12(%esp)
> // call __umoddi3
> // addl %eax, -16(%ebp)
>
> // my custom assembly:
> movl $-1073741823, %ecx
> div %ecx
> addl %edx, -16(%ebp)
>
> // --- end changes
>
Now compile the assembly (gcc foo.s -o foo2) and compare the results and the
speed difference. It is quite likely that gcc could do an even better job
through good pipelining and better register assignment. Furthermore, in this
particular example code, the divisor is a constant which might allow gcc to even
optimize away the div altogether. However, I have no idea what the performance
impact of this would be.
--
Summary: GCC generates bad assembly for integer division
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: terpstra at ito dot tu-darmstadt dot de
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug target/14224] GCC generates pessimizes code for integer division
2004-02-20 11:38 [Bug c/14224] New: GCC generates bad assembly " terpstra at ito dot tu-darmstadt dot de
@ 2004-02-20 16:23 ` pinskia at gcc dot gnu dot org
2004-04-05 13:31 ` uros at kss-loka dot si
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-02-20 16:23 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-02-20 16:23 -------
Confirmed.
--
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
Status|UNCONFIRMED |NEW
Component|c |target
Ever Confirmed| |1
Keywords| |pessimizes-code
Known to fail| |3.5.0
Last reconfirmed|0000-00-00 00:00:00 |2004-02-20 16:23:16
date| |
Summary|GCC generates bad assembly |GCC generates pessimizes
|for integer division |code for integer division
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug target/14224] GCC generates pessimizes code for integer division
2004-02-20 11:38 [Bug c/14224] New: GCC generates bad assembly " terpstra at ito dot tu-darmstadt dot de
2004-02-20 16:23 ` [Bug target/14224] GCC generates pessimizes code " pinskia at gcc dot gnu dot org
@ 2004-04-05 13:31 ` uros at kss-loka dot si
2004-04-05 14:58 ` terpstra at ito dot tu-darmstadt dot de
2004-06-03 3:56 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 6+ messages in thread
From: uros at kss-loka dot si @ 2004-04-05 13:31 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From uros at kss-loka dot si 2004-04-05 13:31 -------
Problem here is, that maximum quotient should always be 2^32-1, as it should fit
in 32bit EAX register. Consider the case, when dividend is 0x0000 0001 0000 0000
and divisor is 0x0000 0001. The result won't fit in 32 bits, and division will
produce #DE exception.
For your case, divisor is 0xC000 0001. And with your assembly, if x*y is equal
or more than 0xC000 0001 0000 0000 (= 13835058059577131008), #DE exception will
be generated.
I suggest to mark this bug as invalid, because it is not possible for gcc to
know maximum value of (x*y).
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug target/14224] GCC generates pessimizes code for integer division
2004-02-20 11:38 [Bug c/14224] New: GCC generates bad assembly " terpstra at ito dot tu-darmstadt dot de
2004-02-20 16:23 ` [Bug target/14224] GCC generates pessimizes code " pinskia at gcc dot gnu dot org
2004-04-05 13:31 ` uros at kss-loka dot si
@ 2004-04-05 14:58 ` terpstra at ito dot tu-darmstadt dot de
2004-06-03 3:56 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 6+ messages in thread
From: terpstra at ito dot tu-darmstadt dot de @ 2004-04-05 14:58 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From terpstra at ito dot tu-darmstadt dot de 2004-04-05 14:57 -------
(In reply to comment #2)
> Problem here is, that maximum quotient should always be 2^32-1, as it should fit
> in 32bit EAX register. Consider the case, when dividend is 0x0000 0001 0000 0000
> and divisor is 0x0000 0001. The result won't fit in 32 bits, and division will
> produce #DE exception.
>
> For your case, divisor is 0xC000 0001. And with your assembly, if x*y is equal
> or more than 0xC000 0001 0000 0000 (= 13835058059577131008), #DE exception will
> be generated.
You're quite right; I hadn't considered this.
> I suggest to mark this bug as invalid, because it is not possible for gcc to
> know maximum value of (x*y).
It's really a shame there's no way to inform gcc about the size.
For modulus arithmetic, x and y are known to be less than m.
Therefore, x*y%m and x*y/m must both fit in the registers.
However, I have a couple points to make still.
Firstly, the modulus will always fit inside EDX, regardless of the size of
the product. I'm not an assembler expert, but isn't it possible to turn off
the arithmetic exceptions prior to running DIV? As long as the code is only
interested in the modulus, this would be much faster.
At least in the case of finite mathematics (cryptography, coding theory,
etc) only the modulus really matters and computing it is often also the
critical section of the code.
Even if the code is interested in both the quotient and the modulus, it
seems that a quick comparison of the high 32bit part of EDX:EAX with the
modulus could determine whether or not a DIV would suffice. If it doesn't
then umoddi could be a fallback.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug target/14224] GCC generates pessimizes code for integer division
2004-02-20 11:38 [Bug c/14224] New: GCC generates bad assembly " terpstra at ito dot tu-darmstadt dot de
` (2 preceding siblings ...)
2004-04-05 14:58 ` terpstra at ito dot tu-darmstadt dot de
@ 2004-06-03 3:56 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-03 3:56 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-06-03 03:56 -------
*** Bug 14255 has been marked as a duplicate of this bug. ***
--
What |Removed |Added
----------------------------------------------------------------------------
CC| |bernie at develer dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-06-08 8:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <bug-14224-4@http.gcc.gnu.org/bugzilla/>
2021-06-08 8:41 ` [Bug target/14224] GCC generates pessimizes code for integer division pinskia at gcc dot gnu.org
2021-06-08 8:59 ` jakub at gcc dot gnu.org
2004-02-20 11:38 [Bug c/14224] New: GCC generates bad assembly " terpstra at ito dot tu-darmstadt dot de
2004-02-20 16:23 ` [Bug target/14224] GCC generates pessimizes code " pinskia at gcc dot gnu dot org
2004-04-05 13:31 ` uros at kss-loka dot si
2004-04-05 14:58 ` terpstra at ito dot tu-darmstadt dot de
2004-06-03 3:56 ` pinskia at gcc dot gnu dot org
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).