vq_sse.h 3.6 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. used = 0;
  72. ALLOC(dist, entries, float);
  73. ALLOC(in, len, __m128);
  74. for (i=0;i<len;i++)
  75. in[i] = _mm_set_ps1(_in[i]);
  76. for (i=0;i<entries>>2;i++)
  77. {
  78. __m128 d = _mm_setzero_ps();
  79. for (j=0;j<len;j++)
  80. d = _mm_add_ps(d, _mm_mul_ps(in[j], *codebook++));
  81. _mm_storeu_ps(dist+4*i, d);
  82. }
  83. for (i=0;i<entries;i++)
  84. {
  85. int sign;
  86. if (dist[i]>0)
  87. {
  88. sign=0;
  89. dist[i]=-dist[i];
  90. } else
  91. {
  92. sign=1;
  93. }
  94. dist[i] += .5f*((float*)E)[i];
  95. if (i<N || dist[i]<best_dist[N-1])
  96. {
  97. for (k=N-1; (k >= 1) && (k > used || dist[i] < best_dist[k-1]); k--)
  98. {
  99. best_dist[k]=best_dist[k-1];
  100. nbest[k] = nbest[k-1];
  101. }
  102. best_dist[k]=dist[i];
  103. nbest[k]=i;
  104. used++;
  105. if (sign)
  106. nbest[k]+=entries;
  107. }
  108. }
  109. }