## [Sdcc-user] multiplication problem

 [Sdcc-user] multiplication problem From: Philipp Klaus Krause - 2007-07-25 19:36:51 ```The following functions should be equivalent: typedef f int16_t; #define F_R 6 struct ff { uint8_t l; int8_t m; } b; f fmul2_alt(f l, f r) { int32_t tl = l; tl *= r; return(tl >> F_R); } f fmul2(f l, f r) { int32_t tmp1, tmp2; tmp2 = (ffcast(l)->l * ffcast(r)->m); tmp2 += (ffcast(l)->m * ffcast(r)->l); tmp2 <<= 8; tmp1 = ffcast(l)->m * ffcast(r)->m; tmp1 <<= 16; tmp1 += (ffcast(l)->l * ffcast(r)->l); tmp1 += tmp2; return(tmp1 >> F_R); } On my host system using gcc I have tested them for all values of l, r and get the same result. Compiled using sdcc --std-c99 -mz80 I get different results though. The functions are multiplications of 16 bit fixed point numbers, the first one using a 32 bit multiplication (implemented by sdcc using 10 8 bit multiplications in device/lib/_mullong.c)), the second one my attempt to do it faster using 4 8-bit multiplications. Philipp ```

 [Sdcc-user] multiplication problem From: Philipp Klaus Krause - 2007-07-25 19:36:51 ```The following functions should be equivalent: typedef f int16_t; #define F_R 6 struct ff { uint8_t l; int8_t m; } b; f fmul2_alt(f l, f r) { int32_t tl = l; tl *= r; return(tl >> F_R); } f fmul2(f l, f r) { int32_t tmp1, tmp2; tmp2 = (ffcast(l)->l * ffcast(r)->m); tmp2 += (ffcast(l)->m * ffcast(r)->l); tmp2 <<= 8; tmp1 = ffcast(l)->m * ffcast(r)->m; tmp1 <<= 16; tmp1 += (ffcast(l)->l * ffcast(r)->l); tmp1 += tmp2; return(tmp1 >> F_R); } On my host system using gcc I have tested them for all values of l, r and get the same result. Compiled using sdcc --std-c99 -mz80 I get different results though. The functions are multiplications of 16 bit fixed point numbers, the first one using a 32 bit multiplication (implemented by sdcc using 10 8 bit multiplications in device/lib/_mullong.c)), the second one my attempt to do it faster using 4 8-bit multiplications. Philipp ```