Page 1 of 1
GCC optimization question...
Posted: Sat Jun 05, 2010 12:58 pm
by relminator
Hi, I was wondering of how GCC optimizes equations.
Say I have this:
Would the right-hand-side be evaluated into a single constant at compile time?
Thanks!!!
Re: GCC optimization question...
Posted: Sat Jun 05, 2010 1:40 pm
by vuurrobin
AFAIK, it should make it 1 constant, but I don't know how it will treat floats, seeing as the DS has no floating point unit.
asuming of course that you use optimisation level 2 (-O2), which should be standard in the makefile.
Re: GCC optimization question...
Posted: Sat Jun 05, 2010 2:21 pm
by relminator
So at -O2 even literal floats * literal ints would be converted to a literal constant?
Re: GCC optimization question...
Posted: Sat Jun 05, 2010 5:25 pm
by WinterMute
If the compiler can tell that an expression resolves to a compile time constant then it will emit that constant rather than the expression it's derived from when optimisation is enabled. It's fairly simple to see this using the -save-temps command and examining the emitted assembly.
Code: Select all
monalisa:test davem$ cat testconst.c
int testconst() {
int a = (0.3f) * (1<<12);
return a;
}
monalisa:test davem$ arm-eabi-gcc -c -O2 -march=armv5te -mtune=arm946e-s -mthumb -save-temps testconst.c
monalisa:test davem$ cat testconst.s
.arch armv5te
.fpu softvfp
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 1
.eabi_attribute 30, 2
.eabi_attribute 18, 4
.code 16
.file "testconst.c"
.text
.align 2
.global testconst
.code 16
.thumb_func
.type testconst, %function
testconst:
ldr r0, .L2
@ sp needed for prologue
bx lr
.L3:
.align 2
.L2:
.word 1228
.size testconst, .-testconst
Re: GCC optimization question...
Posted: Sun Jun 06, 2010 12:53 am
by relminator
Thanks dude!!