summaryrefslogtreecommitdiff
path: root/n/asserv/src/dsp.c
blob: bb81fdb97470e65f33a7ea4e5f1eddc5d4dfb732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* dsp.c */
/* asserv - Position & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2004 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "dsp.h"

/* +AutoDec */
/* -AutoDec */

/** Add two signed words and saturate. UNTESTED. */
extern inline int16_t
dsp_add_sat_i16i16 (int16_t a, int16_t b)
{
    asm ("add %A0, %A1\n\t"
	 "adc %B0, %B1\n\t"
	 /* Branch if not V. */
	 "brvc 1f\n\t"
	 /* Load Max. */
	 "ldi %A0, 0xff\n\t"
	 "ldi %B0, 0x7f\n\t"
	 /* Branch if not S. */
	 "brlt 1f\n\t"
	 /* Load Min. */
	 "ldi %A0, 0x00\n\t"
	 "ldi %B0, 0x80\n\t"
    "1:" "\n\t"
	 : "=r" (a) : "0" (a), "r" (b));
    return a;
}

/** Multiply signed 16 by unsigned 8.8, return signed 16. */
extern inline int16_t
dsp_mul_i16f88 (int16_t a, uint16_t b)
{
    int16_t r;
    asm ("mulsu %B1, %B2\n\t"
	 "mov %B0, r0\n\t"
	 "mul %A1, %A2\n\t"
	 "mov %A0, r1\n\t"
	 "mul %A1, %B2\n\t"
	 "add %A0, r0\n\t"
	 "adc %B0, r1\n\t"
	 "mulsu %B1, %A2\n\t"
	 "add %A0, r0\n\t"
	 "adc %B0, r1\n\t"
	 "clr r1\n\t"
	 : "=&r" (r) : "a" (a), "a" (b) : "r0");
    return r;
}

/** Multiply signed 8.24 by signed 8.24, return signed 8.24. */
int32_t
dsp_mul_f824 (int32_t a, int32_t b)
{
    register int32_t ar asm ("r16"), br asm ("r20");
    ar = a;
    br = b;
    int32_t r;
    int8_t z;
    asm (""
	 "clr %1\n\t"
	 /* Low dword (>> 8, with 8 guards). */
	 "mul %A2, %B3\n\t"
	  "movw %A0, r0\n\t"
	  "clr %C0\n\t"
	  "clr %D0\n\t"
	 "mul %A2, %A3\n\t"
	  "add %A0, r1\n\t"
	  "adc %B0, %1\n\t"
	  "adc %C0, %1\n\t"
	 "mul %B2, %A3\n\t"
	  "add %A0, r0\n\t"
	  "adc %B0, r1\n\t"
	  "adc %C0, %1\n\t"
	 "mul %A2, %C3\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 "mul %B2, %B3\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 "mul %C2, %A3\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 /* Shift. */
	 "movw %A0, %C0\n\t"
	 /* Upper word. */
	 "mulsu %D3, %C2\n\t"
	  "movw %C0, r0\n\t"
	 "mulsu %D3, %A2\n\t"
	  "sbc %C0, %1\n\t"
	  "sbc %D0, %1\n\t"
	  "add %A0, r0\n\t"
	  "adc %B0, r1\n\t"
	  "adc %C0, %1\n\t"
	  "adc %D0, %1\n\t"
	 "mul %B2, %C3\n\t"
	  "add %A0, r0\n\t"
	  "adc %B0, r1\n\t"
	  "adc %C0, %1\n\t"
	  "adc %D0, %1\n\t"
	 "mul %C2, %B3\n\t"
	  "add %A0, r0\n\t"
	  "adc %B0, r1\n\t"
	  "adc %C0, %1\n\t"
	  "adc %D0, %1\n\t"
	 "mulsu %D2, %A3\n\t"
	  "sbc %C0, %1\n\t"
	  "sbc %D0, %1\n\t"
	  "add %A0, r0\n\t"
	  "adc %B0, r1\n\t"
	  "adc %C0, %1\n\t"
	  "adc %D0, %1\n\t"
	 "mulsu %D3, %B2\n\t"
	  "sbc %D0, %1\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 "mul %C2, %C3\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 "mulsu %D2, %B3\n\t"
	  "sbc %D0, %1\n\t"
	  "add %B0, r0\n\t"
	  "adc %C0, r1\n\t"
	  "adc %D0, %1\n\t"
	 "mulsu %D2, %C3\n\t"
	  "add %C0, r0\n\t"
	  "adc %D0, r1\n\t"
	 "muls %D2, %D3\n\t"
	  "add %D0, r0\n\t"
	 "clr r1\n\t"
	 : "=&r" (r), "=r" (z) : "a" (ar), "a" (br) : "r0");
    return r;
}

/** Compute cosinus. */