rockpaint: steal the big buffer from audiobuffer
[kugel-rb.git] / apps / codecs / libatrac / fixp_math.h
blob8d2e0783b8fca5766f137b725176ada43904a1e7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Mohamed Tarek
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdlib.h>
22 #include <inttypes.h>
24 /* Macros for converting between various fixed-point representations and floating point. */
25 #define ONE_16 (1L << 16)
26 #define fixtof64(x) (float)((float)(x) / (float)(1 << 16)) //does not work on int64_t!
27 #define ftofix32(x) ((int32_t)((x) * (float)(1 << 16) + ((x) < 0 ? -0.5 : 0.5)))
28 #define ftofix31(x) ((int32_t)((x) * (float)(1 << 31) + ((x) < 0 ? -0.5 : 0.5)))
29 #define fix31tof64(x) (float)((float)(x) / (float)(1 << 31))
31 /* Fixed point math routines for use in atrac3.c */
33 #if defined(CPU_ARM)
34 /* Calculates: result = (X*Y)>>16 */
35 #define fixmul16(X,Y) \
36 ({ \
37 int32_t lo; \
38 int32_t hi; \
39 asm volatile ( \
40 "smull %[lo], %[hi], %[x], %[y] \n\t" /* multiply */ \
41 "mov %[lo], %[lo], lsr #16 \n\t" /* lo >>= 16 */ \
42 "orr %[lo], %[lo], %[hi], lsl #16" /* lo |= (hi << 16) */ \
43 : [lo]"=&r"(lo), [hi]"=&r"(hi) \
44 : [x]"r"(X), [y]"r"(Y)); \
45 lo; \
48 /* Calculates: result = (X*Y)>>31 */
49 /* Use scratch register r12 */
50 #define fixmul31(X,Y) \
51 ({ \
52 int32_t lo; \
53 int32_t hi; \
54 asm volatile ( \
55 "smull %[lo], %[hi], %[x], %[y] \n\t" /* multiply */ \
56 "mov %[lo], %[lo], lsr #31 \n\t" /* lo >>= 31 */ \
57 "orr %[lo], %[lo], %[hi], lsl #1" /* lo |= (hi << 1) */ \
58 : [lo]"=&r"(lo), [hi]"=&r"(hi) \
59 : [x]"r"(X), [y]"r"(Y)); \
60 lo; \
62 #elif defined(CPU_COLDFIRE)
63 #define fixmul16(X,Y) \
64 ({ \
65 int32_t t1, t2; \
66 asm volatile ( \
67 "mac.l %[x],%[y],%%acc0\n\t" /* multiply */ \
68 "mulu.l %[y],%[x] \n\t" /* get lower half, avoid emac stall */ \
69 "movclr.l %%acc0,%[t1] \n\t" /* get higher half */ \
70 "moveq.l #15,%[t2] \n\t" \
71 "asl.l %[t2],%[t1] \n\t" /* hi <<= 15, plus one free */ \
72 "moveq.l #16,%[t2] \n\t" \
73 "lsr.l %[t2],%[x] \n\t" /* (unsigned)lo >>= 16 */ \
74 "or.l %[x],%[t1] \n\t" /* combine result */ \
75 : /* outputs */ \
76 [t1]"=&d"(t1), \
77 [t2]"=&d"(t2) \
78 : /* inputs */ \
79 [x] "d" ((X)), \
80 [y] "d" ((Y))); \
81 t1; \
84 #define fixmul31(X,Y) \
85 ({ \
86 int32_t t; \
87 asm volatile ( \
88 "mac.l %[x], %[y], %%acc0\n\t" /* multiply */ \
89 "movclr.l %%acc0, %[t]\n\t" /* get higher half as result */ \
90 : [t] "=d" (t) \
91 : [x] "r" ((X)), [y] "r" ((Y))); \
92 t; \
94 #else
95 static inline int32_t fixmul16(int32_t x, int32_t y)
97 int64_t temp;
98 temp = x;
99 temp *= y;
101 temp >>= 16;
103 return (int32_t)temp;
106 static inline int32_t fixmul31(int32_t x, int32_t y)
108 int64_t temp;
109 temp = x;
110 temp *= y;
112 temp >>= 31; //16+31-16 = 31 bits
114 return (int32_t)temp;
116 #endif