Page 2 of 2

Re: 68000 programming optimization tips? (for speed)

Posted: Wed Apr 19, 2017 8:22 pm
by GManiac
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)

Posted: Thu Apr 20, 2017 9:50 pm
by Sik
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.

Re: 68000 programming optimization tips? (for speed)

Posted: Fri Apr 21, 2017 10:53 am
by Natsumi
also it can be very useful for something like this;

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
Over branching conditionally, this actually saves 4 cycles per loop. Quite nifty optimization since this loop is ran many times over.

Re: 68000 programming optimization tips? (for speed)

Posted: Sat Apr 22, 2017 12:35 am
by Sik
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)

Re: 68000 programming optimization tips? (for speed)

Posted: Mon Jul 09, 2018 5:43 am
by Miquel
Sik wrote:
Thu Apr 20, 2017 9:50 pm
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.
Or just do a: "neg.b" to get the appropriate value.

Hard to remember all those 68k tricks ;)

Re: 68000 programming optimization tips? (for speed)

Posted: Mon Jul 09, 2018 7:35 am
by Sik
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

Re: 68000 programming optimization tips? (for speed)

Posted: Wed Jul 11, 2018 9:40 am
by Miquel
You are right ;)

Re: 68000 programming optimization tips? (for speed)

Posted: Mon Aug 13, 2018 4:52 am
by gasega68k
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:

Code: Select all

    subx.b  d0,d0  ;set d0 to $ff if X is set, otherwise 0
then to restore X flag:

Code: Select all

    add.b  d0,d0
This can also work with subx.w, add.w, in this case d0 will be set to $ffff, or 0.

Re: 68000 programming optimization tips? (for speed)

Posted: Sun Aug 19, 2018 6:09 pm
by Stef
I rarely need to save X but neat way of doing it :)

Re: 68000 programming optimization tips? (for speed)

Posted: Mon Aug 27, 2018 9:32 am
by flamewing
Speaking of X, there is a neat thing you can do with it:

Code: Select all

add.w    d0,d0
subx.w    d1,d1
negx.w    d0
addx.w    d1,d1
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).

Re: 68000 programming optimization tips? (for speed)

Posted: Tue Aug 28, 2018 2:58 pm
by Stef
what sign(x) is intended to return ? 1 if signed, 0 if not ?

Re: 68000 programming optimization tips? (for speed)

Posted: Tue Aug 28, 2018 8:49 pm
by Sik
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).

Re: 68000 programming optimization tips? (for speed)

Posted: Wed Aug 29, 2018 10:49 am
by flamewing
Sik wrote:
Tue Aug 28, 2018 8:49 pm
I think it's meant to return -1, 0 or +1. At least that's what sign functions usually do.
You guessed right.
Sik wrote:
Tue Aug 28, 2018 8:49 pm
Still trying to get my head around it, mind you (mainly the NEGX).
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.