All locals do indeed get ‘killed’ by the end of the function,
but it’s the register demand during the function that you need to worry about.
AVR chips only have 32x8-bit registers, and certain instructions only work with certain registers.
E.g. ‘add immediate to word’ (roughly
reg += constant, where reg is 16-bits wide) only works on 4 of the register pairs (8 of the 32 registers), ‘compare with immediate’ only works for the ‘upper’ 16 registers.
As a result of the constraints, it’s easy for the CPU to end up wasting cycles simply juggling registers around to free up a register that can do what it needs to do.
I think roughly it would be 1 extra branch, possibly 1 extra compare (depending on whether or not an ‘and’ sets the zero flag), definitely 2 extra 'and’s.
I’ve been thinking, if there were some way to avoid having to do this 8 step loop for each scanline you could probably save quite a bit of processing power.
Unfortunately I don’t fully understand what it’s doing,
so I can’t suggest anything yet.
(For one thing I’m still not clear on exactly what
(Which reminds me, why is the type
SPRITES, plural and in macro case, but the actual array is
sprite, singular? Surely the type should be singular because it describes a single entity and the array should be plural since an array contains multiple items?)
Come to think of it, I’m not sure that
% 8 is needed:
spriteReference.offset = (((lineY - spriteReference.y) % 8) * 4);
The prior condition:
if((lineY >= spriteReference.y) && (lineY <= spriteReference.y + 7))
Would imply that
(lineY - spriteReference.y) can only be between 0 and 7 (inclusive).
spriteReference.y + 7 overflows of course, but that shouldn’t be possible because of
(lineY >= spriteReference.y) and
That only saves you an
& 0x7, but at least it’s something.
There’s a potential buffer overrun bug with the current code…
for(uint8_t offset = 0; offset < 4; ++offset)
scanLine[spriteReference.x + offset] = (spriteIndex + 1);
spriteReference.x is 125 or more then
scanLine[spriteReference.x + offset] overruns the buffer.