Higher the button delay on the fuze a bit more, the wrong hold button reads aren...
[kugel-rb.git] / apps / codecs / lib / asm_mcf5249.h
blob8378accb2a629ba6c035609841ffc23506c56107
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2005 by Pedro Vasconcelos
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 /* asm routines for wide math on the MCF5249 */
22 #if defined(CPU_COLDFIRE)
24 /* attribute for 16-byte alignment */
25 #define LINE_ATTR __attribute__ ((aligned (16)))
27 #ifndef _V_WIDE_MATH
28 #define _V_WIDE_MATH
30 static inline int32_t MULT32(int32_t x, int32_t y) {
32 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply & shift */
33 "movclr.l %%acc0, %[x];" /* move & clear acc */
34 "asr.l #1, %[x];" /* no overflow test */
35 : [x] "+&d" (x)
36 : [y] "r" (y)
37 : "cc");
38 return x;
41 static inline int32_t MULT31(int32_t x, int32_t y) {
42 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply */
43 "movclr.l %%acc0, %[x];" /* move and clear */
44 : [x] "+&r" (x)
45 : [y] "r" (y)
46 : "cc");
47 return x;
50 static inline int32_t MULT31_SHIFT15(int32_t x, int32_t y) {
51 int32_t r;
53 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply */
54 "mulu.l %[y], %[x];" /* get lower half, avoid emac stall */
55 "movclr.l %%acc0, %[r];" /* get higher half */
56 "asl.l #8, %[r];" /* hi<<16, plus one free */
57 "asl.l #8, %[r];"
58 "lsr.l #8, %[x];" /* (unsigned)lo >> 15 */
59 "lsr.l #7, %[x];"
60 "or.l %[x], %[r];" /* logical-or results */
61 : [r] "=&d" (r), [x] "+d" (x)
62 : [y] "d" (y)
63 : "cc");
64 return r;
67 static inline
68 void XPROD31(int32_t a, int32_t b,
69 int32_t t, int32_t v,
70 int32_t *x, int32_t *y)
72 asm volatile ("mac.l %[a], %[t], %%acc0;"
73 "mac.l %[b], %[v], %%acc0;"
74 "mac.l %[b], %[t], %%acc1;"
75 "msac.l %[a], %[v], %%acc1;"
76 "movclr.l %%acc0, %[a];"
77 "move.l %[a], (%[x]);"
78 "movclr.l %%acc1, %[a];"
79 "move.l %[a], (%[y]);"
80 : [a] "+&r" (a)
81 : [x] "a" (x), [y] "a" (y),
82 [b] "r" (b), [t] "r" (t), [v] "r" (v)
83 : "cc", "memory");
86 static inline
87 void XNPROD31(int32_t a, int32_t b,
88 int32_t t, int32_t v,
89 int32_t *x, int32_t *y)
91 asm volatile ("mac.l %[a], %[t], %%acc0;"
92 "msac.l %[b], %[v], %%acc0;"
93 "mac.l %[b], %[t], %%acc1;"
94 "mac.l %[a], %[v], %%acc1;"
95 "movclr.l %%acc0, %[a];"
96 "move.l %[a], (%[x]);"
97 "movclr.l %%acc1, %[a];"
98 "move.l %[a], (%[y]);"
99 : [a] "+&r" (a)
100 : [x] "a" (x), [y] "a" (y),
101 [b] "r" (b), [t] "r" (t), [v] "r" (v)
102 : "cc", "memory");
105 #if 0 /* canonical Tremor definition */
106 #define XPROD32(_a, _b, _t, _v, _x, _y) \
107 { (_x)=MULT32(_a,_t)+MULT32(_b,_v); \
108 (_y)=MULT32(_b,_t)-MULT32(_a,_v); }
109 #endif
111 /* this could lose the LSB by overflow, but i don't think it'll ever happen.
112 if anyone think they can hear a bug caused by this, please try the above
113 version. */
114 #define XPROD32(_a, _b, _t, _v, _x, _y) \
115 asm volatile ("mac.l %[a], %[t], %%acc0;" \
116 "mac.l %[b], %[v], %%acc0;" \
117 "mac.l %[b], %[t], %%acc1;" \
118 "msac.l %[a], %[v], %%acc1;" \
119 "movclr.l %%acc0, %[x];" \
120 "asr.l #1, %[x];" \
121 "movclr.l %%acc1, %[y];" \
122 "asr.l #1, %[y];" \
123 : [x] "=&d" (_x), [y] "=&d" (_y) \
124 : [a] "r" (_a), [b] "r" (_b), \
125 [t] "r" (_t), [v] "r" (_v) \
126 : "cc");
128 #ifndef _V_VECT_OPS
129 #define _V_VECT_OPS
131 /* asm versions of vector operations for block.c, window.c */
132 /* assumes MAC is initialized & accumulators cleared */
133 static inline
134 void vect_add(int32_t *x, const int32_t *y, int n)
136 /* align to 16 bytes */
137 while(n>0 && (int)x&15) {
138 *x++ += *y++;
139 n--;
141 asm volatile ("bra 1f;"
142 "0:" /* loop start */
143 "movem.l (%[x]), %%d0-%%d3;" /* fetch values */
144 "movem.l (%[y]), %%a0-%%a3;"
145 /* add */
146 "add.l %%a0, %%d0;"
147 "add.l %%a1, %%d1;"
148 "add.l %%a2, %%d2;"
149 "add.l %%a3, %%d3;"
150 /* store and advance */
151 "movem.l %%d0-%%d3, (%[x]);"
152 "lea.l (4*4, %[x]), %[x];"
153 "lea.l (4*4, %[y]), %[y];"
154 "subq.l #4, %[n];" /* done 4 elements */
155 "1: cmpi.l #4, %[n];"
156 "bge 0b;"
157 : [n] "+d" (n), [x] "+a" (x), [y] "+a" (y)
158 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
159 "cc", "memory");
160 /* add final elements */
161 while (n>0) {
162 *x++ += *y++;
163 n--;
167 static inline
168 void vect_copy(int32_t *x, int32_t *y, int n)
170 /* align to 16 bytes */
171 while(n>0 && (int)x&15) {
172 *x++ = *y++;
173 n--;
175 asm volatile ("bra 1f;"
176 "0:" /* loop start */
177 "movem.l (%[y]), %%d0-%%d3;" /* fetch values */
178 "movem.l %%d0-%%d3, (%[x]);" /* store */
179 "lea.l (4*4, %[x]), %[x];" /* advance */
180 "lea.l (4*4, %[y]), %[y];"
181 "subq.l #4, %[n];" /* done 4 elements */
182 "1: cmpi.l #4, %[n];"
183 "bge 0b;"
184 : [n] "+d" (n), [x] "+a" (x), [y] "+a" (y)
185 : : "%d0", "%d1", "%d2", "%d3", "cc", "memory");
186 /* copy final elements */
187 while (n>0) {
188 *x++ = *y++;
189 n--;
193 static inline
194 void vect_mult_fw(int32_t *data, int32_t *window, int n)
196 /* ensure data is aligned to 16-bytes */
197 while(n>0 && (int)data&15) {
198 *data = MULT31(*data, *window);
199 data++;
200 window++;
201 n--;
203 asm volatile ("movem.l (%[d]), %%d0-%%d3;" /* loop start */
204 "movem.l (%[w]), %%a0-%%a3;" /* pre-fetch registers */
205 "lea.l (4*4, %[w]), %[w];"
206 "bra 1f;" /* jump to loop condition */
207 "0:" /* loop body */
208 /* multiply and load next window values */
209 "mac.l %%d0, %%a0, (%[w])+, %%a0, %%acc0;"
210 "mac.l %%d1, %%a1, (%[w])+, %%a1, %%acc1;"
211 "mac.l %%d2, %%a2, (%[w])+, %%a2, %%acc2;"
212 "mac.l %%d3, %%a3, (%[w])+, %%a3, %%acc3;"
213 "movclr.l %%acc0, %%d0;" /* get the products */
214 "movclr.l %%acc1, %%d1;"
215 "movclr.l %%acc2, %%d2;"
216 "movclr.l %%acc3, %%d3;"
217 /* store and advance */
218 "movem.l %%d0-%%d3, (%[d]);"
219 "lea.l (4*4, %[d]), %[d];"
220 "movem.l (%[d]), %%d0-%%d3;"
221 "subq.l #4, %[n];" /* done 4 elements */
222 "1: cmpi.l #4, %[n];"
223 "bge 0b;"
224 /* multiply final elements */
225 "tst.l %[n];"
226 "beq 1f;" /* n=0 */
227 "mac.l %%d0, %%a0, %%acc0;"
228 "movclr.l %%acc0, %%d0;"
229 "move.l %%d0, (%[d])+;"
230 "subq.l #1, %[n];"
231 "beq 1f;" /* n=1 */
232 "mac.l %%d1, %%a1, %%acc0;"
233 "movclr.l %%acc0, %%d1;"
234 "move.l %%d1, (%[d])+;"
235 "subq.l #1, %[n];"
236 "beq 1f;" /* n=2 */
237 /* otherwise n = 3 */
238 "mac.l %%d2, %%a2, %%acc0;"
239 "movclr.l %%acc0, %%d2;"
240 "move.l %%d2, (%[d])+;"
241 "1:"
242 : [n] "+d" (n), [d] "+a" (data), [w] "+a" (window)
243 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
244 "cc", "memory");
247 static inline
248 void vect_mult_bw(int32_t *data, int32_t *window, int n)
250 /* ensure at least data is aligned to 16-bytes */
251 while(n>0 && (int)data&15) {
252 *data = MULT31(*data, *window);
253 data++;
254 window--;
255 n--;
257 asm volatile ("lea.l (-3*4, %[w]), %[w];" /* loop start */
258 "movem.l (%[d]), %%d0-%%d3;" /* pre-fetch registers */
259 "movem.l (%[w]), %%a0-%%a3;"
260 "bra 1f;" /* jump to loop condition */
261 "0:" /* loop body */
262 /* multiply and load next window value */
263 "mac.l %%d0, %%a3, -(%[w]), %%a3, %%acc0;"
264 "mac.l %%d1, %%a2, -(%[w]), %%a2, %%acc1;"
265 "mac.l %%d2, %%a1, -(%[w]), %%a1, %%acc2;"
266 "mac.l %%d3, %%a0, -(%[w]), %%a0, %%acc3;"
267 "movclr.l %%acc0, %%d0;" /* get the products */
268 "movclr.l %%acc1, %%d1;"
269 "movclr.l %%acc2, %%d2;"
270 "movclr.l %%acc3, %%d3;"
271 /* store and advance */
272 "movem.l %%d0-%%d3, (%[d]);"
273 "lea.l (4*4, %[d]), %[d];"
274 "movem.l (%[d]), %%d0-%%d3;"
275 "subq.l #4, %[n];" /* done 4 elements */
276 "1: cmpi.l #4, %[n];"
277 "bge 0b;"
278 /* multiply final elements */
279 "tst.l %[n];"
280 "beq 1f;" /* n=0 */
281 "mac.l %%d0, %%a3, %%acc0;"
282 "movclr.l %%acc0, %%d0;"
283 "move.l %%d0, (%[d])+;"
284 "subq.l #1, %[n];"
285 "beq 1f;" /* n=1 */
286 "mac.l %%d1, %%a2, %%acc0;"
287 "movclr.l %%acc0, %%d1;"
288 "move.l %%d1, (%[d])+;"
289 "subq.l #1, %[n];"
290 "beq 1f;" /* n=2 */
291 /* otherwise n = 3 */
292 "mac.l %%d2, %%a1, %%acc0;"
293 "movclr.l %%acc0, %%d2;"
294 "move.l %%d2, (%[d])+;"
295 "1:"
296 : [n] "+d" (n), [d] "+a" (data), [w] "+a" (window)
297 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
298 "cc", "memory");
301 #endif
303 #endif
305 #ifndef _V_CLIP_MATH
306 #define _V_CLIP_MATH
308 /* this is portable C and simple; why not use this as default? */
309 static inline int32_t CLIP_TO_15(register int32_t x) {
310 register int32_t hi=32767, lo=-32768;
311 return (x>=hi ? hi : (x<=lo ? lo : x));
314 #endif
315 #else
316 #define LINE_ATTR
317 #endif