1 /* Copyright (C) 2005 Analog Devices */
2 /**
3    @file lpc_bfin.h
4    @author Jean-Marc Valin
5    @brief Functions for LPC (Linear Prediction Coefficients) analysis (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 #include "bfin.h"
37 
38 #define OVERRIDE_SPEEX_AUTOCORR
_spx_autocorr(const spx_word16_t * x,spx_word16_t * ac,int lag,int n)39 void _spx_autocorr(
40 const spx_word16_t *x,   /*  in: [0...n-1] samples x   */
41 spx_word16_t       *ac,  /* out: [0...lag-1] ac values */
42 int          lag,
43 int          n
44                   )
45 {
46    spx_word32_t d;
47    const spx_word16_t *xs;
48    int i, j;
49    spx_word32_t ac0=1;
50    spx_word32_t ac32[11], *ac32top;
51    int shift, ac_shift;
52    ac32top = ac32+lag-1;
53    int lag_1, N_lag;
54    int nshift;
55    lag_1 = lag-1;
56    N_lag = n-lag_1;
57    for (j=0;j<n;j++)
58       ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
59    ac0 = ADD32(ac0,n);
60    shift = 8;
61    while (shift && ac0<0x40000000)
62    {
63       shift--;
64       ac0 <<= 1;
65    }
66    ac_shift = 18;
67    while (ac_shift && ac0<0x40000000)
68    {
69       ac_shift--;
70       ac0 <<= 1;
71    }
72 
73    xs = x+lag-1;
74    nshift = -shift;
75    __asm__ __volatile__
76    (
77          "P2 = %0;\n\t"
78          "I0 = P2;\n\t" /* x in I0 */
79          "B0 = P2;\n\t" /* x in B0 */
80          "R0 = %3;\n\t" /* len in R0 */
81          "P3 = %3;\n\t" /* len in R0 */
82          "P4 = %4;\n\t" /* nb_pitch in R0 */
83          "R1 = R0 << 1;\n\t" /* number of bytes in x */
84          "L0 = R1;\n\t"
85          "P0 = %1;\n\t"
86          "P1 = %2;\n\t"
87          "B1 = P1;\n\t"
88          "R4 = %5;\n\t"
89          "L1 = 0;\n\t" /*Disable looping on I1*/
90 
91          "r0 = [I0++];\n\t"
92          "R2 = 0;R3=0;"
93          "LOOP pitch%= LC0 = P4 >> 1;\n\t"
94          "LOOP_BEGIN pitch%=;\n\t"
95             "I1 = P0;\n\t"
96             "A1 = A0 = 0;\n\t"
97             "R1 = [I1++];\n\t"
98             "LOOP inner_prod%= LC1 = P3 >> 1;\n\t"
99             "LOOP_BEGIN inner_prod%=;\n\t"
100                "A1 += R0.L*R1.H, A0 += R0.L*R1.L (IS) || R1.L = W[I1++];\n\t"
101                "A1 += R0.H*R1.L, A0 += R0.H*R1.H (IS) || R1.H = W[I1++] || R0 = [I0++];\n\t"
102             "LOOP_END inner_prod%=;\n\t"
103             "A0 = ASHIFT A0 by R4.L;\n\t"
104             "A1 = ASHIFT A1 by R4.L;\n\t"
105 
106             "R2 = A0, R3 = A1;\n\t"
107             "[P1--] = R2;\n\t"
108             "[P1--] = R3;\n\t"
109             "P0 += 4;\n\t"
110          "LOOP_END pitch%=;\n\t"
111    : : "m" (xs), "m" (x), "m" (ac32top), "m" (N_lag), "m" (lag_1), "m" (nshift)
112    : "A0", "A1", "P0", "P1", "P2", "P3", "P4", "R0", "R1", "R2", "R3", "R4", "I0", "I1", "L0", "L1", "B0", "B1", "memory",
113      "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
114    );
115    d=0;
116    for (j=0;j<n;j++)
117    {
118       d = ADD32(d,SHR32(MULT16_16(x[j],x[j]), shift));
119    }
120    ac32[0] = d;
121 
122    for (i=0;i<lag;i++)
123    {
124       d=0;
125       for (j=i;j<lag_1;j++)
126       {
127          d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
128       }
129       if (i)
130          ac32[i] += d;
131       ac[i] = SHR32(ac32[i], ac_shift);
132    }
133 }
134 
135