Add the Rockbox GNU header to some files in libatrac.
[kugel-rb.git] / apps / codecs / libatrac / fixp_math.h
blobac53310cd2b1d72f7ea42746b8b2f45d345ecde3
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 #define fixmul16(X,Y) \
35 ({ \
36 int32_t low; \
37 int32_t high; \
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) \
43 : "r"(X),"r"(Y)); \
44 low; \
47 #define fixmul31(X,Y) \
48 ({ \
49 int32_t low; \
50 int32_t high; \
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) \
56 : "r"(X),"r"(Y)); \
57 low; \
59 #elif defined(CPU_COLDFIRE)
60 #define fixmul16(X,Y) \
61 ({ \
62 int32_t t1, t2; \
63 asm volatile ( \
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 */ \
72 : /* outputs */ \
73 [t1]"=&d"(t1), \
74 [t2]"=&d"(t2) \
75 : /* inputs */ \
76 [x] "d" ((X)), \
77 [y] "d" ((Y))); \
78 t1; \
81 #define fixmul31(X,Y) \
82 ({ \
83 int32_t t; \
84 asm volatile ( \
85 "mac.l %[x], %[y], %%acc0\n\t" /* multiply */ \
86 "movclr.l %%acc0, %[t]\n\t" /* get higher half as result */ \
87 : [t] "=d" (t) \
88 : [x] "r" ((X)), [y] "r" ((Y))); \
89 t; \
91 #else
92 static inline int32_t fixmul16(int32_t x, int32_t y)
94 int64_t temp;
95 temp = x;
96 temp *= y;
98 temp >>= 16;
100 return (int32_t)temp;
103 static inline int32_t fixmul31(int32_t x, int32_t y)
105 int64_t temp;
106 temp = x;
107 temp *= y;
109 temp >>= 31; //16+31-16 = 31 bits
111 return (int32_t)temp;
113 #endif