## ffmpeg / libavcodec / x86 / fft_sse.c @ 5e7dfb7d

History | View | Annotate | Download (6.75 KB)

1 | bb6f5690 | Fabrice Bellard | ```
/*
``` |
---|---|---|---|

2 | ```
* FFT/MDCT transform with SSE optimizations
``` |
||

3 | 46803f4f | Loren Merritt | ```
* Copyright (c) 2008 Loren Merritt
``` |

4 | bb6f5690 | Fabrice Bellard | ```
*
``` |

5 | b78e7197 | Diego Biurrun | ```
* This file is part of FFmpeg.
``` |

6 | ```
*
``` |
||

7 | ```
* FFmpeg is free software; you can redistribute it and/or
``` |
||

8 | bb6f5690 | Fabrice Bellard | ```
* modify it under the terms of the GNU Lesser General Public
``` |

9 | ```
* License as published by the Free Software Foundation; either
``` |
||

10 | b78e7197 | Diego Biurrun | ```
* version 2.1 of the License, or (at your option) any later version.
``` |

11 | bb6f5690 | Fabrice Bellard | ```
*
``` |

12 | b78e7197 | Diego Biurrun | ```
* FFmpeg is distributed in the hope that it will be useful,
``` |

13 | bb6f5690 | Fabrice Bellard | ```
* but WITHOUT ANY WARRANTY; without even the implied warranty of
``` |

14 | ```
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
``` |
||

15 | ```
* Lesser General Public License for more details.
``` |
||

16 | ```
*
``` |
||

17 | ```
* You should have received a copy of the GNU Lesser General Public
``` |
||

18 | b78e7197 | Diego Biurrun | ```
* License along with FFmpeg; if not, write to the Free Software
``` |

19 | 5509bffa | Diego Biurrun | ```
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
``` |

20 | bb6f5690 | Fabrice Bellard | ```
*/
``` |

21 | 245976da | Diego Biurrun | |

22 | #include "libavutil/x86_cpu.h" |
||

23 | #include "libavcodec/dsputil.h" |
||

24 | f4863213 | Måns Rullgård | #include "fft.h" |

25 | bb6f5690 | Fabrice Bellard | |

26 | 74a841af | Ramiro Polla | DECLARE_ALIGNED(16, static const int, m1m1m1m1[4]) = |

27 | ebbafcb4 | Loren Merritt | { 1 << 31, 1 << 31, 1 << 31, 1 << 31 }; |

28 | |||

29 | 5d0ddd1a | Loren Merritt | void ff_fft_dispatch_sse(FFTComplex *z, int nbits); |

30 | void ff_fft_dispatch_interleave_sse(FFTComplex *z, int nbits); |
||

31 | bb6f5690 | Fabrice Bellard | |

32 | 68951ecf | Gildas Bazin | ```
void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
``` |

33 | bb6f5690 | Fabrice Bellard | { |

34 | 5d0ddd1a | Loren Merritt | int n = 1 << s->nbits; |

35 | |||

36 | ff_fft_dispatch_interleave_sse(z, s->nbits); |
||

37 | |||

38 | if(n <= 16) { |
||

39 | ```
x86_reg i = -8*n;
``` |
||

40 | be449fca | Diego Pettenò | ```
__asm__ volatile(
``` |

41 | 5d0ddd1a | Loren Merritt | ```
"1: \n"
``` |

42 | ```
"movaps (%0,%1), %%xmm0 \n"
``` |
||

43 | ```
"movaps %%xmm0, %%xmm1 \n"
``` |
||

44 | ```
"unpcklps 16(%0,%1), %%xmm0 \n"
``` |
||

45 | ```
"unpckhps 16(%0,%1), %%xmm1 \n"
``` |
||

46 | ```
"movaps %%xmm0, (%0,%1) \n"
``` |
||

47 | ```
"movaps %%xmm1, 16(%0,%1) \n"
``` |
||

48 | ```
"add $32, %0 \n"
``` |
||

49 | ```
"jl 1b \n"
``` |
||

50 | ```
:"+r"(i)
``` |
||

51 | ```
:"r"(z+n)
``` |
||

52 | ```
:"memory"
``` |
||

53 | ); |
||

54 | } |
||

55 | } |
||

56 | bb6f5690 | Fabrice Bellard | |

57 | 5d0ddd1a | Loren Merritt | ```
void ff_fft_permute_sse(FFTContext *s, FFTComplex *z)
``` |

58 | { |
||

59 | int n = 1 << s->nbits; |
||

60 | ```
int i;
``` |
||

61 | for(i=0; i<n; i+=2) { |
||

62 | be449fca | Diego Pettenò | ```
__asm__ volatile(
``` |

63 | 5d0ddd1a | Loren Merritt | ```
"movaps %2, %%xmm0 \n"
``` |

64 | ```
"movlps %%xmm0, %0 \n"
``` |
||

65 | ```
"movhps %%xmm0, %1 \n"
``` |
||

66 | ```
:"=m"(s->tmp_buf[s->revtab[i]]),
``` |
||

67 | "=m"(s->tmp_buf[s->revtab[i+1]]) |
||

68 | ```
:"m"(z[i])
``` |
||

69 | ); |
||

70 | } |
||

71 | ```
memcpy(z, s->tmp_buf, n*sizeof(FFTComplex));
``` |
||

72 | bb6f5690 | Fabrice Bellard | } |

73 | e1958604 | Fabrice Bellard | |

74 | 01b22147 | Måns Rullgård | void ff_imdct_half_sse(FFTContext *s, FFTSample *output, const FFTSample *input) |

75 | ebbafcb4 | Loren Merritt | { |

76 | 46803f4f | Loren Merritt | av_unused x86_reg i, j, k, l; |

77 | 01b22147 | Måns Rullgård | long n = 1 << s->mdct_bits; |

78 | 46803f4f | Loren Merritt | long n2 = n >> 1; |

79 | long n4 = n >> 2; |
||

80 | long n8 = n >> 3; |
||

81 | 01b22147 | Måns Rullgård | ```
const uint16_t *revtab = s->revtab + n8;
``` |

82 | ebbafcb4 | Loren Merritt | ```
const FFTSample *tcos = s->tcos;
``` |

83 | ```
const FFTSample *tsin = s->tsin;
``` |
||

84 | 46803f4f | Loren Merritt | FFTComplex *z = (FFTComplex *)output; |

85 | ebbafcb4 | Loren Merritt | |

86 | ```
/* pre rotation */
``` |
||

87 | 46803f4f | Loren Merritt | for(k=n8-2; k>=0; k-=2) { |

88 | be449fca | Diego Pettenò | ```
__asm__ volatile(
``` |

89 | 46803f4f | Loren Merritt | "movaps (%2,%1,2), %%xmm0 \n" // { z[k].re, z[k].im, z[k+1].re, z[k+1].im } |

90 | "movaps -16(%2,%0,2), %%xmm1 \n" // { z[-k-2].re, z[-k-2].im, z[-k-1].re, z[-k-1].im } |
||

91 | ```
"movaps %%xmm0, %%xmm2 \n"
``` |
||

92 | "shufps $0x88, %%xmm1, %%xmm0 \n" // { z[k].re, z[k+1].re, z[-k-2].re, z[-k-1].re } |
||

93 | "shufps $0x77, %%xmm2, %%xmm1 \n" // { z[-k-1].im, z[-k-2].im, z[k+1].im, z[k].im } |
||

94 | ```
"movlps (%3,%1), %%xmm4 \n"
``` |
||

95 | ```
"movlps (%4,%1), %%xmm5 \n"
``` |
||

96 | "movhps -8(%3,%0), %%xmm4 \n" // { cos[k], cos[k+1], cos[-k-2], cos[-k-1] } |
||

97 | "movhps -8(%4,%0), %%xmm5 \n" // { sin[k], sin[k+1], sin[-k-2], sin[-k-1] } |
||

98 | ```
"movaps %%xmm0, %%xmm2 \n"
``` |
||

99 | ```
"movaps %%xmm1, %%xmm3 \n"
``` |
||

100 | "mulps %%xmm5, %%xmm0 \n" // re*sin |
||

101 | "mulps %%xmm4, %%xmm1 \n" // im*cos |
||

102 | "mulps %%xmm4, %%xmm2 \n" // re*cos |
||

103 | "mulps %%xmm5, %%xmm3 \n" // im*sin |
||

104 | "subps %%xmm0, %%xmm1 \n" // -> re |
||

105 | "addps %%xmm3, %%xmm2 \n" // -> im |
||

106 | ```
"movaps %%xmm1, %%xmm0 \n"
``` |
||

107 | "unpcklps %%xmm2, %%xmm1 \n" // { z[k], z[k+1] } |
||

108 | "unpckhps %%xmm2, %%xmm0 \n" // { z[-k-2], z[-k-1] } |
||

109 | ::"r"(-4*k), "r"(4*k), |
||

110 | "r"(input+n4), "r"(tcos+n8), "r"(tsin+n8) |
||

111 | ebbafcb4 | Loren Merritt | ); |

112 | b250f9c6 | Aurelien Jacobs | ```
#if ARCH_X86_64
``` |

113 | 46803f4f | Loren Merritt | ```
// if we have enough regs, don't let gcc make the luts latency-bound
``` |

114 | ```
// but if not, latency is faster than spilling
``` |
||

115 | be449fca | Diego Pettenò | ```
__asm__("movlps %%xmm0, %0 \n"
``` |

116 | 46803f4f | Loren Merritt | ```
"movhps %%xmm0, %1 \n"
``` |

117 | ```
"movlps %%xmm1, %2 \n"
``` |
||

118 | ```
"movhps %%xmm1, %3 \n"
``` |
||

119 | :"=m"(z[revtab[-k-2]]), |
||

120 | "=m"(z[revtab[-k-1]]), |
||

121 | ```
"=m"(z[revtab[ k ]]),
``` |
||

122 | "=m"(z[revtab[ k+1]]) |
||

123 | ebbafcb4 | Loren Merritt | ); |

124 | 46803f4f | Loren Merritt | ```
#else
``` |

125 | be449fca | Diego Pettenò | __asm__("movlps %%xmm0, %0" :"=m"(z[revtab[-k-2]])); |

126 | __asm__("movhps %%xmm0, %0" :"=m"(z[revtab[-k-1]])); |
||

127 | __asm__("movlps %%xmm1, %0" :"=m"(z[revtab[ k ]])); |
||

128 | __asm__("movhps %%xmm1, %0" :"=m"(z[revtab[ k+1]])); |
||

129 | 25e4f8aa | Zuxy Meng | ```
#endif
``` |

130 | ebbafcb4 | Loren Merritt | } |

131 | b9fa3208 | Loren Merritt | |

132 | 01b22147 | Måns Rullgård | ff_fft_dispatch_sse(z, s->nbits); |

133 | b9fa3208 | Loren Merritt | |

134 | 46803f4f | Loren Merritt | ```
/* post rotation + reinterleave + reorder */
``` |

135 | b9fa3208 | Loren Merritt | |

136 | 46803f4f | Loren Merritt | ```
#define CMUL(j,xmm0,xmm1)\
``` |

137 | "movaps (%2,"#j",2), %%xmm6 \n"\ |
||

138 | "movaps 16(%2,"#j",2), "#xmm0"\n"\ |
||

139 | "movaps %%xmm6, "#xmm1"\n"\ |
||

140 | "movaps "#xmm0",%%xmm7 \n"\ |
||

141 | "mulps (%3,"#j"), %%xmm6 \n"\ |
||

142 | "mulps (%4,"#j"), "#xmm0"\n"\ |
||

143 | "mulps (%4,"#j"), "#xmm1"\n"\ |
||

144 | "mulps (%3,"#j"), %%xmm7 \n"\ |
||

145 | "subps %%xmm6, "#xmm0"\n"\ |
||

146 | "addps %%xmm7, "#xmm1"\n" |
||

147 | ebbafcb4 | Loren Merritt | |

148 | 46803f4f | Loren Merritt | j = -n2; |

149 | ```
k = n2-16;
``` |
||

150 | be449fca | Diego Pettenò | ```
__asm__ volatile(
``` |

151 | 46803f4f | Loren Merritt | ```
"1: \n"
``` |

152 | ```
CMUL(%0, %%xmm0, %%xmm1)
``` |
||

153 | ```
CMUL(%1, %%xmm4, %%xmm5)
``` |
||

154 | ```
"shufps $0x1b, %%xmm1, %%xmm1 \n"
``` |
||

155 | ```
"shufps $0x1b, %%xmm5, %%xmm5 \n"
``` |
||

156 | ```
"movaps %%xmm4, %%xmm6 \n"
``` |
||

157 | ```
"unpckhps %%xmm1, %%xmm4 \n"
``` |
||

158 | ```
"unpcklps %%xmm1, %%xmm6 \n"
``` |
||

159 | ```
"movaps %%xmm0, %%xmm2 \n"
``` |
||

160 | ```
"unpcklps %%xmm5, %%xmm0 \n"
``` |
||

161 | ```
"unpckhps %%xmm5, %%xmm2 \n"
``` |
||

162 | ```
"movaps %%xmm6, (%2,%1,2) \n"
``` |
||

163 | ```
"movaps %%xmm4, 16(%2,%1,2) \n"
``` |
||

164 | ```
"movaps %%xmm0, (%2,%0,2) \n"
``` |
||

165 | ```
"movaps %%xmm2, 16(%2,%0,2) \n"
``` |
||

166 | ```
"sub $16, %1 \n"
``` |
||

167 | ```
"add $16, %0 \n"
``` |
||

168 | ```
"jl 1b \n"
``` |
||

169 | :"+&r"(j), "+&r"(k) |
||

170 | :"r"(z+n8), "r"(tcos+n8), "r"(tsin+n8) |
||

171 | ebbafcb4 | Loren Merritt | ```
:"memory"
``` |

172 | ); |
||

173 | } |
||

174 | |||

175 | 01b22147 | Måns Rullgård | void ff_imdct_calc_sse(FFTContext *s, FFTSample *output, const FFTSample *input) |

176 | b9fa3208 | Loren Merritt | { |

177 | x86_reg j, k; |
||

178 | 01b22147 | Måns Rullgård | long n = 1 << s->mdct_bits; |

179 | 46803f4f | Loren Merritt | long n4 = n >> 2; |

180 | b9fa3208 | Loren Merritt | |

181 | 46803f4f | Loren Merritt | ff_imdct_half_sse(s, output+n4, input); |

182 | b9fa3208 | Loren Merritt | |

183 | j = -n; |
||

184 | ```
k = n-16;
``` |
||

185 | be449fca | Diego Pettenò | ```
__asm__ volatile(
``` |

186 | 46803f4f | Loren Merritt | ```
"movaps %4, %%xmm7 \n"
``` |

187 | ```
"1: \n"
``` |
||

188 | ```
"movaps (%2,%1), %%xmm0 \n"
``` |
||

189 | ```
"movaps (%3,%0), %%xmm1 \n"
``` |
||

190 | ```
"shufps $0x1b, %%xmm0, %%xmm0 \n"
``` |
||

191 | ```
"shufps $0x1b, %%xmm1, %%xmm1 \n"
``` |
||

192 | ```
"xorps %%xmm7, %%xmm0 \n"
``` |
||

193 | ```
"movaps %%xmm1, (%3,%1) \n"
``` |
||

194 | ```
"movaps %%xmm0, (%2,%0) \n"
``` |
||

195 | ```
"sub $16, %1 \n"
``` |
||

196 | ```
"add $16, %0 \n"
``` |
||

197 | ```
"jl 1b \n"
``` |
||

198 | b9fa3208 | Loren Merritt | :"+r"(j), "+r"(k) |

199 | 46803f4f | Loren Merritt | :"r"(output+n4), "r"(output+n4*3), |

200 | ```
"m"(*m1m1m1m1)
``` |
||

201 | b9fa3208 | Loren Merritt | ); |

202 | } |