68000 programming optimization tips? (for speed)

Ask anything your want about Megadrive/Genesis programming.

Moderator: BigEvilCorporation

GManiac
Very interested
Posts: 92
Joined: Thu Jan 29, 2009 2:05 am
Location: Russia

Re: 68000 programming optimization tips? (for speed)

Post by GManiac » Wed Apr 19, 2017 8:22 pm

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.

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Sik » Thu Apr 20, 2017 9:50 pm

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.
Sik is pronounced as "seek", not as "sick".

Natsumi
Very interested
Posts: 82
Joined: Mon Oct 05, 2015 3:00 pm
Location: 0x0
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Natsumi » Fri Apr 21, 2017 10:53 am

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.

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Sik » Sat Apr 22, 2017 12:35 am

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".

Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: 68000 programming optimization tips? (for speed)

Post by Miquel » Mon Jul 09, 2018 5:43 am

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 ;)
HELP. Spanish TVs are brain washing people to be hostile to me.

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Sik » Mon Jul 09, 2018 7:35 am

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
Sik is pronounced as "seek", not as "sick".

Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: 68000 programming optimization tips? (for speed)

Post by Miquel » Wed Jul 11, 2018 9:40 am

You are right ;)
HELP. Spanish TVs are brain washing people to be hostile to me.

gasega68k
Very interested
Posts: 141
Joined: Thu Aug 22, 2013 3:47 am
Location: Venezuela - Caracas
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by gasega68k » Mon Aug 13, 2018 4:52 am

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.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Stef » Sun Aug 19, 2018 6:09 pm

I rarely need to save X but neat way of doing it :)

flamewing
Very interested
Posts: 56
Joined: Tue Sep 23, 2014 2:39 pm
Location: France

Re: 68000 programming optimization tips? (for speed)

Post by flamewing » Mon Aug 27, 2018 9:32 am

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).

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Stef » Tue Aug 28, 2018 2:58 pm

what sign(x) is intended to return ? 1 if signed, 0 if not ?

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: 68000 programming optimization tips? (for speed)

Post by Sik » 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.

Still trying to get my head around it, mind you (mainly the NEGX).
Sik is pronounced as "seek", not as "sick".

flamewing
Very interested
Posts: 56
Joined: Tue Sep 23, 2014 2:39 pm
Location: France

Re: 68000 programming optimization tips? (for speed)

Post by flamewing » Wed Aug 29, 2018 10:49 am

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.

Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: 68000 programming optimization tips? (for speed)

Post by Miquel » Sat Jan 04, 2020 12:56 pm

The most stupid optimization, but useful since can be done in lot of occasions:

Code: Select all

move.b    0xA10001, d0
and.b     #0xF , d0
Optimized:

Code: Select all

moveq.l    #0xF, d0
and.b      0xA10001, d0
There is a difference: it clears the 3 upper bytes, which perhaps is what you need.
Also works with other bit operations.
HELP. Spanish TVs are brain washing people to be hostile to me.

Post Reply