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