1 /****************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 Michael Giacomelli
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 /* fixed precision code. We use a combination of Sign 15.16 and Sign.31
24 The WMA decoder does not always follow this convention, and occasionally
25 renormalizes values to other formats in order to maximize precision.
26 However, only the two precisions above are provided in this file.
33 #define PRECISION64 16
36 #define fixtof64(x) (float)((float)(x) / (float)(1 << PRECISION64)) //does not work on int64_t!
37 #define ftofix32(x) ((fixed32)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
38 #define itofix64(x) (IntTo64(x))
39 #define itofix32(x) ((x) << PRECISION)
40 #define fixtoi32(x) ((x) >> PRECISION)
41 #define fixtoi64(x) (IntFrom64(x))
46 fixed64
IntTo64(int x
);
47 int IntFrom64(fixed64 x
);
48 fixed32
Fixed32From64(fixed64 x
);
49 fixed64
Fixed32To64(fixed32 x
);
50 fixed32
fixdiv32(fixed32 x
, fixed32 y
);
51 fixed64
fixdiv64(fixed64 x
, fixed64 y
);
52 fixed32
fixsqrt32(fixed32 x
);
53 long fsincos(unsigned long phase
, fixed32
*cos
);
57 /*Sign-15.16 format */
59 #define fixmul32(x, y) \
63 asm ("smull %0, %1, %3, %4\n\t" \
64 "movs %0, %0, lsr %5\n\t" \
65 "adc %2, %0, %1, lsl %6" \
66 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
67 : "%r" (x), "r" (y), \
68 "M" (PRECISION), "M" (32 - PRECISION) \
73 #define fixmul32b(x, y) \
77 asm ("smull %0, %1, %3, %4\n\t" \
78 "movs %2, %1, lsl #1" \
79 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
85 #elif defined(CPU_COLDFIRE)
87 static inline int32_t fixmul32(int32_t x
, int32_t y
)
90 #warning Coldfire fixmul32() only works for PRECISION == 16
94 "mac.l %[x], %[y], %%acc0 \n" /* multiply */
95 "mulu.l %[y], %[x] \n" /* get lower half, avoid emac stall */
96 "movclr.l %%acc0, %[t1] \n" /* get higher half */
98 "move.w %[t1], %[x] \n"
100 : [t1
] "=&d" (t1
), [x
] "+d" (x
)
106 static inline int32_t fixmul32b(int32_t x
, int32_t y
)
109 "mac.l %[x], %[y], %%acc0 \n" /* multiply */
110 "movclr.l %%acc0, %[x] \n" /* get higher half */
119 static inline fixed32
fixmul32(fixed32 x
, fixed32 y
)
127 return (fixed32
)temp
;
130 static inline fixed32
fixmul32b(fixed32 x
, fixed32 y
)
137 temp
>>= 31; //16+31-16 = 31 bits
139 return (fixed32
)temp
;