summaryrefslogtreecommitdiff
path: root/lib/fixedpoint/fixedpoint.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fixedpoint/fixedpoint.h')
-rw-r--r--lib/fixedpoint/fixedpoint.h125
1 files changed, 125 insertions, 0 deletions
diff --git a/lib/fixedpoint/fixedpoint.h b/lib/fixedpoint/fixedpoint.h
new file mode 100644
index 0000000000..31d60eca4b
--- /dev/null
+++ b/lib/fixedpoint/fixedpoint.h
@@ -0,0 +1,125 @@
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/** FIXED POINT EXP10
106 * Return 10^x as FP integer. Argument is FP integer.
107 */
108long fp_exp10(long x, unsigned int fracbits);
109
110/** FIXED POINT LOG10
111 * Return log10(x) as FP integer. Argument is FP integer.
112 */
113long fp_log10(long n, unsigned int fracbits);
114
115/* fracbits in range 12 - 22 work well. Higher is better for
116 * calculating dB, lower is better for calculating factor.
117 */
118
119/** CONVERT FACTOR TO DECIBELS */
120long fp_decibels(unsigned long factor, unsigned int fracbits);
121
122/** CONVERT DECIBELS TO FACTOR */
123long fp_factor(long decibels, unsigned int fracbits);
124
125#endif /* FIXEDPOINT_H */