summaryrefslogtreecommitdiffstats
path: root/libao2/fir.h
blob: 9046e3dc0f9ce334824d807e487324e69528d9ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*=============================================================================
//	
//  This software has been released under the terms of the GNU Public
//  license. See http://www.gnu.org/copyleft/gpl.html for details.
//
//  Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au
//
//=============================================================================
*/

#ifndef __FIR_H__
#define __FIR_H__

/* 4, 8 and 16 tap FIR filters implemented using SSE instructions 
   int16_t* x Input data
   int16_t* y Output value
   int16_t* w Filter weights 
   
   C function
   for(int i = 0 ; i < L ; i++)
     *y += w[i]*x[i];
*/

#ifdef HAVE_SSE

// This block should be MMX only compatible, but it isn't...
#ifdef L4
#define LOAD_QUE(x) \
        __asm __volatile("movq %0, %%mm2\n\t" \
                         :                    \
                         :"m"((x)[0])         \
                         :"memory");
#define SAVE_QUE(x) \
        __asm __volatile("movq %%mm2, %0\n\t" \
                         :"=m"(x[0])          \
                         :                    \
                         :"memory");
#define UPDATE_QUE(in) \
        __asm __volatile("psllq   $16,   %%mm2\n\t"    \
                         "pinsrw  $0,    %0,%%mm2\n\t" \
                          :                            \
                          :"m" ((in)[0])               \
                          :"memory");                  
#define FIR(x,w,y) \
        __asm __volatile("movq	  %%mm2, %%mm0\n\t" \
                         "pmaddwd %1,    %%mm0\n\t" \
                         "movq    %%mm0, %%mm1\n\t" \
                         "psrlq   $32, 	 %%mm1\n\t" \
                         "paddd   %%mm0, %%mm1\n\t" \
                         "movd    %%mm1, %%esi\n\t" \
                         "shrl    $16,   %%esi\n\t" \
                         "movw    %%si,  %0\n\t"    \
			 : "=m" ((y)[0])            \
			 : "m" ((w)[0])             \
			 : "memory", "%esi"); 
#endif /* L4 */

// It is possible to make the 8 bit filter a lot faster by using the
// 128 bit registers, feel free to optimize.
#ifdef L8
#define LOAD_QUE(x) \
        __asm __volatile("movq %0, %%mm5\n\t" \
                         "movq %1, %%mm4\n\t" \
                         :                    \
                         :"m"((x)[0]),        \
                          "m"((x)[4])         \
                         :"memory");
#define SAVE_QUE(x) \
        __asm __volatile("movq %%mm5, %0\n\t" \
                         "movq %%mm4, %1\n\t" \
                         :"=m"((x)[0]),       \
                          "=m"((x)[4])        \
                         :                    \
                         :"memory");

// Below operation could replace line 2 to 5 in macro below but can
// not cause of compiler bug ???
// "pextrw $3, %%mm5,%%eax\n\t"
#define UPDATE_QUE(in) \
        __asm __volatile("psllq    $16,   %%mm4\n\t"        \
                         "movq	   %%mm5, %%mm0\n\t" 	    \
                         "psrlq    $48,   %%mm0\n\t"        \
                         "movd     %%mm0, %%eax\n\t"        \
			 "pinsrw   $0,    %%eax,%%mm4\n\t"  \
                         "psllq    $16,   %%mm5\n\t"        \
                         "pinsrw   $0,    %0,%%mm5\n\t"     \
                          :                                 \
                          :"m" ((in)[0])                    \
                          :"memory", "%eax");                  
#define FIR(x,w,y) \
        __asm __volatile("movq	  %%mm5, %%mm0\n\t" \
                         "pmaddwd %1,    %%mm0\n\t" \
                         "movq	  %%mm4, %%mm1\n\t" \
                         "pmaddwd %2,    %%mm1\n\t" \
                         "paddd   %%mm1, %%mm0\n\t" \
                         "movq    %%mm0, %%mm1\n\t" \
                         "psrlq   $32, 	 %%mm1\n\t" \
                         "paddd   %%mm0, %%mm1\n\t" \
                         "movd    %%mm1, %%esi\n\t" \
                         "shrl    $16,   %%esi\n\t" \
                         "movw    %%si,  %0\n\t"    \
			 : "=m" ((y)[0])            \
			 : "m" ((w)[0]),            \
			   "m" ((w)[4])             \
			 : "memory", "%esi"); 
#endif /* L8 */

#else /* HAVE_SSE */

#define LOAD_QUE(x)
#define SAVE_QUE(x)
#define UPDATE_QUE(inm) \
  xi=(--xi)&(L-1);     \
  x[xi]=x[xi+L]=*inm;

#ifdef L4
#define FIR(x,w,y) \
        y[0]=(w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3]) >> 16;
#else
#define FIR(x,w,y){ \
  int16_t a = (w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3]) >> 16; \
  int16_t b = (w[4]*x[4]+w[5]*x[5]+w[6]*x[6]+w[7]*x[7]) >> 16; \
  y[0]      = a+b; \
}
#endif /* L4 */

#endif /* HAVE_SSE */

#endif /* __FIR_H__ */