Shop OBEX P1 Docs P2 Docs Learn Events
Performing modulus in PASM — Parallax Forums

Performing modulus in PASM

escherescher Posts: 138
edited 2018-01-31 00:33 in Propeller 1
I have a dynamic value, n, that I need to perform modulus on within a cog's PASM code in order to calculate a hub RAM memory offset.

Is there a fast/low instruction count trick to performing this operation? Google and forum searches haven't helped.

Thank you!

Comments

  • I apologize, my re-attempt at Googling somehow was more helpful... I'm sure there's some Law out there that states "Any problem searched extensively for will be solved by a re-search performed immediately after posting on a forum about it"
  • Heater.Heater. Posts: 21,230
    Yes indeed. Internet or not, often as soon as you can form the question in your mind, your mind has the answer!

    As to the "modulus" thing. That is easy or hard depending if your modulus is a power of 2 or not.

  • Heater. wrote: »
    Yes indeed. Internet or not, often as soon as you can form the question in your mind, your mind has the answer!

    As to the "modulus" thing. That is easy or hard depending if your modulus is a power of 2 or not.

    Fortunately it was modulus-8, so I was in luck on that front!
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2018-01-31 18:58
    For future reference then, modulus any number is just as easy, just a bit esoteric.
    cmpsub offset, modulus .  ' keeps offset in the range 0 to (modulus-1)
    

  • Of course that instruction assumes the value is already in the range 0 .. 2*modulus-1.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2018-02-01 22:03
    Good point. I was thinking of offset into a circular buffer or paged memory (as in data logging) where the address increments by one or by a small value less than the modulus. Quite a different thing, more complicated, for random access.

    I bookmarked the page that Escher linked to at Stanford.edu, bithacks. It reminds me of Hacker's Delight, wherein chapters 9 and 10 cover a lot of the same ground. A great reference.
Sign In or Register to comment.