Question of Shifts in SPIN
frank freedman
Posts: 1,983
in Propeller 1
Most likely I have missed something in the PROP manual, but my question regards shifts in SPIN and if there is a carry in/out test than can be done. Easy enough in PASM, but the spin descriptions are not as clear to me. Given an array of 12 bit words, I would like to unwind them into another array of words by shifting left or right and using the carry out of SourceVar(m) as the bit to shift into the DestVar(n) of the destination array (think rotate the array 90 degrees). Has anyone used the SPIN shift instructions to set or clear a bit in a word depending on the carry out value?
Thanks for any ideas.
FF
Thanks for any ideas.
FF
Comments
mask out 4-bit of fromarray with and
or 4-bit into toarray
I would use a pointer to the from byte
and then split into 3 variations
just first 4 bit
just last 4 bits
both (all) 8 bits
and a pointer to the tobyte also 3 variations
first 4
last4
both 8 bit
You could check first if both arrays are at 0bit position in the start byte and 7bit on the end byte, special case, then you can use bytemove…
but c out of << I don't know.
Enjoy!
Mike
use bytearrays,
if shift left check if source byte is negative -> your carry
if shift right check if byte bit 0 is set - your carry
the you can use << or >> on bytes
but that still leaves the half byte problem of 12 bit
Mike
-Phil
-Phil
I think much better to use shifts instead of multiply and divide. A simple compiler (like the default Spin compiler) will just blindly translate that code and will actually do multiply and divide, which are very slow. fastspin will try to use shifts automatically, but the "out /= 2" actually requires some tricky handling because in Spin variables are signed and a signed divide by 2 is not the same as a logical shift right, so it has to check for the "out" variable being negative (if fastspin were a bit smarter perhaps it could conclude that it cannot be negative because of the reverse at the top, but it's not that clever).
For fastspin does do something like:
Definitely a 0.01 version, so if anyone sees another way, chime in. It does work as expected.
@Phil, did end up doing a bit test and shift.....
FF
word[Channel][m] |= word[SampleBits][n] >> m & 1 to
word[Channel][m] |= byte[SampleBits][n] >> m & 1 since the sample bits array is a byte wide sample for up to 8 channels.
it worked perfectly. Nice......
'8channels 12 bit converter sample for testing
' channel: 76543210
Test11 = %01011010 'bit 2^11 snapshot
Test10 = %01011100 'bit 2^10 snapshot
Test9 = %10011010 'bit 2^9 snapshot
Test8 = %10011100 'bit 2^8 snapshot
Test7 = %01101010 'bit 2^7 snapshot
Test6 = %01101100 'bit 2^6 snapshot
Test5 = %10101010 'bit 2^5 snapshot
Test4 = %10101100 'bit 2^4 snapshot
Test3 = %01011010 'bit 2^3 snapshot
Test2 = %01011100 'bit 2^2 snapshot
Test1 = %10011010 'bit 2^1 snapshot
Test0 = %10011100 'bit 2^0 snapshot
FF routine.....
What Output array addx 050C
number of channels 8
Channel 0 addx 050C binary 000000000000
Channel 1 addx 050E binary 101010101010
Channel 2 addx 0510 binary 010101010101
Channel 3 addx 0512 binary 111111111111
Channel 4 addx 0514 binary 111100001111
Channel 5 addx 0516 binary 000011110000
Channel 6 addx 0518 binary 110011001100
Channel 7 addx 051A binary 001100110011
Chris' routine
What Output array addx 0500
number of channels 8
Channel 0 addx 0500 binary 000000000000
Channel 1 addx 0502 binary 101010101010
Channel 2 addx 0504 binary 010101010101
Channel 3 addx 0506 binary 111111111111
Channel 4 addx 0508 binary 111100001111
Channel 5 addx 050A binary 000011110000
Channel 6 addx 050C binary 110011001100
Channel 7 addx 050E binary 001100110011