filters_sse.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (C) 2002 Jean-Marc Valin */
  2. /**
  3. @file filters_sse.h
  4. @brief Various analysis/synthesis filters (SSE version)
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. - Neither the name of the Xiph.org Foundation nor the names of its
  16. contributors may be used to endorse or promote products derived from
  17. this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <xmmintrin.h>
  31. void filter_mem16_10(const float *x, const float *_num, const float *_den, float *y, int N, int ord, float *_mem)
  32. {
  33. __m128 num[3], den[3], mem[3];
  34. int i;
  35. /* Copy numerator, denominator and memory to aligned xmm */
  36. for (i=0;i<2;i++)
  37. {
  38. mem[i] = _mm_loadu_ps(_mem+4*i);
  39. num[i] = _mm_loadu_ps(_num+4*i);
  40. den[i] = _mm_loadu_ps(_den+4*i);
  41. }
  42. mem[2] = _mm_setr_ps(_mem[8], _mem[9], 0, 0);
  43. num[2] = _mm_setr_ps(_num[8], _num[9], 0, 0);
  44. den[2] = _mm_setr_ps(_den[8], _den[9], 0, 0);
  45. for (i=0;i<N;i++)
  46. {
  47. __m128 xx;
  48. __m128 yy;
  49. /* Compute next filter result */
  50. xx = _mm_load_ps1(x+i);
  51. yy = _mm_add_ss(xx, mem[0]);
  52. _mm_store_ss(y+i, yy);
  53. yy = _mm_shuffle_ps(yy, yy, 0);
  54. /* Update memory */
  55. mem[0] = _mm_move_ss(mem[0], mem[1]);
  56. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  57. mem[0] = _mm_add_ps(mem[0], _mm_mul_ps(xx, num[0]));
  58. mem[0] = _mm_sub_ps(mem[0], _mm_mul_ps(yy, den[0]));
  59. mem[1] = _mm_move_ss(mem[1], mem[2]);
  60. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  61. mem[1] = _mm_add_ps(mem[1], _mm_mul_ps(xx, num[1]));
  62. mem[1] = _mm_sub_ps(mem[1], _mm_mul_ps(yy, den[1]));
  63. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0xfd);
  64. mem[2] = _mm_add_ps(mem[2], _mm_mul_ps(xx, num[2]));
  65. mem[2] = _mm_sub_ps(mem[2], _mm_mul_ps(yy, den[2]));
  66. }
  67. /* Put memory back in its place */
  68. _mm_storeu_ps(_mem, mem[0]);
  69. _mm_storeu_ps(_mem+4, mem[1]);
  70. _mm_store_ss(_mem+8, mem[2]);
  71. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0x55);
  72. _mm_store_ss(_mem+9, mem[2]);
  73. }
  74. void filter_mem16_8(const float *x, const float *_num, const float *_den, float *y, int N, int ord, float *_mem)
  75. {
  76. __m128 num[2], den[2], mem[2];
  77. int i;
  78. /* Copy numerator, denominator and memory to aligned xmm */
  79. for (i=0;i<2;i++)
  80. {
  81. mem[i] = _mm_loadu_ps(_mem+4*i);
  82. num[i] = _mm_loadu_ps(_num+4*i);
  83. den[i] = _mm_loadu_ps(_den+4*i);
  84. }
  85. for (i=0;i<N;i++)
  86. {
  87. __m128 xx;
  88. __m128 yy;
  89. /* Compute next filter result */
  90. xx = _mm_load_ps1(x+i);
  91. yy = _mm_add_ss(xx, mem[0]);
  92. _mm_store_ss(y+i, yy);
  93. yy = _mm_shuffle_ps(yy, yy, 0);
  94. /* Update memory */
  95. mem[0] = _mm_move_ss(mem[0], mem[1]);
  96. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  97. mem[0] = _mm_add_ps(mem[0], _mm_mul_ps(xx, num[0]));
  98. mem[0] = _mm_sub_ps(mem[0], _mm_mul_ps(yy, den[0]));
  99. mem[1] = _mm_sub_ss(mem[1], mem[1]);
  100. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  101. mem[1] = _mm_add_ps(mem[1], _mm_mul_ps(xx, num[1]));
  102. mem[1] = _mm_sub_ps(mem[1], _mm_mul_ps(yy, den[1]));
  103. }
  104. /* Put memory back in its place */
  105. _mm_storeu_ps(_mem, mem[0]);
  106. _mm_storeu_ps(_mem+4, mem[1]);
  107. }
  108. #define OVERRIDE_FILTER_MEM16
  109. void filter_mem16(const float *x, const float *_num, const float *_den, float *y, int N, int ord, float *_mem, char *stack)
  110. {
  111. if(ord==10)
  112. filter_mem16_10(x, _num, _den, y, N, ord, _mem);
  113. else if (ord==8)
  114. filter_mem16_8(x, _num, _den, y, N, ord, _mem);
  115. }
  116. void iir_mem16_10(const float *x, const float *_den, float *y, int N, int ord, float *_mem)
  117. {
  118. __m128 den[3], mem[3];
  119. int i;
  120. /* Copy numerator, denominator and memory to aligned xmm */
  121. for (i=0;i<2;i++)
  122. {
  123. mem[i] = _mm_loadu_ps(_mem+4*i);
  124. den[i] = _mm_loadu_ps(_den+4*i);
  125. }
  126. mem[2] = _mm_setr_ps(_mem[8], _mem[9], 0, 0);
  127. den[2] = _mm_setr_ps(_den[8], _den[9], 0, 0);
  128. for (i=0;i<N;i++)
  129. {
  130. __m128 xx;
  131. __m128 yy;
  132. /* Compute next filter result */
  133. xx = _mm_load_ps1(x+i);
  134. yy = _mm_add_ss(xx, mem[0]);
  135. _mm_store_ss(y+i, yy);
  136. yy = _mm_shuffle_ps(yy, yy, 0);
  137. /* Update memory */
  138. mem[0] = _mm_move_ss(mem[0], mem[1]);
  139. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  140. mem[0] = _mm_sub_ps(mem[0], _mm_mul_ps(yy, den[0]));
  141. mem[1] = _mm_move_ss(mem[1], mem[2]);
  142. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  143. mem[1] = _mm_sub_ps(mem[1], _mm_mul_ps(yy, den[1]));
  144. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0xfd);
  145. mem[2] = _mm_sub_ps(mem[2], _mm_mul_ps(yy, den[2]));
  146. }
  147. /* Put memory back in its place */
  148. _mm_storeu_ps(_mem, mem[0]);
  149. _mm_storeu_ps(_mem+4, mem[1]);
  150. _mm_store_ss(_mem+8, mem[2]);
  151. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0x55);
  152. _mm_store_ss(_mem+9, mem[2]);
  153. }
  154. void iir_mem16_8(const float *x, const float *_den, float *y, int N, int ord, float *_mem)
  155. {
  156. __m128 den[2], mem[2];
  157. int i;
  158. /* Copy numerator, denominator and memory to aligned xmm */
  159. for (i=0;i<2;i++)
  160. {
  161. mem[i] = _mm_loadu_ps(_mem+4*i);
  162. den[i] = _mm_loadu_ps(_den+4*i);
  163. }
  164. for (i=0;i<N;i++)
  165. {
  166. __m128 xx;
  167. __m128 yy;
  168. /* Compute next filter result */
  169. xx = _mm_load_ps1(x+i);
  170. yy = _mm_add_ss(xx, mem[0]);
  171. _mm_store_ss(y+i, yy);
  172. yy = _mm_shuffle_ps(yy, yy, 0);
  173. /* Update memory */
  174. mem[0] = _mm_move_ss(mem[0], mem[1]);
  175. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  176. mem[0] = _mm_sub_ps(mem[0], _mm_mul_ps(yy, den[0]));
  177. mem[1] = _mm_sub_ss(mem[1], mem[1]);
  178. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  179. mem[1] = _mm_sub_ps(mem[1], _mm_mul_ps(yy, den[1]));
  180. }
  181. /* Put memory back in its place */
  182. _mm_storeu_ps(_mem, mem[0]);
  183. _mm_storeu_ps(_mem+4, mem[1]);
  184. }
  185. #define OVERRIDE_IIR_MEM16
  186. void iir_mem16(const float *x, const float *_den, float *y, int N, int ord, float *_mem, char *stack)
  187. {
  188. if(ord==10)
  189. iir_mem16_10(x, _den, y, N, ord, _mem);
  190. else if (ord==8)
  191. iir_mem16_8(x, _den, y, N, ord, _mem);
  192. }
  193. void fir_mem16_10(const float *x, const float *_num, float *y, int N, int ord, float *_mem)
  194. {
  195. __m128 num[3], mem[3];
  196. int i;
  197. /* Copy numerator, denominator and memory to aligned xmm */
  198. for (i=0;i<2;i++)
  199. {
  200. mem[i] = _mm_loadu_ps(_mem+4*i);
  201. num[i] = _mm_loadu_ps(_num+4*i);
  202. }
  203. mem[2] = _mm_setr_ps(_mem[8], _mem[9], 0, 0);
  204. num[2] = _mm_setr_ps(_num[8], _num[9], 0, 0);
  205. for (i=0;i<N;i++)
  206. {
  207. __m128 xx;
  208. __m128 yy;
  209. /* Compute next filter result */
  210. xx = _mm_load_ps1(x+i);
  211. yy = _mm_add_ss(xx, mem[0]);
  212. _mm_store_ss(y+i, yy);
  213. yy = _mm_shuffle_ps(yy, yy, 0);
  214. /* Update memory */
  215. mem[0] = _mm_move_ss(mem[0], mem[1]);
  216. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  217. mem[0] = _mm_add_ps(mem[0], _mm_mul_ps(xx, num[0]));
  218. mem[1] = _mm_move_ss(mem[1], mem[2]);
  219. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  220. mem[1] = _mm_add_ps(mem[1], _mm_mul_ps(xx, num[1]));
  221. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0xfd);
  222. mem[2] = _mm_add_ps(mem[2], _mm_mul_ps(xx, num[2]));
  223. }
  224. /* Put memory back in its place */
  225. _mm_storeu_ps(_mem, mem[0]);
  226. _mm_storeu_ps(_mem+4, mem[1]);
  227. _mm_store_ss(_mem+8, mem[2]);
  228. mem[2] = _mm_shuffle_ps(mem[2], mem[2], 0x55);
  229. _mm_store_ss(_mem+9, mem[2]);
  230. }
  231. void fir_mem16_8(const float *x, const float *_num, float *y, int N, int ord, float *_mem)
  232. {
  233. __m128 num[2], mem[2];
  234. int i;
  235. /* Copy numerator, denominator and memory to aligned xmm */
  236. for (i=0;i<2;i++)
  237. {
  238. mem[i] = _mm_loadu_ps(_mem+4*i);
  239. num[i] = _mm_loadu_ps(_num+4*i);
  240. }
  241. for (i=0;i<N;i++)
  242. {
  243. __m128 xx;
  244. __m128 yy;
  245. /* Compute next filter result */
  246. xx = _mm_load_ps1(x+i);
  247. yy = _mm_add_ss(xx, mem[0]);
  248. _mm_store_ss(y+i, yy);
  249. yy = _mm_shuffle_ps(yy, yy, 0);
  250. /* Update memory */
  251. mem[0] = _mm_move_ss(mem[0], mem[1]);
  252. mem[0] = _mm_shuffle_ps(mem[0], mem[0], 0x39);
  253. mem[0] = _mm_add_ps(mem[0], _mm_mul_ps(xx, num[0]));
  254. mem[1] = _mm_sub_ss(mem[1], mem[1]);
  255. mem[1] = _mm_shuffle_ps(mem[1], mem[1], 0x39);
  256. mem[1] = _mm_add_ps(mem[1], _mm_mul_ps(xx, num[1]));
  257. }
  258. /* Put memory back in its place */
  259. _mm_storeu_ps(_mem, mem[0]);
  260. _mm_storeu_ps(_mem+4, mem[1]);
  261. }
  262. #define OVERRIDE_FIR_MEM16
  263. void fir_mem16(const float *x, const float *_num, float *y, int N, int ord, float *_mem, char *stack)
  264. {
  265. if(ord==10)
  266. fir_mem16_10(x, _num, y, N, ord, _mem);
  267. else if (ord==8)
  268. fir_mem16_8(x, _num, y, N, ord, _mem);
  269. }