fixed_bfin.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (C) 2005 Analog Devices
  2. Author: Jean-Marc Valin */
  3. /**
  4. @file fixed_bfin.h
  5. @brief Blackfin fixed-point operations
  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. #ifndef FIXED_BFIN_H
  32. #define FIXED_BFIN_H
  33. #include "bfin.h"
  34. #undef PDIV32_16
  35. static inline spx_word16_t PDIV32_16(spx_word32_t a, spx_word16_t b)
  36. {
  37. spx_word32_t res, bb;
  38. bb = b;
  39. a += b>>1;
  40. __asm__ (
  41. "P0 = 15;\n\t"
  42. "R0 = %1;\n\t"
  43. "R1 = %2;\n\t"
  44. //"R0 = R0 + R1;\n\t"
  45. "R0 <<= 1;\n\t"
  46. "DIVS (R0, R1);\n\t"
  47. "LOOP divide%= LC0 = P0;\n\t"
  48. "LOOP_BEGIN divide%=;\n\t"
  49. "DIVQ (R0, R1);\n\t"
  50. "LOOP_END divide%=;\n\t"
  51. "R0 = R0.L;\n\t"
  52. "%0 = R0;\n\t"
  53. : "=m" (res)
  54. : "m" (a), "m" (bb)
  55. : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS);
  56. return res;
  57. }
  58. #undef DIV32_16
  59. static inline spx_word16_t DIV32_16(spx_word32_t a, spx_word16_t b)
  60. {
  61. spx_word32_t res, bb;
  62. bb = b;
  63. /* Make the roundinf consistent with the C version
  64. (do we need to do that?)*/
  65. if (a<0)
  66. a += (b-1);
  67. __asm__ (
  68. "P0 = 15;\n\t"
  69. "R0 = %1;\n\t"
  70. "R1 = %2;\n\t"
  71. "R0 <<= 1;\n\t"
  72. "DIVS (R0, R1);\n\t"
  73. "LOOP divide%= LC0 = P0;\n\t"
  74. "LOOP_BEGIN divide%=;\n\t"
  75. "DIVQ (R0, R1);\n\t"
  76. "LOOP_END divide%=;\n\t"
  77. "R0 = R0.L;\n\t"
  78. "%0 = R0;\n\t"
  79. : "=m" (res)
  80. : "m" (a), "m" (bb)
  81. : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS);
  82. return res;
  83. }
  84. #undef MAX16
  85. static inline spx_word16_t MAX16(spx_word16_t a, spx_word16_t b)
  86. {
  87. spx_word32_t res;
  88. __asm__ (
  89. "%1 = %1.L (X);\n\t"
  90. "%2 = %2.L (X);\n\t"
  91. "%0 = MAX(%1,%2);"
  92. : "=d" (res)
  93. : "%d" (a), "d" (b)
  94. : "ASTAT"
  95. );
  96. return res;
  97. }
  98. #undef MULT16_32_Q15
  99. static inline spx_word32_t MULT16_32_Q15(spx_word16_t a, spx_word32_t b)
  100. {
  101. spx_word32_t res;
  102. __asm__
  103. (
  104. "A1 = %2.L*%1.L (M);\n\t"
  105. "A1 = A1 >>> 15;\n\t"
  106. "%0 = (A1 += %2.L*%1.H) ;\n\t"
  107. : "=&W" (res), "=&d" (b)
  108. : "d" (a), "1" (b)
  109. : "A1", "ASTAT"
  110. );
  111. return res;
  112. }
  113. #undef MAC16_32_Q15
  114. static inline spx_word32_t MAC16_32_Q15(spx_word32_t c, spx_word16_t a, spx_word32_t b)
  115. {
  116. spx_word32_t res;
  117. __asm__
  118. (
  119. "A1 = %2.L*%1.L (M);\n\t"
  120. "A1 = A1 >>> 15;\n\t"
  121. "%0 = (A1 += %2.L*%1.H);\n\t"
  122. "%0 = %0 + %4;\n\t"
  123. : "=&W" (res), "=&d" (b)
  124. : "d" (a), "1" (b), "d" (c)
  125. : "A1", "ASTAT"
  126. );
  127. return res;
  128. }
  129. #undef MULT16_32_Q14
  130. static inline spx_word32_t MULT16_32_Q14(spx_word16_t a, spx_word32_t b)
  131. {
  132. spx_word32_t res;
  133. __asm__
  134. (
  135. "%2 <<= 1;\n\t"
  136. "A1 = %1.L*%2.L (M);\n\t"
  137. "A1 = A1 >>> 15;\n\t"
  138. "%0 = (A1 += %1.L*%2.H);\n\t"
  139. : "=W" (res), "=d" (a), "=d" (b)
  140. : "1" (a), "2" (b)
  141. : "A1", "ASTAT"
  142. );
  143. return res;
  144. }
  145. #undef MAC16_32_Q14
  146. static inline spx_word32_t MAC16_32_Q14(spx_word32_t c, spx_word16_t a, spx_word32_t b)
  147. {
  148. spx_word32_t res;
  149. __asm__
  150. (
  151. "%1 <<= 1;\n\t"
  152. "A1 = %2.L*%1.L (M);\n\t"
  153. "A1 = A1 >>> 15;\n\t"
  154. "%0 = (A1 += %2.L*%1.H);\n\t"
  155. "%0 = %0 + %4;\n\t"
  156. : "=&W" (res), "=&d" (b)
  157. : "d" (a), "1" (b), "d" (c)
  158. : "A1", "ASTAT"
  159. );
  160. return res;
  161. }
  162. #endif