68K Mod/Modular Arithmet -- nice and fast Routine Ideas

Ask anything your want about Megadrive/Genesis programming.

Moderator: BigEvilCorporation

Post Reply
MintyTheCat
Very interested
Posts: 484
Joined: Sat Mar 05, 2011 11:11 pm
Location: Berlin, Germany

68K Mod/Modular Arithmet -- nice and fast Routine Ideas

Post by MintyTheCat » Tue Apr 03, 2012 9:19 pm

Hello all,

this is a general M68K Question: I wonder how efficiently it is to implement the familiar Mod/Modular Arithmetic Operation in 68K.

I am thinking for my own Problem it might be better not to use a Mod Arithmetic Operation and just calculate the n-Multiples that I need over the Range, then compare each time. However, it's one of those Operations that comes up frequently so I'm going to spend some time working it out.

Any ideas would be appreciated :wink:

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Tue Apr 03, 2012 10:09 pm

Depends on how large the values are and how fast you wish the code to go. The standard divide on the 68000 gives both the quotient and remainder. So if a 16 bit remainder is sufficient, the regular DIVS/DIVU gives the mod in one operation in somewhere around 150 cycles.

MintyTheCat
Very interested
Posts: 484
Joined: Sat Mar 05, 2011 11:11 pm
Location: Berlin, Germany

Post by MintyTheCat » Sat Apr 07, 2012 10:59 pm

Chilly Willy wrote:Depends on how large the values are and how fast you wish the code to go. The standard divide on the 68000 gives both the quotient and remainder. So if a 16 bit remainder is sufficient, the regular DIVS/DIVU gives the mod in one operation in somewhere around 150 cycles.
Many Thanks, Chilly Willy.

A 16bit Remainder is what I am looking for.

I had forgotten that the 68K gave you the split Results in the two Words. Tbh I barely use mul and div.

I think that I'd be better off 'unrolling my Loop' as 150 Cycles is extreme.

Cheers.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Sat Apr 07, 2012 11:12 pm

Perhaps... be sure to cycle count your code. Sometimes people forget just how many cycles the 68000 uses for things - 8 to 10 cycles for a single instruction is not extreme... in fact, instructions are more likely to be 8 to 10 cycles than to be 4 to 6.

MintyTheCat
Very interested
Posts: 484
Joined: Sat Mar 05, 2011 11:11 pm
Location: Berlin, Germany

Post by MintyTheCat » Sat Apr 07, 2012 11:31 pm

Chilly Willy wrote:Perhaps... be sure to cycle count your code. Sometimes people forget just how many cycles the 68000 uses for things - 8 to 10 cycles for a single instruction is not extreme... in fact, instructions are more likely to be 8 to 10 cycles than to be 4 to 6.
:D Indeed. Most of the work I have done over the last six Years has been with PowerPCs and ARMs of one form or another so coming back to 68K is bit of an eye opener.

Post Reply