mixer_sse3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * OpenAL cross platform audio library, SSE3 mixer functions
  3. *
  4. * Copyright (C) 2014 by Timothy Arceri <[email protected]>.
  5. * Copyright (C) 2015 by Chris Robinson <[email protected]>.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. * Or go to http://www.gnu.org/copyleft/lgpl.html
  22. */
  23. #include "config.h"
  24. #include <xmmintrin.h>
  25. #include <emmintrin.h>
  26. #include <pmmintrin.h>
  27. #include "alu.h"
  28. #include "mixer_defs.h"
  29. const ALfloat *Resample_fir4_32_SSE3(const BsincState* UNUSED(state), const ALfloat *restrict src,
  30. ALuint frac, ALuint increment, ALfloat *restrict dst,
  31. ALuint numsamples)
  32. {
  33. const __m128i increment4 = _mm_set1_epi32(increment*4);
  34. const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
  35. union { alignas(16) ALuint i[4]; float f[4]; } pos_;
  36. union { alignas(16) ALuint i[4]; float f[4]; } frac_;
  37. __m128i frac4, pos4;
  38. ALuint pos;
  39. ALuint i;
  40. InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
  41. frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
  42. pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
  43. --src;
  44. for(i = 0;numsamples-i > 3;i += 4)
  45. {
  46. const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]]);
  47. const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]]);
  48. const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]]);
  49. const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]]);
  50. __m128 k0 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[0]]);
  51. __m128 k1 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[1]]);
  52. __m128 k2 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[2]]);
  53. __m128 k3 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[3]]);
  54. __m128 out;
  55. k0 = _mm_mul_ps(k0, val0);
  56. k1 = _mm_mul_ps(k1, val1);
  57. k2 = _mm_mul_ps(k2, val2);
  58. k3 = _mm_mul_ps(k3, val3);
  59. k0 = _mm_hadd_ps(k0, k1);
  60. k2 = _mm_hadd_ps(k2, k3);
  61. out = _mm_hadd_ps(k0, k2);
  62. _mm_store_ps(&dst[i], out);
  63. frac4 = _mm_add_epi32(frac4, increment4);
  64. pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
  65. frac4 = _mm_and_si128(frac4, fracMask4);
  66. _mm_store_ps(pos_.f, _mm_castsi128_ps(pos4));
  67. _mm_store_ps(frac_.f, _mm_castsi128_ps(frac4));
  68. }
  69. /* NOTE: These four elements represent the position *after* the last four
  70. * samples, so the lowest element is the next position to resample.
  71. */
  72. pos = pos_.i[0];
  73. frac = frac_.i[0];
  74. for(;i < numsamples;i++)
  75. {
  76. dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], frac);
  77. frac += increment;
  78. pos += frac>>FRACTIONBITS;
  79. frac &= FRACTIONMASK;
  80. }
  81. return dst;
  82. }
  83. const ALfloat *Resample_fir8_32_SSE3(const BsincState* UNUSED(state), const ALfloat *restrict src,
  84. ALuint frac, ALuint increment, ALfloat *restrict dst,
  85. ALuint numsamples)
  86. {
  87. const __m128i increment4 = _mm_set1_epi32(increment*4);
  88. const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
  89. union { alignas(16) ALuint i[4]; float f[4]; } pos_;
  90. union { alignas(16) ALuint i[4]; float f[4]; } frac_;
  91. __m128i frac4, pos4;
  92. ALuint pos;
  93. ALuint i, j;
  94. InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
  95. frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
  96. pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
  97. src -= 3;
  98. for(i = 0;numsamples-i > 3;i += 4)
  99. {
  100. __m128 out[2];
  101. for(j = 0;j < 8;j+=4)
  102. {
  103. const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]+j]);
  104. const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]+j]);
  105. const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]+j]);
  106. const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]+j]);
  107. __m128 k0 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[0]][j]);
  108. __m128 k1 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[1]][j]);
  109. __m128 k2 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[2]][j]);
  110. __m128 k3 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[3]][j]);
  111. k0 = _mm_mul_ps(k0, val0);
  112. k1 = _mm_mul_ps(k1, val1);
  113. k2 = _mm_mul_ps(k2, val2);
  114. k3 = _mm_mul_ps(k3, val3);
  115. k0 = _mm_hadd_ps(k0, k1);
  116. k2 = _mm_hadd_ps(k2, k3);
  117. out[j>>2] = _mm_hadd_ps(k0, k2);
  118. }
  119. out[0] = _mm_add_ps(out[0], out[1]);
  120. _mm_store_ps(&dst[i], out[0]);
  121. frac4 = _mm_add_epi32(frac4, increment4);
  122. pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
  123. frac4 = _mm_and_si128(frac4, fracMask4);
  124. _mm_store_ps(pos_.f, _mm_castsi128_ps(pos4));
  125. _mm_store_ps(frac_.f, _mm_castsi128_ps(frac4));
  126. }
  127. pos = pos_.i[0];
  128. frac = frac_.i[0];
  129. for(;i < numsamples;i++)
  130. {
  131. dst[i] = resample_fir8(src[pos ], src[pos+1], src[pos+2], src[pos+3],
  132. src[pos+4], src[pos+5], src[pos+6], src[pos+7], frac);
  133. frac += increment;
  134. pos += frac>>FRACTIONBITS;
  135. frac &= FRACTIONMASK;
  136. }
  137. return dst;
  138. }