Consolidate all fixed point math routines in one library (FS#10400) by Jeffrey Goode
[kugel-rb.git] / apps / codecs / lib / fixedpoint.h
blob54ece27080b75622dc05b3fd54739cecc14a5f05
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 #ifndef _FIXEDPOINT_H
25 #define _FIXEDPOINT_H
27 #include <inttypes.h>
29 /** TAKEN FROM apps/dsp.h */
30 /* A bunch of fixed point assembler helper macros */
31 #if defined(CPU_COLDFIRE)
32 /* These macros use the Coldfire EMAC extension and need the MACSR flags set
33 * to fractional mode with no rounding.
36 /* Multiply two S.31 fractional integers and return the sign bit and the
37 * 31 most significant bits of the result.
39 #define FRACMUL(x, y) \
40 ({ \
41 long t; \
42 asm ("mac.l %[a], %[b], %%acc0\n\t" \
43 "movclr.l %%acc0, %[t]\n\t" \
44 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \
45 t; \
48 /* Multiply two S.31 fractional integers, and return the 32 most significant
49 * bits after a shift left by the constant z. NOTE: Only works for shifts of
50 * 1 to 8 on Coldfire!
52 #define FRACMUL_SHL(x, y, z) \
53 ({ \
54 long t, t2; \
55 asm ("mac.l %[a], %[b], %%acc0\n\t" \
56 "moveq.l %[d], %[t]\n\t" \
57 "move.l %%accext01, %[t2]\n\t" \
58 "and.l %[mask], %[t2]\n\t" \
59 "lsr.l %[t], %[t2]\n\t" \
60 "movclr.l %%acc0, %[t]\n\t" \
61 "asl.l %[c], %[t]\n\t" \
62 "or.l %[t2], %[t]\n\t" \
63 : [t] "=&d" (t), [t2] "=&d" (t2) \
64 : [a] "r" (x), [b] "r" (y), [mask] "d" (0xff), \
65 [c] "i" ((z)), [d] "i" (8 - (z))); \
66 t; \
69 #elif defined(CPU_ARM)
71 /* Multiply two S.31 fractional integers and return the sign bit and the
72 * 31 most significant bits of the result.
74 #define FRACMUL(x, y) \
75 ({ \
76 long t, t2; \
77 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
78 "mov %[t2], %[t2], asl #1\n\t" \
79 "orr %[t], %[t2], %[t], lsr #31\n\t" \
80 : [t] "=&r" (t), [t2] "=&r" (t2) \
81 : [a] "r" (x), [b] "r" (y)); \
82 t; \
85 /* Multiply two S.31 fractional integers, and return the 32 most significant
86 * bits after a shift left by the constant z.
88 #define FRACMUL_SHL(x, y, z) \
89 ({ \
90 long t, t2; \
91 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
92 "mov %[t2], %[t2], asl %[c]\n\t" \
93 "orr %[t], %[t2], %[t], lsr %[d]\n\t" \
94 : [t] "=&r" (t), [t2] "=&r" (t2) \
95 : [a] "r" (x), [b] "r" (y), \
96 [c] "M" ((z) + 1), [d] "M" (31 - (z))); \
97 t; \
100 #else
102 #define FRACMUL(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 31))
103 #define FRACMUL_SHL(x, y, z) \
104 ((long)(((((long long) (x)) * ((long long) (y))) >> (31 - (z)))))
106 #endif
108 #define DIV64(x, y, z) (long)(((long long)(x) << (z))/(y))
111 /** TAKEN FROM ORIGINAL fixedpoint.h */
112 /* fast unsigned multiplication (16x16bit->32bit or 32x32bit->32bit,
113 * whichever is faster for the architecture) */
114 #ifdef CPU_ARM
115 #define FMULU(a, b) ((uint32_t) (((uint32_t) (a)) * ((uint32_t) (b))))
116 #else /* SH1, coldfire */
117 #define FMULU(a, b) ((uint32_t) (((uint16_t) (a)) * ((uint16_t) (b))))
118 #endif
120 long fsincos(unsigned long phase, long *cos);
121 long fsqrt(long a, unsigned int fracbits);
122 long cos_int(int val);
123 long sin_int(int val);
124 long flog(int x);
126 #endif