lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / libmusepack / mpcdec_math.h
blobf4c87324b5b6cbdd8b57c0ca92187e8ded3dab82
1 /*
2 Copyright (c) 2005, The Musepack Development Team
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
20 written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /// \file math.h
36 /// Libmpcdec internal math routines.
38 #ifndef _mpcdec_math_h_
39 #define _mpcdec_math_h_
41 #include "mpc_types.h"
43 #define MPC_FIXED_POINT_SHIFT 16
45 #ifdef MPC_FIXED_POINT
47 #ifdef _WIN32_WCE
48 #include <cmnintrin.h>
49 #define MPC_HAVE_MULHIGH
50 #endif
52 typedef mpc_int64_t MPC_SAMPLE_FORMAT_MULTIPLY;
54 #define MAKE_MPC_SAMPLE(X) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<MPC_FIXED_POINT_FRACTPART))
55 #define MAKE_MPC_SAMPLE_EX(X,Y) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<(Y)))
57 #define MPC_SHR_RND(X, Y) ((X+(1<<(Y-1)))>>Y)
59 #if defined(CPU_COLDFIRE)
60 /* Calculate: result = (X*Y)>>14 */
61 #define MPC_MULTIPLY(X,Y) \
62 ({ \
63 MPC_SAMPLE_FORMAT t1; \
64 MPC_SAMPLE_FORMAT t2; \
65 asm volatile ( \
66 "mac.l %[x],%[y],%%acc0\n\t" /* multiply */ \
67 "mulu.l %[y],%[x] \n\t" /* get lower half, avoid emac stall */ \
68 "movclr.l %%acc0,%[t1] \n\t" /* get higher half */ \
69 "moveq.l #17,%[t2] \n\t" \
70 "asl.l %[t2],%[t1] \n\t" /* hi <<= 17, plus one free */ \
71 "moveq.l #14,%[t2] \n\t" \
72 "lsr.l %[t2],%[x] \n\t" /* (unsigned)lo >>= 14 */ \
73 "or.l %[x],%[t1] \n" /* combine result */ \
74 : [t1]"=&d"(t1), [t2]"=&d"(t2) \
75 : [x]"d"((X)), [y] "d"((Y))); \
76 t1; \
79 /* Calculate: result = (X*Y)>>Z */
80 #define MPC_MULTIPLY_EX(X,Y,Z) \
81 ({ \
82 MPC_SAMPLE_FORMAT t1; \
83 MPC_SAMPLE_FORMAT t2; \
84 asm volatile ( \
85 "mac.l %[x],%[y],%%acc0\n\t" /* multiply */ \
86 "mulu.l %[y],%[x] \n\t" /* get lower half, avoid emac stall */ \
87 "movclr.l %%acc0,%[t1] \n\t" /* get higher half */ \
88 "moveq.l #31,%[t2] \n\t" \
89 "sub.l %[sh],%[t2] \n\t" /* t2 = 31 - shift */ \
90 "ble.s 1f \n\t" \
91 "asl.l %[t2],%[t1] \n\t" /* hi <<= 31 - shift */ \
92 "lsr.l %[sh],%[x] \n\t" /* (unsigned)lo >>= shift */ \
93 "or.l %[x],%[t1] \n\t" /* combine result */ \
94 "bra.s 2f \n\t" \
95 "1: \n\t" \
96 "neg.l %[t2] \n\t" /* t2 = shift - 31 */ \
97 "asr.l %[t2],%[t1] \n\t" /* hi >>= t2 */ \
98 "2: \n" \
99 : [t1]"=&d"(t1), [t2]"=&d"(t2) \
100 : [x] "d"((X)), [y] "d"((Y)), [sh]"d"((Z))); \
101 t1; \
103 #elif defined(CPU_ARM)
104 /* Calculate: result = (X*Y)>>14 */
105 #define MPC_MULTIPLY(X,Y) \
106 ({ \
107 MPC_SAMPLE_FORMAT lo; \
108 MPC_SAMPLE_FORMAT hi; \
109 asm volatile ( \
110 "smull %[lo], %[hi], %[x], %[y] \n\t" /* multiply */ \
111 "mov %[lo], %[lo], lsr #14 \n\t" /* lo >>= 14 */ \
112 "orr %[lo], %[lo], %[hi], lsl #18" /* lo |= (hi << 18) */ \
113 : [lo]"=&r"(lo), [hi]"=&r"(hi) \
114 : [x]"r"(X), [y]"r"(Y)); \
115 lo; \
118 /* Calculate: result = (X*Y)>>Z */
119 #define MPC_MULTIPLY_EX(X,Y,Z) \
120 ({ \
121 MPC_SAMPLE_FORMAT lo; \
122 MPC_SAMPLE_FORMAT hi; \
123 asm volatile ( \
124 "smull %[lo], %[hi], %[x], %[y] \n\t" /* multiply */ \
125 "mov %[lo], %[lo], lsr %[shr] \n\t" /* lo >>= Z */ \
126 "orr %[lo], %[lo], %[hi], lsl %[shl]" /* lo |= (hi << (32-Z)) */ \
127 : [lo]"=&r"(lo), [hi]"=&r"(hi) \
128 : [x]"r"(X), [y]"r"(Y), [shr]"r"(Z), [shl]"r"(32-Z)); \
129 lo; \
131 #else /* libmusepack standard */
133 #define MPC_MULTIPLY_NOTRUNCATE(X,Y) \
134 (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> MPC_FIXED_POINT_FRACTPART)
136 #define MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z) \
137 (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> (Z))
139 #ifdef _DEBUG
140 static inline MPC_SAMPLE_FORMAT MPC_MULTIPLY(MPC_SAMPLE_FORMAT item1,MPC_SAMPLE_FORMAT item2)
142 MPC_SAMPLE_FORMAT_MULTIPLY temp = MPC_MULTIPLY_NOTRUNCATE(item1,item2);
143 assert(temp == (MPC_SAMPLE_FORMAT_MULTIPLY)(MPC_SAMPLE_FORMAT)temp);
144 return (MPC_SAMPLE_FORMAT)temp;
147 static inline MPC_SAMPLE_FORMAT MPC_MULTIPLY_EX(MPC_SAMPLE_FORMAT item1,MPC_SAMPLE_FORMAT item2,unsigned shift)
149 MPC_SAMPLE_FORMAT_MULTIPLY temp = MPC_MULTIPLY_EX_NOTRUNCATE(item1,item2,shift);
150 assert(temp == (MPC_SAMPLE_FORMAT_MULTIPLY)(MPC_SAMPLE_FORMAT)temp);
151 return (MPC_SAMPLE_FORMAT)temp;
153 #else
154 #define MPC_MULTIPLY(X,Y) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_NOTRUNCATE(X,Y))
155 #define MPC_MULTIPLY_EX(X,Y,Z) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z))
156 #endif
158 #endif
160 #ifdef MPC_HAVE_MULHIGH
161 #define MPC_MULTIPLY_FRACT(X,Y) _MulHigh(X,Y)
162 #else
163 #if defined(CPU_COLDFIRE)
164 /* loses one bit of accuracy. The rest of the macros won't be as easy as this... */
165 #define MPC_MULTIPLY_FRACT(X,Y) \
166 ({ \
167 MPC_SAMPLE_FORMAT t; \
168 asm volatile ( \
169 "mac.l %[A], %[B], %%acc0\n\t" \
170 "movclr.l %%acc0, %[t] \n\t" \
171 "asr.l #1, %[t] \n\t" \
172 : [t] "=d" (t) \
173 : [A] "r" ((X)), [B] "r" ((Y))); \
174 t; \
176 #elif defined(CPU_ARM)
177 /* Calculate: result = (X*Y)>>32, without need for >>32 */
178 #define MPC_MULTIPLY_FRACT(X,Y) \
179 ({ \
180 MPC_SAMPLE_FORMAT lo; \
181 MPC_SAMPLE_FORMAT hi; \
182 asm volatile ( \
183 "smull %[lo], %[hi], %[x], %[y]" /* hi = result */ \
184 : [lo]"=&r"(lo), [hi]"=&r"(hi) \
185 : [x]"r"(X), [y]"r"(Y)); \
186 hi; \
188 #else
189 #define MPC_MULTIPLY_FRACT(X,Y) MPC_MULTIPLY_EX(X,Y,32)
190 #endif
191 #endif
193 #define MPC_MAKE_FRACT_CONST(X) (MPC_SAMPLE_FORMAT)((X) * (double)(((mpc_int64_t)1)<<32) )
195 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
197 #else
198 //in floating-point mode, decoded samples are in -1...1 range
200 typedef float MPC_SAMPLE_FORMAT;
202 #define MAKE_MPC_SAMPLE(X) ((MPC_SAMPLE_FORMAT)(X))
203 #define MAKE_MPC_SAMPLE_EX(X,Y) ((MPC_SAMPLE_FORMAT)(X))
205 #define MPC_MULTIPLY_FRACT(X,Y) ((X)*(Y))
206 #define MPC_MAKE_FRACT_CONST(X) (X)
208 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
209 #define MPC_MULTIPLY(X,Y) ((X)*(Y))
210 #define MPC_MULTIPLY_EX(X,Y,Z) ((X)*(Y))
212 #define MPC_SHR_RND(X, Y) (X)
214 #endif
216 #endif // _mpcdec_math_h_