(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
06-13-24 07:58 AM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Doing a modulus with limited resources
  
User name:
Password:
Reply:
 
Options: - -
Quik-Attach:
Preview for more options

Max size 1.00 MB, types: png, gif, jpg, txt, zip, rar, tar, gz, 7z, ace, mp3, ogg, mid, ips, bz2, lzh, psd

UserPost
sloat
Posts: 14/20
for SNES, it might be possible to multiply by the (reciprocal * 2^8)+1 if you're using a constant. I've never tried it on SNES, but with GBA and x86 asm, it works. The result would be in the high-byte of the multiplication result.

You could multiply the result by the divisor and subtract from the original number to get the remainder. I have no idea if all this would be slower than the regular divide function though.

Here's an example:

;123 / 10 = 12
(1/10 * 2^8)+1 = 0x1A

123 * 0x1A = 0x0C7E

As you can see, the high-byte of the result is 0x0C, which is 12.
Disch
Posts: 126/202
There's no shortcut... only way to mimic division/mod is to repeatedly subtract until you can't subtract anymore (painfully slow). This has been a topic with me in the past when trying to find a quick way to convert hex numbers to decimal strings in NES games (ie, 02 01 --> 0 2 5 8) -- I never found one. And all the resources I've checked (6502.org, various video game sources) all did it the "long" way or some variant.

The only way you can shortcut it is if you're dividing by a perfect power-of-two... in which case you can mod by ANDing out the low bits (AND #$3F == mod 64) -- but then that only works when unsigned.
MathOnNapkins
Posts: 362/1106
I don't really need this per se, but I'm curious if anyone has any ideas on how to implement the function a mod b with no hardware divide or division. Assume a and b are signed 16-bit integers. (SNES or NES pretty much). And yes I know the SNES has hardware division of sorts, but it takes 8 cycles to complete. I can't think of any bitwise trickery that would work, so I'm looking for a fairly fast algorithm in assembly.

Acmlm's Board - I3 Archive - Programming - Doing a modulus with limited resources


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.010 seconds; used 349.02 kB (max 381.41 kB)