This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/48812] New: optimizing integer power of 2
- From: "castet.matthieu at free dot fr" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 28 Apr 2011 20:24:57 +0000
- Subject: [Bug c/48812] New: optimizing integer power of 2
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48812
Summary: optimizing integer power of 2
Product: gcc
Version: 4.4.5
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: castet.matthieu@free.fr
gcc correctly optimize
int divu(uint a, uint b)
{
return a / (1<<b);
}
to
divu:
mov r0, r0, lsr r1
mov pc, lr
but it fails to optimize
int divu3(uint a, uint b)
{
return a / ((1U<<b) / 4);
}
gcc generate
(arm-linux-gnueabi-gcc -Os p.c -march=armv4 -mno-thumb-interwork -S)
divu3:
stmfd sp!, {r3, lr}
mov r3, #1
mov r1, r3, asl r1
mov r1, r1, lsr #2
bl {{{__}}}aeabi_uidiv
ldmfd sp!, {r3, pc}
or
(gcc p.c -S -O3 -fomit-frame-pointer -mregparm=3)
divu3:
pushl %ebx
movl %edx, %ecx
movl $1, %ebx
xorl %edx, %edx
sall %cl, %ebx
shrl $2, %ebx
divl %ebx
popl %ebx
ret
but ((1U<<b) / 4) is 0 or a power of 2. Div by 0 is undefined in C ( C99
6.5.5p5)
So why can we generate :
mov r3, #1
mov r1, r3, asl r1
mov r1, r1, lsr #2
mov r0, r0, lsr r1
?
Note that gcc correctly optimize
int divu5(uint a, uint b)
{
return a / ((1U<<b) * 4);
}