wmafixed.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include "wmadec.h"
  21. #include "wmafixed.h"
  22. // #include <codecs.h>
  23. fixed64 IntTo64(int x){
  24. fixed64 res = 0;
  25. unsigned char *p = (unsigned char *)&res;
  26. #ifdef ROCKBOX_BIG_ENDIAN
  27. p[5] = x & 0xff;
  28. p[4] = (x & 0xff00)>>8;
  29. p[3] = (x & 0xff0000)>>16;
  30. p[2] = (x & 0xff000000)>>24;
  31. #else
  32. p[2] = x & 0xff;
  33. p[3] = (x & 0xff00)>>8;
  34. p[4] = (x & 0xff0000)>>16;
  35. p[5] = (x & 0xff000000)>>24;
  36. #endif
  37. return res;
  38. }
  39. int IntFrom64(fixed64 x)
  40. {
  41. int res = 0;
  42. unsigned char *p = (unsigned char *)&x;
  43. #ifdef ROCKBOX_BIG_ENDIAN
  44. res = p[5] | (p[4]<<8) | (p[3]<<16) | (p[2]<<24);
  45. #else
  46. res = p[2] | (p[3]<<8) | (p[4]<<16) | (p[5]<<24);
  47. #endif
  48. return res;
  49. }
  50. fixed32 Fixed32From64(fixed64 x)
  51. {
  52. return x & 0xFFFFFFFF;
  53. }
  54. fixed64 Fixed32To64(fixed32 x)
  55. {
  56. return (fixed64)x;
  57. }
  58. /*
  59. Not performance senstitive code here
  60. */
  61. fixed64 fixmul64byfixed(fixed64 x, fixed32 y)
  62. {
  63. //return x * y;
  64. return (x * y);
  65. // return (fixed64) fixmul32(Fixed32From64(x),y);
  66. }
  67. fixed32 fixdiv32(fixed32 x, fixed32 y)
  68. {
  69. fixed64 temp;
  70. if(x == 0)
  71. return 0;
  72. if(y == 0)
  73. return 0x7fffffff;
  74. temp = x;
  75. temp <<= PRECISION;
  76. return (fixed32)(temp / y);
  77. }
  78. fixed64 fixdiv64(fixed64 x, fixed64 y)
  79. {
  80. fixed64 temp;
  81. if(x == 0)
  82. return 0;
  83. if(y == 0)
  84. return 0x07ffffffffffffffLL;
  85. temp = x;
  86. temp <<= PRECISION64;
  87. return (fixed64)(temp / y);
  88. }
  89. fixed32 fixsqrt32(fixed32 x)
  90. {
  91. unsigned long r = 0, s, v = (unsigned long)x;
  92. #define STEP(k) s = r + (1 << k * 2); r >>= 1; \
  93. if (s <= v) { v -= s; r |= (1 << k * 2); }
  94. STEP(15);
  95. STEP(14);
  96. STEP(13);
  97. STEP(12);
  98. STEP(11);
  99. STEP(10);
  100. STEP(9);
  101. STEP(8);
  102. STEP(7);
  103. STEP(6);
  104. STEP(5);
  105. STEP(4);
  106. STEP(3);
  107. STEP(2);
  108. STEP(1);
  109. STEP(0);
  110. return (fixed32)(r << (PRECISION / 2));
  111. }
  112. /* Inverse gain of circular cordic rotation in s0.31 format. */
  113. static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
  114. /* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
  115. static const unsigned long atan_table[] = {
  116. 0x1fffffff, /* +0.785398163 (or pi/4) */
  117. 0x12e4051d, /* +0.463647609 */
  118. 0x09fb385b, /* +0.244978663 */
  119. 0x051111d4, /* +0.124354995 */
  120. 0x028b0d43, /* +0.062418810 */
  121. 0x0145d7e1, /* +0.031239833 */
  122. 0x00a2f61e, /* +0.015623729 */
  123. 0x00517c55, /* +0.007812341 */
  124. 0x0028be53, /* +0.003906230 */
  125. 0x00145f2e, /* +0.001953123 */
  126. 0x000a2f98, /* +0.000976562 */
  127. 0x000517cc, /* +0.000488281 */
  128. 0x00028be6, /* +0.000244141 */
  129. 0x000145f3, /* +0.000122070 */
  130. 0x0000a2f9, /* +0.000061035 */
  131. 0x0000517c, /* +0.000030518 */
  132. 0x000028be, /* +0.000015259 */
  133. 0x0000145f, /* +0.000007629 */
  134. 0x00000a2f, /* +0.000003815 */
  135. 0x00000517, /* +0.000001907 */
  136. 0x0000028b, /* +0.000000954 */
  137. 0x00000145, /* +0.000000477 */
  138. 0x000000a2, /* +0.000000238 */
  139. 0x00000051, /* +0.000000119 */
  140. 0x00000028, /* +0.000000060 */
  141. 0x00000014, /* +0.000000030 */
  142. 0x0000000a, /* +0.000000015 */
  143. 0x00000005, /* +0.000000007 */
  144. 0x00000002, /* +0.000000004 */
  145. 0x00000001, /* +0.000000002 */
  146. 0x00000000, /* +0.000000001 */
  147. 0x00000000, /* +0.000000000 */
  148. };
  149. /*
  150. Below here functions do not use standard fixed precision!
  151. */
  152. /**
  153. * Implements sin and cos using CORDIC rotation.
  154. *
  155. * @param phase has range from 0 to 0xffffffff, representing 0 and
  156. * 2*pi respectively.
  157. * @param cos return address for cos
  158. * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
  159. * representing -1 and 1 respectively.
  160. *
  161. * Gives at least 24 bits precision (last 2-8 bits or so are probably off)
  162. */
  163. long fsincos(unsigned long phase, fixed32 *cos)
  164. {
  165. int32_t x, x1, y, y1;
  166. unsigned long z, z1;
  167. int i;
  168. /* Setup initial vector */
  169. x = cordic_circular_gain;
  170. y = 0;
  171. z = phase;
  172. /* The phase has to be somewhere between 0..pi for this to work right */
  173. if (z < 0xffffffff / 4) {
  174. /* z in first quadrant, z += pi/2 to correct */
  175. x = -x;
  176. z += 0xffffffff / 4;
  177. } else if (z < 3 * (0xffffffff / 4)) {
  178. /* z in third quadrant, z -= pi/2 to correct */
  179. z -= 0xffffffff / 4;
  180. } else {
  181. /* z in fourth quadrant, z -= 3pi/2 to correct */
  182. x = -x;
  183. z -= 3 * (0xffffffff / 4);
  184. }
  185. /* Each iteration adds roughly 1-bit of extra precision */
  186. for (i = 0; i < 31; i++) {
  187. x1 = x >> i;
  188. y1 = y >> i;
  189. z1 = atan_table[i];
  190. /* Decided which direction to rotate vector. Pivot point is pi/2 */
  191. if (z >= 0xffffffff / 4) {
  192. x -= y1;
  193. y += x1;
  194. z -= z1;
  195. } else {
  196. x += y1;
  197. y -= x1;
  198. z += z1;
  199. }
  200. }
  201. if (cos)
  202. *cos = x;
  203. return y;
  204. }