1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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)))
32 static inline int32_t MULT32(int32_t x
, int32_t y
) {
34 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply & shift */
35 "movclr.l %%acc0, %[x];" /* move & clear acc */
36 "asr.l #1, %[x];" /* no overflow test */
43 static inline int32_t MULT31(int32_t x
, int32_t y
) {
44 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply */
45 "movclr.l %%acc0, %[x];" /* move and clear */
52 static inline int32_t MULT31_SHIFT15(int32_t x
, int32_t y
) {
55 asm volatile ("mac.l %[x], %[y], %%acc0;" /* multiply */
56 "mulu.l %[y], %[x];" /* get lower half, avoid emac stall */
57 "movclr.l %%acc0, %[r];" /* get higher half */
58 "asl.l #8, %[r];" /* hi<<16, plus one free */
60 "lsr.l #8, %[x];" /* (unsigned)lo >> 15 */
62 "or.l %[x], %[r];" /* logical-or results */
63 : [r
] "=&d" (r
), [x
] "+d" (x
)
70 void XPROD31(int32_t a
, int32_t b
,
72 int32_t *x
, int32_t *y
)
74 asm volatile ("mac.l %[a], %[t], %%acc0;"
75 "mac.l %[b], %[v], %%acc0;"
76 "mac.l %[b], %[t], %%acc1;"
77 "msac.l %[a], %[v], %%acc1;"
78 "movclr.l %%acc0, %[a];"
79 "move.l %[a], (%[x]);"
80 "movclr.l %%acc1, %[a];"
81 "move.l %[a], (%[y]);"
83 : [x
] "a" (x
), [y
] "a" (y
),
84 [b
] "r" (b
), [t
] "r" (t
), [v
] "r" (v
)
89 void XNPROD31(int32_t a
, int32_t b
,
91 int32_t *x
, int32_t *y
)
93 asm volatile ("mac.l %[a], %[t], %%acc0;"
94 "msac.l %[b], %[v], %%acc0;"
95 "mac.l %[b], %[t], %%acc1;"
96 "mac.l %[a], %[v], %%acc1;"
97 "movclr.l %%acc0, %[a];"
98 "move.l %[a], (%[x]);"
99 "movclr.l %%acc1, %[a];"
100 "move.l %[a], (%[y]);"
102 : [x
] "a" (x
), [y
] "a" (y
),
103 [b
] "r" (b
), [t
] "r" (t
), [v
] "r" (v
)
107 #if 0 /* canonical Tremor definition */
108 #define XPROD32(_a, _b, _t, _v, _x, _y) \
109 { (_x)=MULT32(_a,_t)+MULT32(_b,_v); \
110 (_y)=MULT32(_b,_t)-MULT32(_a,_v); }
113 /* this could lose the LSB by overflow, but i don't think it'll ever happen.
114 if anyone think they can hear a bug caused by this, please try the above
116 #define XPROD32(_a, _b, _t, _v, _x, _y) \
117 asm volatile ("mac.l %[a], %[t], %%acc0;" \
118 "mac.l %[b], %[v], %%acc0;" \
119 "mac.l %[b], %[t], %%acc1;" \
120 "msac.l %[a], %[v], %%acc1;" \
121 "movclr.l %%acc0, %[x];" \
123 "movclr.l %%acc1, %[y];" \
125 : [x] "=&d" (_x), [y] "=&d" (_y) \
126 : [a] "r" (_a), [b] "r" (_b), \
127 [t] "r" (_t), [v] "r" (_v) \
133 /* asm versions of vector operations for block.c, window.c */
134 /* assumes MAC is initialized & accumulators cleared */
136 void vect_add(int32_t *x
, const int32_t *y
, int n
)
138 /* align to 16 bytes */
139 while(n
>0 && (int)x
&15) {
143 asm volatile ("bra 1f;"
144 "0:" /* loop start */
145 "movem.l (%[x]), %%d0-%%d3;" /* fetch values */
146 "movem.l (%[y]), %%a0-%%a3;"
152 /* store and advance */
153 "movem.l %%d0-%%d3, (%[x]);"
154 "lea.l (4*4, %[x]), %[x];"
155 "lea.l (4*4, %[y]), %[y];"
156 "subq.l #4, %[n];" /* done 4 elements */
157 "1: cmpi.l #4, %[n];"
159 : [n
] "+d" (n
), [x
] "+a" (x
), [y
] "+a" (y
)
160 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
162 /* add final elements */
170 void vect_copy(int32_t *x
, int32_t *y
, int n
)
172 /* align to 16 bytes */
173 while(n
>0 && (int)x
&15) {
177 asm volatile ("bra 1f;"
178 "0:" /* loop start */
179 "movem.l (%[y]), %%d0-%%d3;" /* fetch values */
180 "movem.l %%d0-%%d3, (%[x]);" /* store */
181 "lea.l (4*4, %[x]), %[x];" /* advance */
182 "lea.l (4*4, %[y]), %[y];"
183 "subq.l #4, %[n];" /* done 4 elements */
184 "1: cmpi.l #4, %[n];"
186 : [n
] "+d" (n
), [x
] "+a" (x
), [y
] "+a" (y
)
187 : : "%d0", "%d1", "%d2", "%d3", "cc", "memory");
188 /* copy final elements */
196 void vect_mult_fw(int32_t *data
, int32_t *window
, int n
)
198 /* ensure data is aligned to 16-bytes */
199 while(n
>0 && (int)data
&15) {
200 *data
= MULT31(*data
, *window
);
205 asm volatile ("movem.l (%[d]), %%d0-%%d3;" /* loop start */
206 "movem.l (%[w]), %%a0-%%a3;" /* pre-fetch registers */
207 "lea.l (4*4, %[w]), %[w];"
208 "bra 1f;" /* jump to loop condition */
210 /* multiply and load next window values */
211 "mac.l %%d0, %%a0, (%[w])+, %%a0, %%acc0;"
212 "mac.l %%d1, %%a1, (%[w])+, %%a1, %%acc1;"
213 "mac.l %%d2, %%a2, (%[w])+, %%a2, %%acc2;"
214 "mac.l %%d3, %%a3, (%[w])+, %%a3, %%acc3;"
215 "movclr.l %%acc0, %%d0;" /* get the products */
216 "movclr.l %%acc1, %%d1;"
217 "movclr.l %%acc2, %%d2;"
218 "movclr.l %%acc3, %%d3;"
219 /* store and advance */
220 "movem.l %%d0-%%d3, (%[d]);"
221 "lea.l (4*4, %[d]), %[d];"
222 "movem.l (%[d]), %%d0-%%d3;"
223 "subq.l #4, %[n];" /* done 4 elements */
224 "1: cmpi.l #4, %[n];"
226 /* multiply final elements */
229 "mac.l %%d0, %%a0, %%acc0;"
230 "movclr.l %%acc0, %%d0;"
231 "move.l %%d0, (%[d])+;"
234 "mac.l %%d1, %%a1, %%acc0;"
235 "movclr.l %%acc0, %%d1;"
236 "move.l %%d1, (%[d])+;"
239 /* otherwise n = 3 */
240 "mac.l %%d2, %%a2, %%acc0;"
241 "movclr.l %%acc0, %%d2;"
242 "move.l %%d2, (%[d])+;"
244 : [n
] "+d" (n
), [d
] "+a" (data
), [w
] "+a" (window
)
245 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
250 void vect_mult_bw(int32_t *data
, int32_t *window
, int n
)
252 /* ensure at least data is aligned to 16-bytes */
253 while(n
>0 && (int)data
&15) {
254 *data
= MULT31(*data
, *window
);
259 asm volatile ("lea.l (-3*4, %[w]), %[w];" /* loop start */
260 "movem.l (%[d]), %%d0-%%d3;" /* pre-fetch registers */
261 "movem.l (%[w]), %%a0-%%a3;"
262 "bra 1f;" /* jump to loop condition */
264 /* multiply and load next window value */
265 "mac.l %%d0, %%a3, -(%[w]), %%a3, %%acc0;"
266 "mac.l %%d1, %%a2, -(%[w]), %%a2, %%acc1;"
267 "mac.l %%d2, %%a1, -(%[w]), %%a1, %%acc2;"
268 "mac.l %%d3, %%a0, -(%[w]), %%a0, %%acc3;"
269 "movclr.l %%acc0, %%d0;" /* get the products */
270 "movclr.l %%acc1, %%d1;"
271 "movclr.l %%acc2, %%d2;"
272 "movclr.l %%acc3, %%d3;"
273 /* store and advance */
274 "movem.l %%d0-%%d3, (%[d]);"
275 "lea.l (4*4, %[d]), %[d];"
276 "movem.l (%[d]), %%d0-%%d3;"
277 "subq.l #4, %[n];" /* done 4 elements */
278 "1: cmpi.l #4, %[n];"
280 /* multiply final elements */
283 "mac.l %%d0, %%a3, %%acc0;"
284 "movclr.l %%acc0, %%d0;"
285 "move.l %%d0, (%[d])+;"
288 "mac.l %%d1, %%a2, %%acc0;"
289 "movclr.l %%acc0, %%d1;"
290 "move.l %%d1, (%[d])+;"
293 /* otherwise n = 3 */
294 "mac.l %%d2, %%a1, %%acc0;"
295 "movclr.l %%acc0, %%d2;"
296 "move.l %%d2, (%[d])+;"
298 : [n
] "+d" (n
), [d
] "+a" (data
), [w
] "+a" (window
)
299 : : "%d0", "%d1", "%d2", "%d3", "%a0", "%a1", "%a2", "%a3",
310 /* this is portable C and simple; why not use this as default? */
311 static inline int32_t CLIP_TO_15(register int32_t x
) {
312 register int32_t hi
=32767, lo
=-32768;
313 return (x
>=hi
? hi
: (x
<=lo
? lo
: x
));