68000 programming optimization tips? (for speed)
Moderator: BigEvilCorporation
Re: 68000 programming optimization tips? (for speed)
By the way, a lot of people ignore Scc commands (ST, SF, Sxx), but they are pretty useful to calculate complex boolean expressions (with addition of NOT, AND, OR, EOR) rather than doing a massive branching.
Re: 68000 programming optimization tips? (for speed)
Can confirm, once I was looking at the pathfinding code in Star Chaser and ran into a SHS instruction and was wondering what the heck was going on because of how uncommon it is. (pretty ironic since I wrote that code...)
These days I'm exploiting Scc a lot for quickly getting 0/1 values from a condition (Scc followed by AND). Or just outright 0/-1 (no AND) if I just want a boolean to be used later.
These days I'm exploiting Scc a lot for quickly getting 0/1 values from a condition (Scc followed by AND). Or just outright 0/-1 (no AND) if I just want a boolean to be used later.
Sik is pronounced as "seek", not as "sick".
Re: 68000 programming optimization tips? (for speed)
also it can be very useful for something like this;
Over branching conditionally, this actually saves 4 cycles per loop. Quite nifty optimization since this loop is ran many times over.
Code: Select all
.cloop tst.b (a1)+ ; check if entry is null
seq d2 ; if is, set d2 to $FF
add.b d2,d3 ; and then subtract 1 from total count if so
dbf d1,.cloop ; loop til all cards are checked
Re: 68000 programming optimization tips? (for speed)
Not to mention the part where you avoid an extra label just for the branch. The best kind of optimizations are those that also make the code look cleaner =O) (like using MOVEP instead of bit shifting for generating VDP commands for DMA)
Sik is pronounced as "seek", not as "sick".
Re: 68000 programming optimization tips? (for speed)
Or just do a: "neg.b" to get the appropriate value.
Hard to remember all those 68k tricks
HELP. Spanish TVs are brain washing people to be hostile to me.
Re: 68000 programming optimization tips? (for speed)
The Scc instructions only come in byte size so if I want a word or long result I actually need to use AND if I want 1 instead of -1 (I mean, I could use EXT and NEG together, but it's not better and takes up more lines of code).
EDIT: also using AND you can get other values like 2 or 4. Convenient for offsets :v
EDIT: also using AND you can get other values like 2 or 4. Convenient for offsets :v
Sik is pronounced as "seek", not as "sick".
Re: 68000 programming optimization tips? (for speed)
You are right
HELP. Spanish TVs are brain washing people to be hostile to me.
-
- Very interested
- Posts: 141
- Joined: Thu Aug 22, 2013 3:47 am
- Location: Venezuela - Caracas
- Contact:
Re: 68000 programming optimization tips? (for speed)
I don't know if this should be called programmig optimization or programming trick, but is someting that I did recently to quick save/restore X flag:
then to restore X flag:
This can also work with subx.w, add.w, in this case d0 will be set to $ffff, or 0.
Code: Select all
subx.b d0,d0 ;set d0 to $ff if X is set, otherwise 0
Code: Select all
add.b d0,d0
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: 68000 programming optimization tips? (for speed)
I rarely need to save X but neat way of doing it
Re: 68000 programming optimization tips? (for speed)
Speaking of X, there is a neat thing you can do with it:
The result: d1 = sign(d0).
You can also use subx and addx to compute the min, max, and minmax of 2 numbers, but it is slower than the branch versions (but might be useful if you need the functions to run in constant time).
Code: Select all
add.w d0,d0
subx.w d1,d1
negx.w d0
addx.w d1,d1
You can also use subx and addx to compute the min, max, and minmax of 2 numbers, but it is slower than the branch versions (but might be useful if you need the functions to run in constant time).
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: 68000 programming optimization tips? (for speed)
what sign(x) is intended to return ? 1 if signed, 0 if not ?
Re: 68000 programming optimization tips? (for speed)
I think it's meant to return -1, 0 or +1. At least that's what sign functions usually do.
Still trying to get my head around it, mind you (mainly the NEGX).
Still trying to get my head around it, mind you (mainly the NEGX).
Sik is pronounced as "seek", not as "sick".
Re: 68000 programming optimization tips? (for speed)
You guessed right.
It's only purpose is to set X if d0 was nonzero. Note that subx d1,d1 preserves X, which is important for the next if d0 started out as $8000.
Re: 68000 programming optimization tips? (for speed)
The most stupid optimization, but useful since can be done in lot of occasions:
Optimized:
There is a difference: it clears the 3 upper bytes, which perhaps is what you need.
Also works with other bit operations.
Code: Select all
move.b 0xA10001, d0
and.b #0xF , d0
Code: Select all
moveq.l #0xF, d0
and.b 0xA10001, d0
Also works with other bit operations.
HELP. Spanish TVs are brain washing people to be hostile to me.