blob: 7b9b68a1e8843ac3e5d9250f73046437ac505a56 [file] [log] [blame]
Jens Arnoldf98fd722006-12-10 22:50:00 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 Jens Arnold
11 *
12 * Fixed point library for plugins
13 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000014 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
Jens Arnoldf98fd722006-12-10 22:50:00 +000018 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24#include <inttypes.h>
Bertrik Sikkena36c8f12008-05-04 13:21:07 +000025#include "fixedpoint.h"
Jens Arnoldf98fd722006-12-10 22:50:00 +000026
27/* Inverse gain of circular cordic rotation in s0.31 format. */
28static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
29
30/* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
31static const unsigned long atan_table[] = {
32 0x1fffffff, /* +0.785398163 (or pi/4) */
33 0x12e4051d, /* +0.463647609 */
34 0x09fb385b, /* +0.244978663 */
35 0x051111d4, /* +0.124354995 */
36 0x028b0d43, /* +0.062418810 */
37 0x0145d7e1, /* +0.031239833 */
38 0x00a2f61e, /* +0.015623729 */
39 0x00517c55, /* +0.007812341 */
40 0x0028be53, /* +0.003906230 */
41 0x00145f2e, /* +0.001953123 */
42 0x000a2f98, /* +0.000976562 */
43 0x000517cc, /* +0.000488281 */
44 0x00028be6, /* +0.000244141 */
45 0x000145f3, /* +0.000122070 */
46 0x0000a2f9, /* +0.000061035 */
47 0x0000517c, /* +0.000030518 */
48 0x000028be, /* +0.000015259 */
49 0x0000145f, /* +0.000007629 */
50 0x00000a2f, /* +0.000003815 */
51 0x00000517, /* +0.000001907 */
52 0x0000028b, /* +0.000000954 */
53 0x00000145, /* +0.000000477 */
54 0x000000a2, /* +0.000000238 */
55 0x00000051, /* +0.000000119 */
56 0x00000028, /* +0.000000060 */
57 0x00000014, /* +0.000000030 */
58 0x0000000a, /* +0.000000015 */
59 0x00000005, /* +0.000000007 */
60 0x00000002, /* +0.000000004 */
61 0x00000001, /* +0.000000002 */
62 0x00000000, /* +0.000000001 */
63 0x00000000, /* +0.000000000 */
64};
65
Kevin Ferraredf4f56b2007-07-31 04:59:03 +000066/* Precalculated sine and cosine * 16384 (2^14) (fixed point 18.14) */
67static const short sin_table[91] =
68{
69 0, 285, 571, 857, 1142, 1427, 1712, 1996, 2280, 2563,
70 2845, 3126, 3406, 3685, 3963, 4240, 4516, 4790, 5062, 5334,
71 5603, 5871, 6137, 6401, 6663, 6924, 7182, 7438, 7691, 7943,
72 8191, 8438, 8682, 8923, 9161, 9397, 9630, 9860, 10086, 10310,
73 10531, 10748, 10963, 11173, 11381, 11585, 11785, 11982, 12175, 12365,
74 12550, 12732, 12910, 13084, 13254, 13420, 13582, 13740, 13894, 14043,
75 14188, 14329, 14466, 14598, 14725, 14848, 14967, 15081, 15190, 15295,
76 15395, 15491, 15582, 15668, 15749, 15825, 15897, 15964, 16025, 16082,
77 16135, 16182, 16224, 16261, 16294, 16321, 16344, 16361, 16374, 16381,
78 16384
79};
80
Jens Arnoldf98fd722006-12-10 22:50:00 +000081/**
82 * Implements sin and cos using CORDIC rotation.
83 *
84 * @param phase has range from 0 to 0xffffffff, representing 0 and
85 * 2*pi respectively.
86 * @param cos return address for cos
87 * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
88 * representing -1 and 1 respectively.
89 */
90long fsincos(unsigned long phase, long *cos)
91{
92 int32_t x, x1, y, y1;
93 unsigned long z, z1;
94 int i;
95
96 /* Setup initial vector */
97 x = cordic_circular_gain;
98 y = 0;
99 z = phase;
100
101 /* The phase has to be somewhere between 0..pi for this to work right */
102 if (z < 0xffffffff / 4) {
103 /* z in first quadrant, z += pi/2 to correct */
104 x = -x;
105 z += 0xffffffff / 4;
106 } else if (z < 3 * (0xffffffff / 4)) {
107 /* z in third quadrant, z -= pi/2 to correct */
108 z -= 0xffffffff / 4;
109 } else {
110 /* z in fourth quadrant, z -= 3pi/2 to correct */
111 x = -x;
112 z -= 3 * (0xffffffff / 4);
113 }
114
115 /* Each iteration adds roughly 1-bit of extra precision */
116 for (i = 0; i < 31; i++) {
117 x1 = x >> i;
118 y1 = y >> i;
119 z1 = atan_table[i];
120
121 /* Decided which direction to rotate vector. Pivot point is pi/2 */
122 if (z >= 0xffffffff / 4) {
123 x -= y1;
124 y += x1;
125 z -= z1;
126 } else {
127 x += y1;
128 y -= x1;
129 z += z1;
130 }
131 }
132
133 if (cos)
134 *cos = x;
135
136 return y;
137}
Thom Johansen5f48e152007-02-05 01:01:15 +0000138
139/**
140 * Fixed point square root via Newton-Raphson.
141 * @param a square root argument.
142 * @param fracbits specifies number of fractional bits in argument.
143 * @return Square root of argument in same fixed point format as input.
144 */
145long fsqrt(long a, unsigned int fracbits)
146{
147 long b = a/2 + (1 << fracbits); /* initial approximation */
148 unsigned n;
149 const unsigned iterations = 4;
150
151 for (n = 0; n < iterations; ++n)
Thom Johansen5ba289b2007-02-05 01:18:29 +0000152 b = (b + (long)(((long long)(a) << fracbits)/b))/2;
Thom Johansen5f48e152007-02-05 01:01:15 +0000153
154 return b;
155}
156
Kevin Ferraredf4f56b2007-07-31 04:59:03 +0000157/**
158 * Fixed point sinus using a lookup table
159 * don't forget to divide the result by 16384 to get the actual sinus value
160 * @param val sinus argument in degree
161 * @return sin(val)*16384
162 */
163long sin_int(int val)
164{
165 val = (val+360)%360;
166 if (val < 181)
167 {
168 if (val < 91)/* phase 0-90 degree */
169 return (long)sin_table[val];
170 else/* phase 91-180 degree */
171 return (long)sin_table[180-val];
172 }
173 else
174 {
175 if (val < 271)/* phase 181-270 degree */
176 return -(long)sin_table[val-180];
177 else/* phase 270-359 degree */
178 return -(long)sin_table[360-val];
179 }
180 return 0;
181}
182
183/**
184 * Fixed point cosinus using a lookup table
185 * don't forget to divide the result by 16384 to get the actual cosinus value
186 * @param val sinus argument in degree
187 * @return cos(val)*16384
188 */
189long cos_int(int val)
190{
191 val = (val+360)%360;
192 if (val < 181)
193 {
194 if (val < 91)/* phase 0-90 degree */
195 return (long)sin_table[90-val];
196 else/* phase 91-180 degree */
197 return -(long)sin_table[val-90];
198 }
199 else
200 {
201 if (val < 271)/* phase 181-270 degree */
202 return -(long)sin_table[270-val];
203 else/* phase 270-359 degree */
204 return (long)sin_table[val-270];
205 }
206 return 0;
207}
Robert Keevil98e60732007-07-31 17:23:49 +0000208
209/**
210 * Fixed-point natural log
211 * taken from http://www.quinapalus.com/efunc.html
212 * "The code assumes integers are at least 32 bits long. The (positive)
213 * argument and the result of the function are both expressed as fixed-point
214 * values with 16 fractional bits, although intermediates are kept with 28
215 * bits of precision to avoid loss of accuracy during shifts."
216 */
217
218long flog(int x) {
219 long t,y;
220
221 y=0xa65af;
222 if(x<0x00008000) x<<=16, y-=0xb1721;
223 if(x<0x00800000) x<<= 8, y-=0x58b91;
224 if(x<0x08000000) x<<= 4, y-=0x2c5c8;
225 if(x<0x20000000) x<<= 2, y-=0x162e4;
226 if(x<0x40000000) x<<= 1, y-=0x0b172;
227 t=x+(x>>1); if((t&0x80000000)==0) x=t,y-=0x067cd;
228 t=x+(x>>2); if((t&0x80000000)==0) x=t,y-=0x03920;
229 t=x+(x>>3); if((t&0x80000000)==0) x=t,y-=0x01e27;
230 t=x+(x>>4); if((t&0x80000000)==0) x=t,y-=0x00f85;
231 t=x+(x>>5); if((t&0x80000000)==0) x=t,y-=0x007e1;
232 t=x+(x>>6); if((t&0x80000000)==0) x=t,y-=0x003f8;
233 t=x+(x>>7); if((t&0x80000000)==0) x=t,y-=0x001fe;
234 x=0x80000000-x;
235 y-=x>>15;
236 return y;
237}