64-bit integer math (PASM)
ManAtWork
Posts: 2,176
I found out that sometimes it's easier and more efficient (code size) to use 64 bit math instead of trying to fit everything into 32 bits with lots of shift operations. In this thread I'd like to share my solutions for simple math routines in assembler. Suggestions for improvements/optimisations are welcome.
64 bit add and subtract are easy and are documeted in the propeller manual.
If you have to add a signed 32 bit value to a 64 bit long you can use this:
However, abs and neg are a bit more difficult.
64 bit add and subtract are easy and are documeted in the propeller manual.
' unsigned x:= x+y (or also signed if flags don't care) add xl,yl wz,wc addx xh,yh wz ' signed add xl,yl wz,wc addsx xh,yh wz ' unsigned x:= x-y sub xl,yl wc,wz subx xh,yh wz ' signed: use subsx
If you have to add a signed 32 bit value to a 64 bit long you can use this:
move temp,y sar temp,#31 ' sign extend add xl,y wc addx xh,temp
However, abs and neg are a bit more difficult.
abs xh,xh wc,wz if_c neg xl,xl wz if_c_and_nz sub xh,#1 if_nc_and_z mov xl,xl wz ' test for Z-flag (optional) neg xl,xl wz neg xh,xh wc if_nz sub xh,#1 if_z mov xh,xh wz ' test for Z-flag (optional)
Comments
Cool.
One 64 bit lib that is nice to have, that can extend the use of 32 bit, is the classic scaling algorithm
R = (A * / C
Here A * B => 64 bit results, and then 64/32(C) applies to that result. The 64 bit value is important, but hidden from the user.
and another is Binary to BCD, see the Prop2 thread for some examples.
thanks
richard