avr-gcc-list
[Top][All Lists]

## Re: [avr-gcc-list] Optimizing a 16-bit * 8-bit -> 24-bit multiplication

 From: Galen Seitz Subject: Re: [avr-gcc-list] Optimizing a 16-bit * 8-bit -> 24-bit multiplication Date: Fri, 01 Dec 2006 16:41:46 -0800 User-agent: Thunderbird 1.5.0.7 (X11/20060915)

```Shaun Jackman wrote:
```
```I would like to multiply a 16-bit number by an 8-bit number and
produce a 24-bit result on the AVR. The AVR has a hardware 8-bit *
8-bit -> 16-bit multiplier.

If I multiply a 16-bit number by a 16-bit number, it produces a 16-bit
result, which isn't wide enough to hold the result.

If I cast one of the operands to 32-bit and multiply a 32-bit number
by a 16-bit number, GCC generates a call to __mulsi3, which is the
routine to multiply a 32-bit number by a 32-bit number and produce a
32-bit result and requires ten 8-bit * 8-bit multiplications.

A 16-bit * 8-bit -> 24-bit multiplication only requires two 8-bit *
8-bit multiplications. A 16-bit * 16-bit -> 32-bit multiplication
requires four 8-bit * 8-bit multiplications.

I could write a mul24_16_8 (16-bit * 8-bit -> 24-bit) function using
unions and 8-bit * 8-bit -> 16-bit multiplications, but before I go
down that path, is there any way to coerce GCC into generating the
code I desire?
```
```
Not exactly what you want, but this might help you get started.

galen

extern inline int16_t
mult_s16_u8s16(uint8_t a, int16_t b)
{
int16_t product;
asm (
"mul %A2, %1"   "\n\t"
"movw %0, r0"   "\n\t"
"mulsu %B2, %1" "\n\t"
"clr r1"        "\n\t"
: "=w" (product)
: "a" (a), "a" (b)
);
return product;
}

```