Mozzi  version v2.0
sound synthesis library for Arduino
mult16x8.h
1 
2 /*
3 Norbert Pozar 2009
4 http://mekonik.wordpress.com/2009/03/18/arduino-avr-gcc-multiplication/
5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
7 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
8 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
9 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
10 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
11 
12 // multiplies 16 bit X 8 bit
13 // stores lower 16 bits
14 #define MultiSU16X8toL16(intRes, int16In, int8In) \
15 asm volatile ( \
16 "mul %A1, %2 \n\t"\
17 "movw %A0, r0 \n\t"\
18 "mulsu %B1, %2 \n\t"\
19 "add %B0, r0 \n\t"\
20 "clr r1"\
21 : \
22 "=&r" (intRes) \
23 : \
24 "a" (int16In), \
25 "a" (int8In) \
26 )
27 
28 // multiplies 16 bit number X 8 bit constant
29 // saves lower 16 bit
30 // 8 cycles
31 #define MultiSU16XConst8toL16(intRes, int16In, int8In) \
32 asm volatile ( \
33 "ldi r22, %2 \n\t"\
34 "mul %A1, r22 \n\t"\
35 "movw %A0, r0 \n\t"\
36 "mulsu %B1, r22 \n\t"\
37 "add %B0, r0 \n\t"\
38 "clr r1 \n\t"\
39 : \
40 "=&r" (intRes) \
41 : \
42 "a" (int16In), \
43 "M" (int8In) \
44 :\
45 "r22"\
46 )
47 
48 // multiplies 16 bit number X 8 bit and stores 2 high uint8_ts
49 #define MultiSU16X8toH16(intRes, int16In, int8In) \
50 asm volatile ( \
51 "clr r26 \n\t"\
52 "mulsu %B1, %A2 \n\t"\
53 "movw %A0, r0 \n\t"\
54 "mul %A1, %A2 \n\t"\
55 "add %A0, r1 \n\t"\
56 "adc %B0, r26 \n\t"\
57 "clr r1 \n\t"\
58 : \
59 "=&r" (intRes) \
60 : \
61 "a" (int16In), \
62 "a" (int8In) \
63 :\
64 "r26"\
65 )
66 
67 // multiplies 16 bit signed number X 8 bit and stores 2 high uint8_ts
68 // rounds the number based on the MSB of the lowest uint8_t
69 #define MultiSU16X8toH16Round(intRes, int16In, int8In) \
70 asm volatile ( \
71 "clr r26 \n\t"\
72 "mulsu %B1, %A2 \n\t"\
73 "movw %A0, r0 \n\t"\
74 "mul %A1, %A2 \n\t"\
75 "add %A0, r1 \n\t"\
76 "adc %B0, r26 \n\t"\
77 "lsl r0 \n\t"\
78 "adc %A0, r26 \n\t"\
79 "adc %B0, r26 \n\t"\
80 "clr r1 \n\t"\
81 : \
82 "=&r" (intRes) \
83 : \
84 "a" (int16In), \
85 "a" (int8In) \
86 :\
87 "r26"\
88 )