Rockbox mail archiveSubject: Re: arrays & pointers
Re: arrays & pointers
From: Greg Haerr <greg_at_censoft.com>
Date: Sun, 30 Jan 2005 16:04:33 -0700
> DIR* pdir = opendirs;
> for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
> if ( !pdir->busy )
Since we're on the subject of generated code efficiency, I just
had to jump in... A couple more comments:
1. With the pdir declaration above, since it's only
used in the single for() statement, it's initialization should be
delayed until within the first expression of the for statement.
In this way, the compiler may be able to optimize its use
to a register, and delay an unneeded store/load combo
if there's code inbetween the declaration and for statement
(which there is).
2. Although some think my next comment a matter of style,
too many compilers emit different code: dd++ vs ++dd.
If ++dd is coded, and the processor has an increment
memory opcode, this will always be emitted. If dd++
is coded, and the compiler's optimization is enabled,
this usually also happens, but otherwise a load/add 1/store
is emitted, which is slower and longer.
3. A lesser known evil: not declaring int as 'unsigned int'
when the variable is ever used in a shift, modulo, divide,
or pointer offset. For instance, there is a huge difference
in the code generated by gcc for the following seemingly
innocuous use of variable i as "int" vs "unsigned int":
a[i / 2]
If the var is not unsigned, gcc must sign extend and other
madness before emitting more code. When I contributed
the font code a few years back, I looked over all of it,
but haven't taken another look lately for this big byte-saving
Received on 2005-01-31