1 /* Copyright (C) 2005 Analog Devices
2    Author: Jean-Marc Valin */
3 /**
4    @file fixed_bfin.h
5    @brief Blackfin fixed-point operations
6 */
7 /*
8    Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions
10    are met:
11 
12    - Redistributions of source code must retain the above copyright
13    notice, this list of conditions and the following disclaimer.
14 
15    - Redistributions in binary form must reproduce the above copyright
16    notice, this list of conditions and the following disclaimer in the
17    documentation and/or other materials provided with the distribution.
18 
19    - Neither the name of the Xiph.org Foundation nor the names of its
20    contributors may be used to endorse or promote products derived from
21    this software without specific prior written permission.
22 
23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 
36 #ifndef FIXED_BFIN_H
37 #define FIXED_BFIN_H
38 
39 #undef PDIV32_16
PDIV32_16(spx_word32_t a,spx_word16_t b)40 static inline spx_word16_t PDIV32_16(spx_word32_t a, spx_word16_t b)
41 {
42    spx_word32_t res, bb;
43    bb = b;
44    a += b>>1;
45    __asm__  (
46          "P0 = 15;\n\t"
47          "R0 = %1;\n\t"
48          "R1 = %2;\n\t"
49          //"R0 = R0 + R1;\n\t"
50          "R0 <<= 1;\n\t"
51          "DIVS (R0, R1);\n\t"
52          "LOOP divide%= LC0 = P0;\n\t"
53          "LOOP_BEGIN divide%=;\n\t"
54             "DIVQ (R0, R1);\n\t"
55          "LOOP_END divide%=;\n\t"
56          "R0 = R0.L;\n\t"
57          "%0 = R0;\n\t"
58    : "=m" (res)
59    : "m" (a), "m" (bb)
60    : "P0", "R0", "R1", "cc");
61    return res;
62 }
63 
64 #undef DIV32_16
DIV32_16(spx_word32_t a,spx_word16_t b)65 static inline spx_word16_t DIV32_16(spx_word32_t a, spx_word16_t b)
66 {
67    spx_word32_t res, bb;
68    bb = b;
69    /* Make the roundinf consistent with the C version
70       (do we need to do that?)*/
71    if (a<0)
72       a += (b-1);
73    __asm__  (
74          "P0 = 15;\n\t"
75          "R0 = %1;\n\t"
76          "R1 = %2;\n\t"
77          "R0 <<= 1;\n\t"
78          "DIVS (R0, R1);\n\t"
79          "LOOP divide%= LC0 = P0;\n\t"
80          "LOOP_BEGIN divide%=;\n\t"
81             "DIVQ (R0, R1);\n\t"
82          "LOOP_END divide%=;\n\t"
83          "R0 = R0.L;\n\t"
84          "%0 = R0;\n\t"
85    : "=m" (res)
86    : "m" (a), "m" (bb)
87    : "P0", "R0", "R1", "cc");
88    return res;
89 }
90 
91 #undef MAX16
MAX16(spx_word16_t a,spx_word16_t b)92 static inline spx_word16_t MAX16(spx_word16_t a, spx_word16_t b)
93 {
94    spx_word32_t res;
95    __asm__  (
96          "%1 = %1.L (X);\n\t"
97          "%2 = %2.L (X);\n\t"
98          "%0 = MAX(%1,%2);"
99    : "=d" (res)
100    : "%d" (a), "d" (b)
101    );
102    return res;
103 }
104 
105 #undef MULT16_32_Q15
MULT16_32_Q15(spx_word16_t a,spx_word32_t b)106 static inline spx_word32_t MULT16_32_Q15(spx_word16_t a, spx_word32_t b)
107 {
108    spx_word32_t res;
109    __asm__
110    (
111          "A1 = %2.L*%1.L (M);\n\t"
112          "A1 = A1 >>> 15;\n\t"
113          "%0 = (A1 += %2.L*%1.H) ;\n\t"
114    : "=&W" (res), "=&d" (b)
115    : "d" (a), "1" (b)
116    : "A1"
117    );
118    return res;
119 }
120 
121 #undef MAC16_32_Q15
MAC16_32_Q15(spx_word32_t c,spx_word16_t a,spx_word32_t b)122 static inline spx_word32_t MAC16_32_Q15(spx_word32_t c, spx_word16_t a, spx_word32_t b)
123 {
124    spx_word32_t res;
125    __asm__
126          (
127          "A1 = %2.L*%1.L (M);\n\t"
128          "A1 = A1 >>> 15;\n\t"
129          "%0 = (A1 += %2.L*%1.H);\n\t"
130          "%0 = %0 + %4;\n\t"
131    : "=&W" (res), "=&d" (b)
132    : "d" (a), "1" (b), "d" (c)
133    : "A1"
134          );
135    return res;
136 }
137 
138 #undef MULT16_32_Q14
MULT16_32_Q14(spx_word16_t a,spx_word32_t b)139 static inline spx_word32_t MULT16_32_Q14(spx_word16_t a, spx_word32_t b)
140 {
141    spx_word32_t res;
142    __asm__
143          (
144          "%2 <<= 1;\n\t"
145          "A1 = %1.L*%2.L (M);\n\t"
146          "A1 = A1 >>> 15;\n\t"
147          "%0 = (A1 += %1.L*%2.H);\n\t"
148    : "=W" (res), "=d" (a), "=d" (b)
149    : "1" (a), "2" (b)
150    : "A1"
151          );
152    return res;
153 }
154 
155 #undef MAC16_32_Q14
MAC16_32_Q14(spx_word32_t c,spx_word16_t a,spx_word32_t b)156 static inline spx_word32_t MAC16_32_Q14(spx_word32_t c, spx_word16_t a, spx_word32_t b)
157 {
158    spx_word32_t res;
159    __asm__
160          (
161          "%1 <<= 1;\n\t"
162          "A1 = %2.L*%1.L (M);\n\t"
163          "A1 = A1 >>> 15;\n\t"
164          "%0 = (A1 += %2.L*%1.H);\n\t"
165          "%0 = %0 + %4;\n\t"
166    : "=&W" (res), "=&d" (b)
167    : "d" (a), "1" (b), "d" (c)
168    : "A1"
169          );
170    return res;
171 }
172 
173 #endif
174