AnsweredAssumed Answered

Optimized Multiplies for Cosmic Compiler?

Question asked by bryson.paul on Nov 11, 2017
Latest reply on Nov 21, 2017 by luca

Are there optimized basic math functions available for the STM8 such as; 8 bit by 8 bit multiply and 8 bit by 16 bit multiply and similar?


My application has a time critical 8 bit by 16 bit multiply.  The Cosmic compiler seems to always default to a 16 bit by 16 bit multiply, which is slower.  I wrote an inline assembly macro that runs in about 2/3 the time of the compiler's output; but it was very tedious to write.  I would rather not do this for every math function.

Any helpful information would be appreciated.


I include my macro here, in case anyone else finds it useful:


// macro to perform an optimized UI8 by UI16 multiply: uint16_t RESULT_UI16 = (uint8_t)X_UI8 * (uint16_t)Y_UI16;

// Note: All arguments must be declared "@tiny"

// macro assumes that no overflow occurs

// "_asm()" will load an 8 bit argument in reg A or a 16 argument into reg X

#define MULT_8x16(X_UI8, Y_UI16, RESULT_UI16) {\

      _asm("LDW Y,X\n SWAPW X\n",(uint16_t)Y_UI16);\

      _asm("MUL X,A\n SWAPW X\n PUSHW X\n LDW X,Y\n", (uint8_t)X_UI8);\       

      _asm("MUL X,A\n ADDW X,($1,SP)\n POPW Y\n");\       

      _asm("CLRW Y\n LD YL,A\n LDW (Y),X\n",(@tiny uint16_t*)(&RESULT_UI16));\