Consolidate all fixed point math routines in one library (FS#10400) by Jeffrey Goode
[kugel-rb.git] / apps / fixedpoint.h
bloba3ca6ee6ed9a94bc8588336db14066e71966dab3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: fixedpoint.h -1 $
10 * Copyright (C) 2006 Jens Arnold
12 * Fixed point library for plugins
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 /** FIXED POINT MATH ROUTINES - USAGE
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
31 * Multiply two fixed point numbers:
32 * fp_mul(x, y, fracbits)
34 * Shortcut: Multiply two fixed point numbers with 31 fractional bits:
35 * fp31_mul(x, y)
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.
42 * Divide two fixed point numbers:
43 * fp_div(x, y, fracbits)
45 * Take square root of a fixed point number:
46 * fp_sqrt(x, fracbits)
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.
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.
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.
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.
73 #ifndef _FIXEDPOINT_H
74 #define _FIXEDPOINT_H
76 #include <inttypes.h>
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)
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)))
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.
99 /* Multiply two S.31 fractional integers and return the sign bit and the
100 * 31 most significant bits of the result.
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; \
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!
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; \
132 #elif defined(CPU_ARM)
134 /* Multiply two S.31 fractional integers and return the sign bit and the
135 * 31 most significant bits of the result.
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; \
148 /* Multiply two S.31 fractional integers, and return the 32 most significant
149 * bits after a shift left by the constant z.
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; \
163 #else
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)))))
169 #endif
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
180 long fsincos(unsigned long phase, long *cos);
181 long fsqrt(long x, unsigned int fracbits);
182 long sin_int(int val);
183 long cos_int(int val);
184 long flog(int x);
187 /** MODIFIED FROM replaygain.c */
188 #define FP_INF (0x7fffffff)
189 #define FP_NEGINF -(0x7fffffff)
191 /* fracbits in range 12 - 22 work well. Higher is better for
192 * calculating dB, lower is better for calculating ratio.
194 long fp_decibels(unsigned long factor, unsigned int fracbits);
195 long fp_factor(long decibels, unsigned int fracbits);
197 #endif