summaryrefslogtreecommitdiff
path: root/firmware/include/fixedpoint.h
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2013-04-10 13:24:32 -0400
committerMichael Sevakis <jethead71@rockbox.org>2013-04-10 13:28:35 -0400
commitf49e75053198cc24aff92e9045795e011fa25083 (patch)
treea6800dbd34679289a44f954ca92d6d36fff89978 /firmware/include/fixedpoint.h
parentf5b7134f59fa20685a51d56d3a323044a2c441cf (diff)
downloadrockbox-f49e75053198cc24aff92e9045795e011fa25083.tar.gz
rockbox-f49e75053198cc24aff92e9045795e011fa25083.zip
Move fixedpoint.h to be accessible in /firmware.
Will need it soon enough. Combine the contents of all the various fixedpoint.h files. Not moving fixedpoint.c for now since I'm not sure where it should be and it causes some dependency issues. Change-Id: Ideacbca2ca78f9158c2b114b113c274f68e908d5
Diffstat (limited to 'firmware/include/fixedpoint.h')
-rw-r--r--firmware/include/fixedpoint.h111
1 files changed, 111 insertions, 0 deletions
diff --git a/firmware/include/fixedpoint.h b/firmware/include/fixedpoint.h
new file mode 100644
index 0000000000..3e14bdd68e
--- /dev/null
+++ b/firmware/include/fixedpoint.h
@@ -0,0 +1,111 @@
1/***************************************************************************
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 *
14 * 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.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24/** FIXED POINT MATH ROUTINES - USAGE
25 *
26 * - x and y arguments are fixed point integers
27 * - fracbits is the number of fractional bits in the argument(s)
28 * - functions return long fixed point integers with the specified number
29 * of fractional bits unless otherwise specified
30 *
31 * Multiply two fixed point numbers:
32 * fp_mul(x, y, fracbits)
33 *
34 * Divide two fixed point numbers:
35 * fp_div(x, y, fracbits)
36 *
37 * Calculate sin and cos of an angle:
38 * fp_sincos(phase, *cos)
39 * where phase is a 32 bit unsigned integer with 0 representing 0
40 * and 0xFFFFFFFF representing 2*pi, and *cos is the address to
41 * a long signed integer. Value returned is a long signed integer
42 * from -0x80000000 to 0x7fffffff, representing -1 to 1 respectively.
43 * That is, value is a fixed point integer with 31 fractional bits.
44 *
45 * Take square root of a fixed point number:
46 * fp_sqrt(x, fracbits)
47 *
48 * Take the square root of an integer:
49 * isqrt(x)
50 *
51 * Calculate sin or cos of an angle (very fast, from a table):
52 * fp14_sin(angle)
53 * fp14_cos(angle)
54 * where angle is a non-fixed point integer in degrees. Value
55 * returned is a fixed point integer with 14 fractional bits.
56 *
57 * Calculate the exponential of a fixed point integer
58 * fp16_exp(x)
59 * where x and the value returned are fixed point integers
60 * with 16 fractional bits.
61 *
62 * Calculate the natural log of a positive fixed point integer
63 * fp16_log(x)
64 * where x and the value returned are fixed point integers
65 * with 16 fractional bits.
66 *
67 * Calculate decibel equivalent of a gain factor:
68 * fp_decibels(factor, fracbits)
69 * where fracbits is in the range 12 to 22 (higher is better),
70 * and factor is a positive fixed point integer.
71 *
72 * Calculate factor equivalent of a decibel value:
73 * fp_factor(decibels, fracbits)
74 * where fracbits is in the range 12 to 22 (lower is better),
75 * and decibels is a fixed point integer.
76 */
77
78#ifndef FIXEDPOINT_H
79#define FIXEDPOINT_H
80
81#define fp_mul(x, y, z) (long)((((long long)(x)) * ((long long)(y))) >> (z))
82#define fp_div(x, y, z) (long)((((long long)(x)) << (z)) / ((long long)(y)))
83
84long fp_sincos(unsigned long phase, long *cos);
85long fp_sqrt(long a, unsigned int fracbits);
86long fp14_cos(int val);
87long fp14_sin(int val);
88long fp16_log(int x);
89long fp16_exp(int x);
90
91unsigned long isqrt(unsigned long x);
92
93/* fast unsigned multiplication (16x16bit->32bit or 32x32bit->32bit,
94 * whichever is faster for the architecture) */
95#ifdef CPU_ARM
96#define FMULU(a, b) ((uint32_t) (((uint32_t) (a)) * ((uint32_t) (b))))
97#else /* SH1, coldfire */
98#define FMULU(a, b) ((uint32_t) (((uint16_t) (a)) * ((uint16_t) (b))))
99#endif
100
101/** MODIFIED FROM replaygain.c */
102#define FP_INF (0x7fffffff)
103#define FP_NEGINF -(0x7fffffff)
104
105/* fracbits in range 12 - 22 work well. Higher is better for
106 * calculating dB, lower is better for calculating factor.
107 */
108/* long fp_decibels(unsigned long factor, unsigned int fracbits); */
109long fp_factor(long decibels, unsigned int fracbits);
110
111#endif /* FIXEDPOINT_H */