summaryrefslogtreecommitdiff
path: root/apps/fixedpoint.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/fixedpoint.h')
-rw-r--r--apps/fixedpoint.h197
1 files changed, 0 insertions, 197 deletions
diff --git a/apps/fixedpoint.h b/apps/fixedpoint.h
deleted file mode 100644
index b7acc759d7..0000000000
--- a/apps/fixedpoint.h
+++ /dev/null
@@ -1,197 +0,0 @@
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 * Shortcut: Multiply two fixed point numbers with 31 fractional bits:
35 * fp31_mul(x, y)
36 *
37 * Shortcut: Multiply two fixed point numbers with 31 fractional bits,
38 * then shift left by z bits:
39 * fp31_mulshl(x, y, z)
40 * NOTE: z must be in the range 1-8 on Coldfire targets.
41 *
42 * Divide two fixed point numbers:
43 * fp_div(x, y, fracbits)
44 *
45 * Take square root of a fixed point number:
46 * fp_sqrt(x, fracbits)
47 *
48 * Calculate sin and cos of an angle:
49 * fp_sincos(phase, *cos)
50 * where phase is a 32 bit unsigned integer with 0 representing 0
51 * and 0xFFFFFFFF representing 2*pi, and *cos is the address to
52 * a long signed integer. Value returned is a long signed integer
53 * from LONG_MIN to LONG_MAX, representing -1 to 1 respectively.
54 * That is, value is a fixed point integer with 31 fractional bits.
55 *
56 * Calculate sin or cos of an angle (very fast, from a table):
57 * fp14_sin(angle)
58 * fp14_cos(angle)
59 * where angle is a non-fixed point integer in degrees. Value
60 * returned is a fixed point integer with 14 fractional bits.
61 *
62 * Calculate decibel equivalent of a gain factor:
63 * fp_decibels(factor, fracbits)
64 * where fracbits is in the range 12 to 22 (higher is better),
65 * and factor is a positive fixed point integer.
66 *
67 * Calculate factor equivalent of a decibel value:
68 * fp_factor(decibels, fracbits)
69 * where fracbits is in the range 12 to 22 (lower is better),
70 * and decibels is a fixed point integer.
71 */
72
73#ifndef _FIXEDPOINT_H
74#define _FIXEDPOINT_H
75
76#include <inttypes.h>
77
78/* Redefine function names, making sure legacy code is usable */
79#define fp31_mul(x, y) FRACMUL(x, y)
80#define fp31_mulshl(x, y, z) FRACMUL_SHL(x, y, z)
81#define fp_div(x, y, z) DIV64(x, y, z)
82#define fp_sqrt(x, y) fsqrt(x, y)
83#define fp_sincos(x, y) fsincos(x, y)
84#define fp14_sin(x) sin_int(x)
85#define fp14_cos(x) cos_int(x)
86#define fp16_log(x) flog(x)
87
88
89#define fp_mul(x, y, z) (long)((((long long)(x)) * ((long long)(y))) >> (z))
90#define DIV64(x, y, z) (long)((((long long)(x)) << (z)) / ((long long)(y)))
91
92/** TAKEN FROM apps/dsp.h */
93/* A bunch of fixed point assembler helper macros */
94#if defined(CPU_COLDFIRE)
95/* These macros use the Coldfire EMAC extension and need the MACSR flags set
96 * to fractional mode with no rounding.
97 */
98
99/* Multiply two S.31 fractional integers and return the sign bit and the
100 * 31 most significant bits of the result.
101 */
102#define FRACMUL(x, y) \
103({ \
104 long t; \
105 asm ("mac.l %[a], %[b], %%acc0\n\t" \
106 "movclr.l %%acc0, %[t]\n\t" \
107 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \
108 t; \
109})
110
111/* Multiply two S.31 fractional integers, and return the 32 most significant
112 * bits after a shift left by the constant z. NOTE: Only works for shifts of
113 * 1 to 8 on Coldfire!
114 */
115#define FRACMUL_SHL(x, y, z) \
116({ \
117 long t, t2; \
118 asm ("mac.l %[a], %[b], %%acc0\n\t" \
119 "moveq.l %[d], %[t]\n\t" \
120 "move.l %%accext01, %[t2]\n\t" \
121 "and.l %[mask], %[t2]\n\t" \
122 "lsr.l %[t], %[t2]\n\t" \
123 "movclr.l %%acc0, %[t]\n\t" \
124 "asl.l %[c], %[t]\n\t" \
125 "or.l %[t2], %[t]\n\t" \
126 : [t] "=&d" (t), [t2] "=&d" (t2) \
127 : [a] "r" (x), [b] "r" (y), [mask] "d" (0xff), \
128 [c] "i" ((z)), [d] "i" (8 - (z))); \
129 t; \
130})
131
132#elif defined(CPU_ARM)
133
134/* Multiply two S.31 fractional integers and return the sign bit and the
135 * 31 most significant bits of the result.
136 */
137#define FRACMUL(x, y) \
138({ \
139 long t, t2; \
140 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
141 "mov %[t2], %[t2], asl #1\n\t" \
142 "orr %[t], %[t2], %[t], lsr #31\n\t" \
143 : [t] "=&r" (t), [t2] "=&r" (t2) \
144 : [a] "r" (x), [b] "r" (y)); \
145 t; \
146})
147
148/* Multiply two S.31 fractional integers, and return the 32 most significant
149 * bits after a shift left by the constant z.
150 */
151#define FRACMUL_SHL(x, y, z) \
152({ \
153 long t, t2; \
154 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
155 "mov %[t2], %[t2], asl %[c]\n\t" \
156 "orr %[t], %[t2], %[t], lsr %[d]\n\t" \
157 : [t] "=&r" (t), [t2] "=&r" (t2) \
158 : [a] "r" (x), [b] "r" (y), \
159 [c] "M" ((z) + 1), [d] "M" (31 - (z))); \
160 t; \
161})
162
163#else
164
165#define FRACMUL(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 31))
166#define FRACMUL_SHL(x, y, z) \
167((long)(((((long long) (x)) * ((long long) (y))) >> (31 - (z)))))
168
169#endif
170
171/** TAKEN FROM ORIGINAL fixedpoint.h */
172/* fast unsigned multiplication (16x16bit->32bit or 32x32bit->32bit,
173 * whichever is faster for the architecture) */
174#ifdef CPU_ARM
175#define FMULU(a, b) ((uint32_t) (((uint32_t) (a)) * ((uint32_t) (b))))
176#else /* SH1, coldfire */
177#define FMULU(a, b) ((uint32_t) (((uint16_t) (a)) * ((uint16_t) (b))))
178#endif
179
180long fsincos(unsigned long phase, long *cos);
181long fsqrt(long x, unsigned int fracbits);
182long sin_int(int val);
183long cos_int(int val);
184long flog(int x);
185
186
187/** MODIFIED FROM replaygain.c */
188#define FP_INF (0x7fffffff)
189#define FP_NEGINF -(0x7fffffff)
190
191/* fracbits in range 12 - 22 work well. Higher is better for
192 * calculating dB, lower is better for calculating ratio.
193 */
194long fp_decibels(unsigned long factor, unsigned int fracbits);
195long fp_factor(long decibels, unsigned int fracbits);
196
197#endif