summaryrefslogtreecommitdiff
path: root/lib/fixedpoint/fixedpoint.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fixedpoint/fixedpoint.c')
-rw-r--r--lib/fixedpoint/fixedpoint.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/fixedpoint/fixedpoint.c b/lib/fixedpoint/fixedpoint.c
index 645419d102..d1307bb248 100644
--- a/lib/fixedpoint/fixedpoint.c
+++ b/lib/fixedpoint/fixedpoint.c
@@ -211,6 +211,35 @@ long fp_sqrt(long x, unsigned int fracbits)
211 return g; 211 return g;
212} 212}
213 213
214/* raise an integer to an integer power */
215long ipow(long x, long y)
216{
217 /* y[k] = bit k of y, 0 or 1; k=0...n; n=|_ lg(y) _|
218 *
219 * x^y = x^(y[0]*2^0 + y[1]*2^1 + ... + y[n]*2^n)
220 * = x^(y[0]*2^0) * x^(y[1]*2^1) * ... * x^(y[n]*2^n)
221 */
222 long a = 1;
223
224 if (y < 0 && x != -1)
225 {
226 a = 0; /* would be < 1 or +inf if x == 0 */
227 }
228 else
229 {
230 while (y)
231 {
232 if (y & 1)
233 a *= x;
234
235 y /= 2;
236 x *= x;
237 }
238 }
239
240 return a;
241}
242
214/** 243/**
215 * Fixed point sinus using a lookup table 244 * Fixed point sinus using a lookup table
216 * don't forget to divide the result by 16384 to get the actual sinus value 245 * don't forget to divide the result by 16384 to get the actual sinus value