wmafixed.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * __________ __ ___.
  3. * Open \______ \ ____ ____ | | _\_ |__ _______ ___
  4. * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
  5. * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
  6. * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
  7. * \/ \/ \/ \/ \/
  8. *
  9. * Copyright (C) 2007 Michael Giacomelli
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  17. * KIND, either express or implied.
  18. *
  19. ****************************************************************************/
  20. /* fixed precision code. We use a combination of Sign 15.16 and Sign.31
  21. precision here.
  22. The WMA decoder does not always follow this convention, and occasionally
  23. renormalizes values to other formats in order to maximize precision.
  24. However, only the two precisions above are provided in this file.
  25. */
  26. #include "types.h"
  27. #define PRECISION 16
  28. #define PRECISION64 16
  29. #define fixtof64(x) (float)((float)(x) / (float)(1 << PRECISION64)) //does not work on int64_t!
  30. #define ftofix32(x) ((fixed32)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
  31. #define itofix64(x) (IntTo64(x))
  32. #define itofix32(x) ((x) << PRECISION)
  33. #define fixtoi32(x) ((x) >> PRECISION)
  34. #define fixtoi64(x) (IntFrom64(x))
  35. /*fixed functions*/
  36. fixed64 IntTo64(int x);
  37. int IntFrom64(fixed64 x);
  38. fixed32 Fixed32From64(fixed64 x);
  39. fixed64 Fixed32To64(fixed32 x);
  40. fixed64 fixmul64byfixed(fixed64 x, fixed32 y);
  41. fixed32 fixdiv32(fixed32 x, fixed32 y);
  42. fixed64 fixdiv64(fixed64 x, fixed64 y);
  43. fixed32 fixsqrt32(fixed32 x);
  44. long fsincos(unsigned long phase, fixed32 *cos);
  45. #ifdef CPU_ARM
  46. /*Sign-15.16 format */
  47. #define fixmul32(x, y) \
  48. ({ int32_t __hi; \
  49. uint32_t __lo; \
  50. int32_t __result; \
  51. asm ("smull %0, %1, %3, %4\n\t" \
  52. "movs %0, %0, lsr %5\n\t" \
  53. "adc %2, %0, %1, lsl %6" \
  54. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  55. : "%r" (x), "r" (y), \
  56. "M" (PRECISION), "M" (32 - PRECISION) \
  57. : "cc"); \
  58. __result; \
  59. })
  60. #define fixmul32b(x, y) \
  61. ({ int32_t __hi; \
  62. uint32_t __lo; \
  63. int32_t __result; \
  64. asm ("smull %0, %1, %3, %4\n\t" \
  65. "movs %2, %1, lsl #1" \
  66. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  67. : "%r" (x), "r" (y) \
  68. : "cc"); \
  69. __result; \
  70. })
  71. #elif defined(CPU_COLDFIRE)
  72. static inline int32_t fixmul32(int32_t x, int32_t y)
  73. {
  74. #if PRECISION != 16
  75. #warning Coldfire fixmul32() only works for PRECISION == 16
  76. #endif
  77. int32_t t1;
  78. asm (
  79. "mac.l %[x], %[y], %%acc0 \n" /* multiply */
  80. "mulu.l %[y], %[x] \n" /* get lower half, avoid emac stall */
  81. "movclr.l %%acc0, %[t1] \n" /* get higher half */
  82. "lsr.l #1, %[t1] \n"
  83. "move.w %[t1], %[x] \n"
  84. "swap %[x] \n"
  85. : [t1] "=&d" (t1), [x] "+d" (x)
  86. : [y] "d" (y)
  87. );
  88. return x;
  89. }
  90. static inline int32_t fixmul32b(int32_t x, int32_t y)
  91. {
  92. asm (
  93. "mac.l %[x], %[y], %%acc0 \n" /* multiply */
  94. "movclr.l %%acc0, %[x] \n" /* get higher half */
  95. : [x] "+d" (x)
  96. : [y] "d" (y)
  97. );
  98. return x;
  99. }
  100. #else
  101. static inline fixed32 fixmul32(fixed32 x, fixed32 y)
  102. {
  103. fixed64 temp;
  104. temp = x;
  105. temp *= y;
  106. temp >>= PRECISION;
  107. return (fixed32)temp;
  108. }
  109. static inline fixed32 fixmul32b(fixed32 x, fixed32 y)
  110. {
  111. fixed64 temp;
  112. temp = x;
  113. temp *= y;
  114. temp >>= 31; //16+31-16 = 31 bits
  115. return (fixed32)temp;
  116. }
  117. #endif