quant_lsp_bfin.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (C) 2006 David Rowe */
  2. /**
  3. @file quant_lsp_bfin.h
  4. @author David Rowe
  5. @brief Various compatibility routines for Speex (Blackfin version)
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. - Neither the name of the Xiph.org Foundation nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #define OVERRIDE_LSP_QUANT
  32. #ifdef OVERRIDE_LSP_QUANT
  33. #include "bfin.h"
  34. /*
  35. Note http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
  36. well tell you all the magic register constraints used below
  37. for gcc in-line asm.
  38. */
  39. static int lsp_quant(
  40. spx_word16_t *x,
  41. const signed char *cdbk,
  42. int nbVec,
  43. int nbDim
  44. )
  45. {
  46. int j;
  47. spx_word32_t best_dist=1<<30;
  48. int best_id=0;
  49. __asm__ __volatile__
  50. (
  51. " %0 = 1 (X);\n\t" /* %0: best_dist */
  52. " %0 <<= 30;\n\t"
  53. " %1 = 0 (X);\n\t" /* %1: best_i */
  54. " P2 = %3\n\t" /* P2: ptr to cdbk */
  55. " R5 = 0;\n\t" /* R5: best cb entry */
  56. " R0 = %5;\n\t" /* set up circ addr */
  57. " R0 <<= 1;\n\t"
  58. " L0 = R0;\n\t"
  59. " I0 = %2;\n\t" /* %2: &x[0] */
  60. " B0 = %2;\n\t"
  61. " R2.L = W [I0++];\n\t"
  62. " LSETUP (1f, 2f) LC0 = %4;\n\t"
  63. "1: R3 = 0;\n\t" /* R3: dist */
  64. " LSETUP (3f, 4f) LC1 = %5;\n\t"
  65. "3: R1 = B [P2++] (X);\n\t"
  66. " R1 <<= 5;\n\t"
  67. " R0.L = R2.L - R1.L || R2.L = W [I0++];\n\t"
  68. " R0 = R0.L*R0.L;\n\t"
  69. "4: R3 = R3 + R0;\n\t"
  70. " cc =R3<%0;\n\t"
  71. " if cc %0=R3;\n\t"
  72. " if cc %1=R5;\n\t"
  73. "2: R5 += 1;\n\t"
  74. " L0 = 0;\n\t"
  75. : "=&d" (best_dist), "=&d" (best_id)
  76. : "a" (x), "b" (cdbk), "a" (nbVec), "a" (nbDim)
  77. : "I0", "P2", "R0", "R1", "R2", "R3", "R5", "L0", "B0", "A0",
  78. "CC", "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
  79. );
  80. for (j=0;j<nbDim;j++) {
  81. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  82. }
  83. return best_id;
  84. }
  85. #endif
  86. #define OVERRIDE_LSP_WEIGHT_QUANT
  87. #ifdef OVERRIDE_LSP_WEIGHT_QUANT
  88. /*
  89. Note http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
  90. well tell you all the magic register constraints used below
  91. for gcc in-line asm.
  92. */
  93. static int lsp_weight_quant(
  94. spx_word16_t *x,
  95. spx_word16_t *weight,
  96. const signed char *cdbk,
  97. int nbVec,
  98. int nbDim
  99. )
  100. {
  101. int j;
  102. spx_word32_t best_dist=1<<30;
  103. int best_id=0;
  104. __asm__ __volatile__
  105. (
  106. " %0 = 1 (X);\n\t" /* %0: best_dist */
  107. " %0 <<= 30;\n\t"
  108. " %1 = 0 (X);\n\t" /* %1: best_i */
  109. " P2 = %4\n\t" /* P2: ptr to cdbk */
  110. " R5 = 0;\n\t" /* R5: best cb entry */
  111. " R0 = %6;\n\t" /* set up circ addr */
  112. " R0 <<= 1;\n\t"
  113. " L0 = R0;\n\t"
  114. " L1 = R0;\n\t"
  115. " I0 = %2;\n\t" /* %2: &x[0] */
  116. " I1 = %3;\n\t" /* %3: &weight[0] */
  117. " B0 = %2;\n\t"
  118. " B1 = %3;\n\t"
  119. " LSETUP (1f, 2f) LC0 = %5;\n\t"
  120. "1: R3 = 0 (X);\n\t" /* R3: dist */
  121. " LSETUP (3f, 4f) LC1 = %6;\n\t"
  122. "3: R0.L = W [I0++] || R2.L = W [I1++];\n\t"
  123. " R1 = B [P2++] (X);\n\t"
  124. " R1 <<= 5;\n\t"
  125. " R0.L = R0.L - R1.L;\n\t"
  126. " R0 = R0.L*R0.L;\n\t"
  127. " A1 = R2.L*R0.L (M,IS);\n\t"
  128. " A1 = A1 >>> 16;\n\t"
  129. " R1 = (A1 += R2.L*R0.H) (IS);\n\t"
  130. "4: R3 = R3 + R1;\n\t"
  131. " cc =R3<%0;\n\t"
  132. " if cc %0=R3;\n\t"
  133. " if cc %1=R5;\n\t"
  134. "2: R5 += 1;\n\t"
  135. " L0 = 0;\n\t"
  136. " L1 = 0;\n\t"
  137. : "=&d" (best_dist), "=&d" (best_id)
  138. : "a" (x), "a" (weight), "b" (cdbk), "a" (nbVec), "a" (nbDim)
  139. : "I0", "I1", "P2", "R0", "R1", "R2", "R3", "R5", "A1",
  140. "L0", "L1", "B0", "B1", "CC", "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
  141. );
  142. for (j=0;j<nbDim;j++) {
  143. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  144. }
  145. return best_id;
  146. }
  147. #endif