Propeller Manual

Table Of Contents
Appendix B: Math Samples and Function Tables
Appendix B: Math Samples and Function Tables
Multiplication, Division, and Square Root
Multiplication, division, and square root can be computed by using add, subtract, and shift
instructions. Here is an unsigned multiplier routine that multiplies two 16-bit values to yield
a 32-bit product:
'' Multiply x[15..0] by y[15..0] (y[31..16] must be 0)
' on exit, product in y[31..0]
'
multiply shl x,#16 'get multiplicand into x[31..16]
mov t,#16 'ready for 16 multiplier bits
shr y,#1 wc 'get initial multiplier bit into c
:loop if_c add y,x wc 'if c set, add multiplicand to product
rcr y,#1 wc 'put next multiplier in c, shift prod.
djnz t,#:loop 'loop until done
multiply_ret ret 'return with product in y[31..0]
The above routine's execution time could be cut by ~1/3 if the loop was unrolled, repeating
the
ADD / RCR sequence and getting rid of the DJNZ instruction.
Division is like multiplication, but backwards. It is potentially more complex, though,
because a comparison test must be performed before a subtraction can take place. To remedy
this, there don’t is a special CMPSUB D, S instructions which tests to see if a subtraction can be
performed without causing an underflow. If no underflow would occur, the subtraction takes
place and the C output is 1. If an underflow would occur, D is left alone and the C output is 0.
Here is an unsigned divider routine that divides a 32-bit value by a 16-bit value to yield a 16-
bit quotient and a 16-bit remainder:
' Divide x[31..0] by y[15..0] (y[16] must be 0)
' on exit, quotient is in x[15..0] and remainder is in x[31..16]
'
divide shl y,#15 'get divisor into y[30..15]
mov t,#16 'ready for 16 quotient bits
:loop cmpsub x,y wc 'y =< x? Subtract it, quotient bit in c
rcl x,#1 'rotate c into quotient, shift dividend
djnz t,#:loop 'loop until done
divide_ret ret 'quotient in x[15..0],
'remainder in x[31..16]
Page 380 · Propeller Manual v1.1