#20802 - MumblyJoe - Tue May 18, 2004 3:12 am
Some of you may have seen the argument I have been having over the efficiency differences between using refrences and #defines in C++. While messing with alot of compiled asm to see who was right I thought about the fact that I haven't done any self modifying code since the 486.
Has anyone done any self modifying code on the gba (obviously in ewram/iwram)? I think it could open a couple of improvements on my code if anyone could tell me the pitfalls they have run into etc.
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!
#20804 - Miked0801 - Tue May 18, 2004 3:53 am
Self modifying code is much stronger on the x86 with its caching ability (minor), its reduced register space (meaning less room for work), and its really expensive jumps (cache killing and insane overhead for benefit). A write to the GBA takes 2 cycles which isn't too bad - but the reason most people would write a self-modifying routine would be to get away from jump/loops. In ARM, each intruction can be conditionaly executed so it steals much of the thunder of self-modifying code. Why write a routine to update every 4th byte depending on a table or algorithm (2 cycles per write plus setup code and read code), when a conditinonal execution of each instruction takes only 1 extra intruction (1 for skipped, 1 for execution.)
I've thought about this, but from the above, I've not seen much need for it...
Mike
#20832 - Lupin - Tue May 18, 2004 3:36 pm
I thought of using it for a scanline filler where you don't know if you have to go left or right on the scanline you can just change the loop structure before entering the loop, if you have a lot of itterations it might pay off.
_________________
Team Pokeme
My blog and PM ASM tutorials
#20847 - Miked0801 - Tue May 18, 2004 6:35 pm
Could be - this was a popular application of it in 386/486 early 3D days. I'd have to see it to know if it was better though...
#20870 - keldon - Tue May 18, 2004 9:44 pm
self modifying code is useful only when needed to break through a bottlekneck. Other than that - - forget abowt it - that's my al pachino expression :-)
For code such as sequencers / event based engines, this can be useful, far faster than address[10] = event[3] - - then push address[10]; ret when you can simply write address [10] = event[3] and the code immediately jumps to event 3
but having said that how much time will be saved ?!?? like I said only if it breaks through a bottlekneck
#21003 - sasq - Fri May 21, 2004 2:25 pm
It is good when you run out of registers in the inner loop and can replace a register operand with a "constant". It is good for jumping out of an unrolled loop (when you cant just jump into it n iterations from the end).
The scanline left or right case sounds strange, in that situation I would duplicate the code instead. On GBA this might be a memory problem but surely not on a PC.
#21022 - jd - Fri May 21, 2004 6:41 pm
Self-modified code can get you some big gains in certain situations (my tmapper and audio mixer both use self-modified code extensively) but for most routines the gains are relatively small.
It's fairly straightforward to do on the GBA as there's no instruction cache and the opcodes are fairly logical - the only thing you've got to look out for is modifying instructions that are already in the pipeline although that's only a few instructions long so it's rarely a problem.
It's definitely something you should only consider if the code is performance critical.