Shop OBEX P1 Docs P2 Docs Learn Events
Sine lookup table question — Parallax Forums

Sine lookup table question

RumpleRumple Posts: 38
edited 2013-06-10 19:00 in Propeller 1
Many years ago I captured a handy-looking snippet from this forum that illustrated a method of retrieving sine values from the Prop's internal lookup table. It looked a lot like this:
PUB Sin(angle)                           'Sin angle is 13-bit, returns 16-bits
    Result := angle << 1 & $FFE          
    if angle & $800                      
        Result := word[$F000 - Result]   
    else
        Result := word[$E000 + Result]

Well, now I have a use for it so I'm using it. And it works superbly. But it bothers me that I can't figure out HOW it works superbly. There are gaping holes in my bit-twiddling knowledge. Could somebody gently explain to me how that shifting and masking makes an address out of an an angle (if that's what it's doing), and what problem that IF solves?

Thanks.

Comments

  • Brian RileyBrian Riley Posts: 626
    edited 2013-06-08 11:29
    Page 212 of the Hydra manual
  • max72max72 Posts: 1,155
    edited 2013-06-09 07:06
    Page 212 of th8e Hydra manual
    ... and in case you missed it this great book is available as free pdf download...
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-06-09 08:54
    The explanation on p212 of the Hydra book is the same as the one in appendix B of the Prop manual, in reference to the pasm code. I'm sure there are more succinct explanations in terms of spin code, but I did have a go at it back in Dave Scanlan's spin code tutorials for the beginner.

    Rumple, I think the spin code you posted above works for angles in the range of 0 to 180 degrees, those for which the result is a positive number. It takes a bit more code to complete the circle.

    The IF statement you asked about looks at bit 12 of the incoming angle. If that is a 1, that means the angle is in the second quadrant and the values in the table have to be read "backwards", that is subtracting from 90 degrees and working down to 0+ degrees. Thus
    [NOPARSE]Result := word[$F000 - Result] [/NOPARSE]
    
    The 90 degree value for the sine table is located at word address $F000 in the HUB, and the 0 degree address is at $E000.

    Note that $F000 - $E000 = $1000 or 4096 table bytes, 2048 table words, 11 bits worth, but note also that the table is $E000 to $F000 inclusive, so that is 2049 words. The incoming angle is shifted left by one to turn it into a word address, then the $FFE makes it 11 bits, and that is what goes into the table lookup. In the first quadrant it starts at $E000 and goes up to $EFFE. In the second quadrant it starts at $F000 and goes down to $E002.
  • RumpleRumple Posts: 38
    edited 2013-06-10 19:00
    Thanks, Tracy. Just what I was looking for. More tools for the toolbox.

    And thanks to Brian and Max for turning me on to 812 pages of Prop stuff I've never read before.
Sign In or Register to comment.