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#include "bfin.h"
40
41#undef PDIV32_16
42static inline spx_word16_t PDIV32_16(spx_word32_t a, spx_word16_t b)
43{
44   spx_word32_t res, bb;
45   bb = b;
46   a += b>>1;
47   __asm__  (
48         "P0 = 15;\n\t"
49         "R0 = %1;\n\t"
50         "R1 = %2;\n\t"
51         //"R0 = R0 + R1;\n\t"
52         "R0 <<= 1;\n\t"
53         "DIVS (R0, R1);\n\t"
54         "LOOP divide%= LC0 = P0;\n\t"
55         "LOOP_BEGIN divide%=;\n\t"
56            "DIVQ (R0, R1);\n\t"
57         "LOOP_END divide%=;\n\t"
58         "R0 = R0.L;\n\t"
59         "%0 = R0;\n\t"
60   : "=m" (res)
61   : "m" (a), "m" (bb)
62   : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS);
63   return res;
64}
65
66#undef DIV32_16
67static inline spx_word16_t DIV32_16(spx_word32_t a, spx_word16_t b)
68{
69   spx_word32_t res, bb;
70   bb = b;
71   /* Make the roundinf consistent with the C version
72      (do we need to do that?)*/
73   if (a<0)
74      a += (b-1);
75   __asm__  (
76         "P0 = 15;\n\t"
77         "R0 = %1;\n\t"
78         "R1 = %2;\n\t"
79         "R0 <<= 1;\n\t"
80         "DIVS (R0, R1);\n\t"
81         "LOOP divide%= LC0 = P0;\n\t"
82         "LOOP_BEGIN divide%=;\n\t"
83            "DIVQ (R0, R1);\n\t"
84         "LOOP_END divide%=;\n\t"
85         "R0 = R0.L;\n\t"
86         "%0 = R0;\n\t"
87   : "=m" (res)
88   : "m" (a), "m" (bb)
89   : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS);
90   return res;
91}
92
93#undef MAX16
94static inline spx_word16_t MAX16(spx_word16_t a, spx_word16_t b)
95{
96   spx_word32_t res;
97   __asm__  (
98         "%1 = %1.L (X);\n\t"
99         "%2 = %2.L (X);\n\t"
100         "%0 = MAX(%1,%2);"
101   : "=d" (res)
102   : "%d" (a), "d" (b)
103   : "ASTAT"
104   );
105   return res;
106}
107
108#undef MULT16_32_Q15
109static inline spx_word32_t MULT16_32_Q15(spx_word16_t a, spx_word32_t b)
110{
111   spx_word32_t res;
112   __asm__
113   (
114         "A1 = %2.L*%1.L (M);\n\t"
115         "A1 = A1 >>> 15;\n\t"
116         "%0 = (A1 += %2.L*%1.H) ;\n\t"
117   : "=&W" (res), "=&d" (b)
118   : "d" (a), "1" (b)
119   : "A1", "ASTAT"
120   );
121   return res;
122}
123
124#undef MAC16_32_Q15
125static inline spx_word32_t MAC16_32_Q15(spx_word32_t c, spx_word16_t a, spx_word32_t b)
126{
127   spx_word32_t res;
128   __asm__
129         (
130         "A1 = %2.L*%1.L (M);\n\t"
131         "A1 = A1 >>> 15;\n\t"
132         "%0 = (A1 += %2.L*%1.H);\n\t"
133         "%0 = %0 + %4;\n\t"
134   : "=&W" (res), "=&d" (b)
135   : "d" (a), "1" (b), "d" (c)
136   : "A1", "ASTAT"
137         );
138   return res;
139}
140
141#undef MULT16_32_Q14
142static inline spx_word32_t MULT16_32_Q14(spx_word16_t a, spx_word32_t b)
143{
144   spx_word32_t res;
145   __asm__
146         (
147         "%2 <<= 1;\n\t"
148         "A1 = %1.L*%2.L (M);\n\t"
149         "A1 = A1 >>> 15;\n\t"
150         "%0 = (A1 += %1.L*%2.H);\n\t"
151   : "=W" (res), "=d" (a), "=d" (b)
152   : "1" (a), "2" (b)
153   : "A1", "ASTAT"
154         );
155   return res;
156}
157
158#undef MAC16_32_Q14
159static inline spx_word32_t MAC16_32_Q14(spx_word32_t c, spx_word16_t a, spx_word32_t b)
160{
161   spx_word32_t res;
162   __asm__
163         (
164         "%1 <<= 1;\n\t"
165         "A1 = %2.L*%1.L (M);\n\t"
166         "A1 = A1 >>> 15;\n\t"
167         "%0 = (A1 += %2.L*%1.H);\n\t"
168         "%0 = %0 + %4;\n\t"
169   : "=&W" (res), "=&d" (b)
170   : "d" (a), "1" (b), "d" (c)
171   : "A1", "ASTAT"
172         );
173   return res;
174}
175
176#endif
177