Michael Sevakis | f49e750 | 2013-04-10 13:24:32 -0400 | [diff] [blame] | 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 | * |
Michael Sevakis | f49e750 | 2013-04-10 13:24:32 -0400 | [diff] [blame] | 48 | * Calculate sin or cos of an angle (very fast, from a table): |
| 49 | * fp14_sin(angle) |
| 50 | * fp14_cos(angle) |
| 51 | * where angle is a non-fixed point integer in degrees. Value |
| 52 | * returned is a fixed point integer with 14 fractional bits. |
| 53 | * |
| 54 | * Calculate the exponential of a fixed point integer |
| 55 | * fp16_exp(x) |
| 56 | * where x and the value returned are fixed point integers |
| 57 | * with 16 fractional bits. |
| 58 | * |
| 59 | * Calculate the natural log of a positive fixed point integer |
| 60 | * fp16_log(x) |
| 61 | * where x and the value returned are fixed point integers |
| 62 | * with 16 fractional bits. |
| 63 | * |
| 64 | * Calculate decibel equivalent of a gain factor: |
| 65 | * fp_decibels(factor, fracbits) |
| 66 | * where fracbits is in the range 12 to 22 (higher is better), |
| 67 | * and factor is a positive fixed point integer. |
| 68 | * |
| 69 | * Calculate factor equivalent of a decibel value: |
| 70 | * fp_factor(decibels, fracbits) |
| 71 | * where fracbits is in the range 12 to 22 (lower is better), |
| 72 | * and decibels is a fixed point integer. |
| 73 | */ |
| 74 | |
| 75 | #ifndef FIXEDPOINT_H |
| 76 | #define FIXEDPOINT_H |
| 77 | |
| 78 | #define fp_mul(x, y, z) (long)((((long long)(x)) * ((long long)(y))) >> (z)) |
| 79 | #define fp_div(x, y, z) (long)((((long long)(x)) << (z)) / ((long long)(y))) |
| 80 | |
| 81 | long fp_sincos(unsigned long phase, long *cos); |
| 82 | long fp_sqrt(long a, unsigned int fracbits); |
| 83 | long fp14_cos(int val); |
| 84 | long fp14_sin(int val); |
| 85 | long fp16_log(int x); |
| 86 | long fp16_exp(int x); |
| 87 | |
Michael Sevakis | aced667 | 2017-09-18 06:00:05 -0400 | [diff] [blame] | 88 | long ipow(long x, long y); |
| 89 | |
Michael Sevakis | f49e750 | 2013-04-10 13:24:32 -0400 | [diff] [blame] | 90 | /* fast unsigned multiplication (16x16bit->32bit or 32x32bit->32bit, |
| 91 | * whichever is faster for the architecture) */ |
| 92 | #ifdef CPU_ARM |
| 93 | #define FMULU(a, b) ((uint32_t) (((uint32_t) (a)) * ((uint32_t) (b)))) |
| 94 | #else /* SH1, coldfire */ |
| 95 | #define FMULU(a, b) ((uint32_t) (((uint16_t) (a)) * ((uint16_t) (b)))) |
| 96 | #endif |
| 97 | |
| 98 | /** MODIFIED FROM replaygain.c */ |
| 99 | #define FP_INF (0x7fffffff) |
| 100 | #define FP_NEGINF -(0x7fffffff) |
| 101 | |
Michael Sevakis | 0c7b787 | 2013-04-12 23:35:47 -0400 | [diff] [blame] | 102 | /** FIXED POINT EXP10 |
| 103 | * Return 10^x as FP integer. Argument is FP integer. |
| 104 | */ |
| 105 | long fp_exp10(long x, unsigned int fracbits); |
| 106 | |
Michael Sevakis | 95e23de | 2013-04-16 17:47:58 -0400 | [diff] [blame] | 107 | /** FIXED POINT LOG10 |
| 108 | * Return log10(x) as FP integer. Argument is FP integer. |
| 109 | */ |
| 110 | long fp_log10(long n, unsigned int fracbits); |
| 111 | |
Michael Sevakis | f49e750 | 2013-04-10 13:24:32 -0400 | [diff] [blame] | 112 | /* fracbits in range 12 - 22 work well. Higher is better for |
| 113 | * calculating dB, lower is better for calculating factor. |
| 114 | */ |
Michael Sevakis | 95e23de | 2013-04-16 17:47:58 -0400 | [diff] [blame] | 115 | |
| 116 | /** CONVERT FACTOR TO DECIBELS */ |
| 117 | long fp_decibels(unsigned long factor, unsigned int fracbits); |
| 118 | |
| 119 | /** CONVERT DECIBELS TO FACTOR */ |
Michael Sevakis | f49e750 | 2013-04-10 13:24:32 -0400 | [diff] [blame] | 120 | long fp_factor(long decibels, unsigned int fracbits); |
| 121 | |
| 122 | #endif /* FIXEDPOINT_H */ |