2 Copyright (c) 2005, The Musepack Development Team
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
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
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.
36 /// Libmpcdec internal math routines.
38 #ifndef _mpcdec_math_h_
39 #define _mpcdec_math_h_
41 #include "mpc_config.h"
43 #define MPC_FIXED_POINT_SHIFT 16
45 #ifdef MPC_FIXED_POINT
48 #include <cmnintrin.h>
49 #define MPC_HAVE_MULHIGH
52 #define MPC_FIXED_POINT_SCALE_SHIFT (MPC_FIXED_POINT_SHIFT + MPC_FIXED_POINT_FRACTPART)
53 #define MPC_FIXED_POINT_SCALE (1 << (MPC_FIXED_POINT_SCALE_SHIFT - 1))
54 //in fixedpoint mode, results in decode output buffer are in -MPC_FIXED_POINT_SCALE ... MPC_FIXED_POINT_SCALE range
56 #define MPC_FIXED_POINT_FRACTPART 14
57 typedef mpc_int32_t MPC_SAMPLE_FORMAT
;
58 typedef mpc_int64_t MPC_SAMPLE_FORMAT_MULTIPLY
;
60 #define MAKE_MPC_SAMPLE(X) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<MPC_FIXED_POINT_FRACTPART))
61 #define MAKE_MPC_SAMPLE_EX(X,Y) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<(Y)))
63 #define MPC_SHR_RND(X, Y) ((X+(1<<(Y-1)))>>Y)
65 #if defined(CPU_COLDFIRE)
67 #define MPC_MULTIPLY(X,Y) mpc_multiply((X), (Y))
68 #define MPC_MULTIPLY_EX(X,Y,Z) mpc_multiply_ex((X), (Y), (Z))
70 static inline MPC_SAMPLE_FORMAT
mpc_multiply(MPC_SAMPLE_FORMAT x
,
73 MPC_SAMPLE_FORMAT t1
, t2
;
75 "mac.l %[x],%[y],%%acc0\n" /* multiply */
76 "mulu.l %[y],%[x] \n" /* get lower half, avoid emac stall */
77 "movclr.l %%acc0,%[t1] \n" /* get higher half */
78 "moveq.l #17,%[t2] \n"
79 "asl.l %[t2],%[t1] \n" /* hi <<= 17, plus one free */
80 "moveq.l #14,%[t2] \n"
81 "lsr.l %[t2],%[x] \n" /* (unsigned)lo >>= 14 */
82 "or.l %[x],%[t1] \n" /* combine result */
93 static inline MPC_SAMPLE_FORMAT
mpc_multiply_ex(MPC_SAMPLE_FORMAT x
,
97 MPC_SAMPLE_FORMAT t1
, t2
;
99 "mac.l %[x],%[y],%%acc0\n" /* multiply */
100 "mulu.l %[y],%[x] \n" /* get lower half, avoid emac stall */
101 "movclr.l %%acc0,%[t1] \n" /* get higher half */
102 "moveq.l #31,%[t2] \n"
103 "sub.l %[sh],%[t2] \n" /* t2 = 31 - shift */
105 "asl.l %[t2],%[t1] \n" /* hi <<= 31 - shift */
106 "lsr.l %[sh],%[x] \n" /* (unsigned)lo >>= shift */
107 "or.l %[x],%[t1] \n" /* combine result */
110 "neg.l %[t2] \n" /* t2 = shift - 31 */
111 "asr.l %[t2],%[t1] \n" /* hi >>= t2 */
123 #elif defined(CPU_ARM)
124 // borrowed and adapted from libMAD
125 #define MPC_MULTIPLY(X,Y) \
127 MPC_SAMPLE_FORMAT low; \
128 MPC_SAMPLE_FORMAT high; \
129 asm volatile ( /* will calculate: result = (X*Y)>>14 */ \
130 "smull %0,%1,%2,%3 \n\t" /* multiply with result %0 [0..31], %1 [32..63] */ \
131 "mov %0, %0, lsr #14 \n\t" /* %0 = %0 >> 14 */ \
132 "orr %0, %0, %1, lsl #18 \n\t"/* result = %0 OR (%1 << 18) */ \
133 : "=&r"(low), "=&r" (high) \
138 // borrowed and adapted from libMAD
139 #define MPC_MULTIPLY_EX(X,Y,Z) \
141 MPC_SAMPLE_FORMAT low; \
142 MPC_SAMPLE_FORMAT high; \
143 asm volatile ( /* will calculate: result = (X*Y)>>Z */ \
144 "smull %0,%1,%2,%3 \n\t" /* multiply with result %0 [0..31], %1 [32..63] */ \
145 "mov %0, %0, lsr %4 \n\t" /* %0 = %0 >> Z */ \
146 "orr %0, %0, %1, lsl %5 \n\t" /* result = %0 OR (%1 << (32-Z)) */ \
147 : "=&r"(low), "=&r" (high) \
148 : "r"(X),"r"(Y),"r"(Z),"r"(32-Z)); \
151 #else /* libmusepack standard */
153 #define MPC_MULTIPLY_NOTRUNCATE(X,Y) \
154 (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> MPC_FIXED_POINT_FRACTPART)
156 #define MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z) \
157 (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> (Z))
160 static inline MPC_SAMPLE_FORMAT
MPC_MULTIPLY(MPC_SAMPLE_FORMAT item1
,MPC_SAMPLE_FORMAT item2
)
162 MPC_SAMPLE_FORMAT_MULTIPLY temp
= MPC_MULTIPLY_NOTRUNCATE(item1
,item2
);
163 assert(temp
== (MPC_SAMPLE_FORMAT_MULTIPLY
)(MPC_SAMPLE_FORMAT
)temp
);
164 return (MPC_SAMPLE_FORMAT
)temp
;
167 static inline MPC_SAMPLE_FORMAT
MPC_MULTIPLY_EX(MPC_SAMPLE_FORMAT item1
,MPC_SAMPLE_FORMAT item2
,unsigned shift
)
169 MPC_SAMPLE_FORMAT_MULTIPLY temp
= MPC_MULTIPLY_EX_NOTRUNCATE(item1
,item2
,shift
);
170 assert(temp
== (MPC_SAMPLE_FORMAT_MULTIPLY
)(MPC_SAMPLE_FORMAT
)temp
);
171 return (MPC_SAMPLE_FORMAT
)temp
;
174 #define MPC_MULTIPLY(X,Y) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_NOTRUNCATE(X,Y))
175 #define MPC_MULTIPLY_EX(X,Y,Z) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z))
180 #ifdef MPC_HAVE_MULHIGH
181 #define MPC_MULTIPLY_FRACT(X,Y) _MulHigh(X,Y)
183 #if defined(CPU_COLDFIRE)
184 /* loses one bit of accuracy. The rest of the macros won't be as easy as this... */
185 #define MPC_MULTIPLY_FRACT(X,Y) \
187 MPC_SAMPLE_FORMAT t; \
189 "mac.l %[A], %[B], %%acc0\n\t" \
190 "movclr.l %%acc0, %[t]\n\t" \
191 "asr.l #1, %[t]\n\t" \
193 : [A] "r" ((X)), [B] "r" ((Y))); \
196 #elif defined(CPU_ARM)
197 // borrowed and adapted from libMAD
198 #define MPC_MULTIPLY_FRACT(X,Y) \
200 MPC_SAMPLE_FORMAT low; \
201 MPC_SAMPLE_FORMAT high; \
202 asm volatile ( /* will calculate: result = (X*Y)>>32 */ \
203 "smull %0,%1,%2,%3 \n\t" /* multiply with result %0 [0..31], %1 [32..63] */ \
204 : "=&r"(low), "=&r" (high) /* result = %1 [32..63], saves the >>32 */ \
209 #define MPC_MULTIPLY_FRACT(X,Y) MPC_MULTIPLY_EX(X,Y,32)
213 #define MPC_MAKE_FRACT_CONST(X) (MPC_SAMPLE_FORMAT)((X) * (double)(((mpc_int64_t)1)<<32) )
215 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
218 //in floating-point mode, decoded samples are in -1...1 range
220 typedef float MPC_SAMPLE_FORMAT
;
222 #define MAKE_MPC_SAMPLE(X) ((MPC_SAMPLE_FORMAT)(X))
223 #define MAKE_MPC_SAMPLE_EX(X,Y) ((MPC_SAMPLE_FORMAT)(X))
225 #define MPC_MULTIPLY_FRACT(X,Y) ((X)*(Y))
226 #define MPC_MAKE_FRACT_CONST(X) (X)
228 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
229 #define MPC_MULTIPLY(X,Y) ((X)*(Y))
230 #define MPC_MULTIPLY_EX(X,Y,Z) ((X)*(Y))
232 #define MPC_SHR_RND(X, Y) (X)
236 #endif // _mpcdec_math_h_