cancel
Showing results for 
Search instead for 
Did you mean: 

Optimized Multiplies for Cosmic Compiler?

paulrbryson
Associate III
Posted on November 11, 2017 at 23:27

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));\

}

#compiler #math

Note: this post was migrated and contained many threaded conversations, some content may be missing.
24 REPLIES 24
Posted on November 17, 2017 at 11:56

Since the use case shows that 8x16->16 speec an be important, I've also opened a feature request for SDCC:

https://sourceforge.net/p/sdcc/feature-requests/548/

 

However, we still have a few other optimizations reqeusted by SDCC users to implement first; still, since implementing the 8x16->16 for --opt-code-speed is not that hard, it will probably get implementd sometime after the 3.7.0 release.

Philipp

Posted on November 20, 2017 at 10:18

example?

Posted on November 20, 2017 at 13:59

What you are mean?

Posted on November 21, 2017 at 09:01

if you find that the code generated by the current compiler could be better in some cases (for example multiplication by constants) you should tell us

- the original C code

- the assembler currently generated

- the assembler that you would like to be generated

This will allow us to evaluate the possibility to improve the compiler for this specifc case.

Posted on November 21, 2017 at 11:52

Ok. Multiplying by the column through 8bit MUL instruction is a good solution. For some constants it is possible to build the code from shifts and additions, as shown by the Philipp. But the availability of such an opportunity in the compiler, personally I do not consider as a critical necessity.