1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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 */
34 #define fixmul16(X,Y) \
38 asm volatile ( /* calculates: result = (X*Y)>>16 */ \
39 "smull %0,%1,%2,%3 \n\t" /* 64 = 32x32 multiply */ \
40 "mov %0, %0, lsr #16 \n\t" /* %0 = %0 >> 16 */ \
41 "orr %0, %0, %1, lsl #16 \n\t"/* result = %0 OR (%1 << 16) */ \
42 : "=&r"(low), "=&r" (high) \
47 #define fixmul31(X,Y) \
51 asm volatile ( /* calculates: result = (X*Y)>>31 */ \
52 "smull %0,%1,%2,%3 \n\t" /* 64 = 32x32 multiply */ \
53 "mov %0, %0, lsr #31 \n\t" /* %0 = %0 >> 31 */ \
54 "orr %0, %0, %1, lsl #1 \n\t" /* result = %0 OR (%1 << 1) */ \
55 : "=&r"(low), "=&r" (high) \
59 #elif defined(CPU_COLDFIRE)
60 #define fixmul16(X,Y) \
64 "mac.l %[x],%[y],%%acc0\n\t" /* multiply */ \
65 "mulu.l %[y],%[x] \n\t" /* get lower half, avoid emac stall */ \
66 "movclr.l %%acc0,%[t1] \n\t" /* get higher half */ \
67 "moveq.l #15,%[t2] \n\t" \
68 "asl.l %[t2],%[t1] \n\t" /* hi <<= 15, plus one free */ \
69 "moveq.l #16,%[t2] \n\t" \
70 "lsr.l %[t2],%[x] \n\t" /* (unsigned)lo >>= 16 */ \
71 "or.l %[x],%[t1] \n\t" /* combine result */ \
81 #define fixmul31(X,Y) \
85 "mac.l %[x], %[y], %%acc0\n\t" /* multiply */ \
86 "movclr.l %%acc0, %[t]\n\t" /* get higher half as result */ \
88 : [x] "r" ((X)), [y] "r" ((Y))); \
92 static inline int32_t fixmul16(int32_t x
, int32_t y
)
100 return (int32_t)temp
;
103 static inline int32_t fixmul31(int32_t x
, int32_t y
)
109 temp
>>= 31; //16+31-16 = 31 bits
111 return (int32_t)temp
;