How to map a number range to another number range in spin?
kf4ixm
Posts: 529
i've been trying to wrap my head around this all morning and for the life of me, can't figure out how to do this. what i want to do is take a number from 0 to 4096 and map that to a value between 1000 and 2000. I'm pretty sure im just missing something obvious, but thought that maybe someone could show me a spin example to do this. Thanks in advance!
Comments
-Phil
Question: In the old days bit shifting was much faster than division, but on many modern CPU's the ALU's can divide quickly. Is bit shifting faster on the Propeller?
EDIT: Phil beat me to it, and >> 12 takes less compute than / 4096
The Propeller does multiplication and division by subroutine (about 5us). It's relatively fast in Spin because of the general overhead in interpreting Spin code. All shifting is done in one instruction time (50ns at 80MHz) because the Propeller has what's called a "barrel shifter" which can do any size shift of any kind in one cycle.
@localroger, that is really good to know, multiplying to divide!
The PASM instruction set IS a "blast from the past". It is also a "chunk of the present and future" since the same basic arithmetic and logic functions are used in current computers, and will almost certainly be in future ones as well.