summaryrefslogtreecommitdiff
path: root/n/asserv/src/dsp.c
blob: 576040b356c5f35b643d8511705a723e15907a6b (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* 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 */

/** Numbers notation:
 * [u]{i|f}x[.y]
 * u: unsigned
 * i: integer
 * f: fixed point
 * x: integral part size in bits
 * y: fractionnal part size in bits
 *
 * Ex: i16: signed 16 bit word, uf8.8: unsigned fixed 8.8.
 *
 * Angles are mapped from [0, 2pi) to [0,1). */

/** Add two signed words (i16) 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 i16 by uf8.8, return i16. */
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 f8.24 by f8.24, return f8.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 for angles between [0,pi/2]. */
int32_t
dsp_cos_dli (int32_t a)
{
    static const int32_t f[] = {
	(1L << 24) * -26.42625678337439745096,
	(1L << 24) * 60.24464137187666035919,
	(1L << 24) * -85.45681720669372773226,
	(1L << 24) * 64.93939402266829148905,
	(1L << 24) * -19.73920880217871723738,
	(1L << 24) * 1
    };
    int32_t r;
    int32_t a2;
    uint8_t i;
    a2 = dsp_mul_f824 (a, a);
    r = f[0];
    for (i = 1; i < sizeof (f) / sizeof (f[0]); i++)
	r = dsp_mul_f824 (r, a2) + f[i];
    return r;
}

/** Compute cosinus, angle f8.24, result f8.24. */
int32_t
dsp_cos (int32_t a)
{
    a &= (1L << 24) - 1;
    uint8_t z = ((uint32_t) a >> 16) & 0xc0;
    if (z == 0)
	return dsp_cos_dli (a);
    else if (z == 1 << 6)
	return -dsp_cos_dli ((1L << 23) - a);
    else if (z == 2 << 6)
	return -dsp_cos_dli (a & 0xff7fffff);
    else
	return dsp_cos_dli ((1L << 24) - a);
}

/** Compute sinus, angle f8.24, result f8.24. */
int32_t
dsp_sin (int32_t a)
{
    a &= (1L << 24) - 1;
    uint8_t z = ((uint32_t) a >> 16) & 0xc0;
    if (z == 0)
	return dsp_cos_dli ((1L << 22) - a);
    else if (z == 1 << 6)
	return dsp_cos_dli (a - (1L << 22));
    else if (z == 2 << 6)
	return -dsp_cos_dli ((1L << 23) + (1L << 22) - a);
    else
	return -dsp_cos_dli (a - (1L << 23) - (1L << 22));
}