In the book "Microprocessor Music Applications" the author gives the following algorithm for 4-quadrant multiplication of two 8-bit signed integers with a 16-bit result:
Unsigned multiplication by raw operands. Then, in order to correct the result, if the animation sign is negative, the unsigned single precision subtracts the multiplier from the 8 upper bits of the raw 16-bit result. If the multiplier sign is also negative, then unsigned single precision subtracts the animation from the top 8 bits of the original 16-bit result.
I tried to implement this in assembler and cannot make it work. For example, if I unsigned is multiplied by -2 times -2, the result in binary is B11111100.00000100. When I subtract B1111110 twice from the first 8 bits according to the algorithm, I get B11111110.00000100, and not B00000000.00000100, as I would like. Thanks for any insight into where I might go wrong!
Change - code:
#define smultfix(a,b) \ ({ \ int16_t sproduct; \ int8_t smultiplier = a, smultiplicand = b; \ uint16_t uproduct = umultfix(smultiplier,smultiplicand);\ asm volatile ( \ "add %2, r1 \n\t" \ "brpl smult_"QUOTE(__LINE__)"\n\t" \ "sec \n\t" \ "sbc %B3, %1 \n\t" \ "smult_"QUOTE(__LINE__)": add %1, r1 \n\t" \ "brpl send_"QUOTE(__LINE__)" \n\t" \ "sec \n\t" \ "sbc %B3, %2 \n\t" \ "send_"QUOTE(__LINE__)": movw %A0,%A3 \n\t" \ :"=&r" (sproduct):"a" (smultiplier), "a" (smultiplicand), "a" (uproduct)\ ); \ sproduct; \ })
Bitrex
source share