GCC optimization question...

support for the ARM toolchain
Post Reply
relminator
Posts: 84
Joined: Sun Apr 11, 2010 10:43 am

GCC optimization question...

Post by relminator » Sat Jun 05, 2010 12:58 pm

Hi, I was wondering of how GCC optimizes equations.

Say I have this:

Code: Select all

int a = (0.3f) * (1 << 12);
Would the right-hand-side be evaluated into a single constant at compile time?

Thanks!!!

User avatar
vuurrobin
Posts: 219
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: GCC optimization question...

Post by vuurrobin » Sat Jun 05, 2010 1:40 pm

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.

relminator
Posts: 84
Joined: Sun Apr 11, 2010 10:43 am

Re: GCC optimization question...

Post by relminator » Sat Jun 05, 2010 2:21 pm

So at -O2 even literal floats * literal ints would be converted to a literal constant?

WinterMute
Site Admin
Posts: 1986
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: GCC optimization question...

Post by WinterMute » Sat Jun 05, 2010 5:25 pm

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
Help keep devkitPro toolchains free, Donate today

Personal Blog

relminator
Posts: 84
Joined: Sun Apr 11, 2010 10:43 am

Re: GCC optimization question...

Post by relminator » Sun Jun 06, 2010 12:53 am

Thanks dude!!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest