1/* Copyright (C) 2006 David Rowe */
2/**
3   @file quant_lsp_bfin.h
4   @author David Rowe
5   @brief Various compatibility routines for Speex (Blackfin version)
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#define OVERRIDE_LSP_QUANT
37#ifdef OVERRIDE_LSP_QUANT
38
39/*
40  Note http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
41  well tell you all the magic resgister constraints used below
42  for gcc in-line asm.
43*/
44
45static int lsp_quant(
46  spx_word16_t      *x,
47  const signed char *cdbk,
48  int                nbVec,
49  int                nbDim
50)
51{
52   int          j;
53   spx_word32_t best_dist=1<<30;
54   int          best_id=0;
55
56   __asm__ __volatile__
57     (
58"	%0 = 1 (X);\n\t"                       /* %0: best_dist */
59"	%0 <<= 30;\n\t"
60"	%1 = 0 (X);\n\t"                       /* %1: best_i         */
61"       P2 = %3\n\t"                           /* P2: ptr to cdbk    */
62"       R5 = 0;\n\t"                           /* R5: best cb entry  */
63
64"       R0 = %5;\n\t"                          /* set up circ addr   */
65"       R0 <<= 1;\n\t"
66"       L0 = R0;\n\t"
67"       I0 = %2;\n\t"                          /* %2: &x[0]          */
68"       B0 = %2;\n\t"
69
70"       R2.L = W [I0++];\n\t"
71"	LSETUP (1f, 2f) LC0 = %4;\n\t"
72"1:	  R3 = 0;\n\t"                         /* R3: dist           */
73"	  LSETUP (3f, 4f) LC1 = %5;\n\t"
74"3:       R1 = B [P2++] (X);\n\t"
75"	    R1 <<= 5;\n\t"
76"	    R0.L = R2.L - R1.L || R2.L = W [I0++];\n\t"
77"	    R0 = R0.L*R0.L;\n\t"
78"4:	    R3 = R3 + R0;\n\t"
79
80"	  cc =R3<%0;\n\t"
81"	  if cc %0=R3;\n\t"
82"	  if cc %1=R5;\n\t"
83"2:     R5 += 1;\n\t"
84"         L0 = 0;\n\t"
85   : "=&d" (best_dist), "=&d" (best_id)
86   : "a" (x), "b" (cdbk), "a" (nbVec), "a" (nbDim)
87   : "I0", "P2", "R0", "R1", "R2", "R3", "R5", "L0", "B0", "A0"
88   );
89
90   for (j=0;j<nbDim;j++) {
91      x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
92   }
93   return best_id;
94}
95#endif
96
97#define OVERRIDE_LSP_WEIGHT_QUANT
98#ifdef OVERRIDE_LSP_WEIGHT_QUANT
99
100/*
101  Note http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
102  well tell you all the magic resgister constraints used below
103  for gcc in-line asm.
104*/
105
106static int lsp_weight_quant(
107  spx_word16_t      *x,
108  spx_word16_t      *weight,
109  const signed char *cdbk,
110  int                nbVec,
111  int                nbDim
112)
113{
114   int          j;
115   spx_word32_t best_dist=1<<30;
116   int          best_id=0;
117
118   __asm__ __volatile__
119     (
120"	%0 = 1 (X);\n\t"                       /* %0: best_dist */
121"	%0 <<= 30;\n\t"
122"	%1 = 0 (X);\n\t"                       /* %1: best_i         */
123"       P2 = %4\n\t"                           /* P2: ptr to cdbk    */
124"       R5 = 0;\n\t"                           /* R5: best cb entry  */
125
126"       R0 = %6;\n\t"                          /* set up circ addr   */
127"       R0 <<= 1;\n\t"
128"       L0 = R0;\n\t"
129"       L1 = R0;\n\t"
130"       I0 = %2;\n\t"                          /* %2: &x[0]          */
131"	I1 = %3;\n\t"                          /* %3: &weight[0]     */
132"       B0 = %2;\n\t"
133"	B1 = %3;\n\t"
134
135"	LSETUP (1f, 2f) LC0 = %5;\n\t"
136"1:	  R3 = 0 (X);\n\t"                     /* R3: dist           */
137"	  LSETUP (3f, 4f) LC1 = %6;\n\t"
138"3:	    R0.L = W [I0++] || R2.L = W [I1++];\n\t"
139"           R1 = B [P2++] (X);\n\t"
140"	    R1 <<= 5;\n\t"
141"	    R0.L = R0.L - R1.L;\n\t"
142"           R0 = R0.L*R0.L;\n\t"
143"	    A1 = R2.L*R0.L (M,IS);\n\t"
144"	    A1 = A1 >>> 16;\n\t"
145"	    R1 = (A1 += R2.L*R0.H) (IS);\n\t"
146"4:	    R3 = R3 + R1;\n\t"
147
148"	  cc =R3<%0;\n\t"
149"	  if cc %0=R3;\n\t"
150"	  if cc %1=R5;\n\t"
151"2:    R5 += 1;\n\t"
152"         L0 = 0;\n\t"
153"         L1 = 0;\n\t"
154   : "=&d" (best_dist), "=&d" (best_id)
155   : "a" (x), "a" (weight), "b" (cdbk), "a" (nbVec), "a" (nbDim)
156   : "I0", "I1", "P2", "R0", "R1", "R2", "R3", "R5", "A1",
157     "L0", "L1", "B0", "B1"
158   );
159
160   for (j=0;j<nbDim;j++) {
161      x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
162   }
163   return best_id;
164}
165#endif
166