vq_sse.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 2004 Jean-Marc Valin */
  2. /**
  3. @file vq_sse.h
  4. @brief SSE-optimized vq routine
  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. #define OVERRIDE_VQ_NBEST
  31. void vq_nbest(spx_word16_t *_in, const __m128 *codebook, int len, int entries, __m128 *E, int N, int *nbest, spx_word32_t *best_dist, char *stack)
  32. {
  33. int i,j,k,used;
  34. VARDECL(float *dist);
  35. VARDECL(__m128 *in);
  36. __m128 half;
  37. used = 0;
  38. ALLOC(dist, entries, float);
  39. half = _mm_set_ps1(.5f);
  40. ALLOC(in, len, __m128);
  41. for (i=0;i<len;i++)
  42. in[i] = _mm_set_ps1(_in[i]);
  43. for (i=0;i<entries>>2;i++)
  44. {
  45. __m128 d = _mm_mul_ps(E[i], half);
  46. for (j=0;j<len;j++)
  47. d = _mm_sub_ps(d, _mm_mul_ps(in[j], *codebook++));
  48. _mm_storeu_ps(dist+4*i, d);
  49. }
  50. for (i=0;i<entries;i++)
  51. {
  52. if (i<N || dist[i]<best_dist[N-1])
  53. {
  54. for (k=N-1; (k >= 1) && (k > used || dist[i] < best_dist[k-1]); k--)
  55. {
  56. best_dist[k]=best_dist[k-1];
  57. nbest[k] = nbest[k-1];
  58. }
  59. best_dist[k]=dist[i];
  60. nbest[k]=i;
  61. used++;
  62. }
  63. }
  64. }
  65. #define OVERRIDE_VQ_NBEST_SIGN
  66. void vq_nbest_sign(spx_word16_t *_in, const __m128 *codebook, int len, int entries, __m128 *E, int N, int *nbest, spx_word32_t *best_dist, char *stack)
  67. {
  68. int i,j,k,used;
  69. VARDECL(float *dist);
  70. VARDECL(__m128 *in);
  71. __m128 half;
  72. used = 0;
  73. ALLOC(dist, entries, float);
  74. half = _mm_set_ps1(.5f);
  75. ALLOC(in, len, __m128);
  76. for (i=0;i<len;i++)
  77. in[i] = _mm_set_ps1(_in[i]);
  78. for (i=0;i<entries>>2;i++)
  79. {
  80. __m128 d = _mm_setzero_ps();
  81. for (j=0;j<len;j++)
  82. d = _mm_add_ps(d, _mm_mul_ps(in[j], *codebook++));
  83. _mm_storeu_ps(dist+4*i, d);
  84. }
  85. for (i=0;i<entries;i++)
  86. {
  87. int sign;
  88. if (dist[i]>0)
  89. {
  90. sign=0;
  91. dist[i]=-dist[i];
  92. } else
  93. {
  94. sign=1;
  95. }
  96. dist[i] += .5f*((float*)E)[i];
  97. if (i<N || dist[i]<best_dist[N-1])
  98. {
  99. for (k=N-1; (k >= 1) && (k > used || dist[i] < best_dist[k-1]); k--)
  100. {
  101. best_dist[k]=best_dist[k-1];
  102. nbest[k] = nbest[k-1];
  103. }
  104. best_dist[k]=dist[i];
  105. nbest[k]=i;
  106. used++;
  107. if (sign)
  108. nbest[k]+=entries;
  109. }
  110. }
  111. }