libmpcdec: include/mpcdec/math.h Source File

Main Page | Class List | Directories | File List | Class Members | File Members | Related Pages

# math.h

Go to the documentation of this file.

    00001 /*
    00002   Copyright (c) 2005, The Musepack Development Team
    00003   All rights reserved.
    00004
    00005   Redistribution and use in source and binary forms, with or without
    00006   modification, are permitted provided that the following conditions are
    00007   met:
    00008
    00009   * Redistributions of source code must retain the above copyright
    00010   notice, this list of conditions and the following disclaimer.
    00011
    00012   * Redistributions in binary form must reproduce the above
    00013   copyright notice, this list of conditions and the following
    00014   disclaimer in the documentation and/or other materials provided
    00015   with the distribution.
    00016
    00017   * Neither the name of the The Musepack Development Team nor the
    00018   names of its contributors may be used to endorse or promote
    00019   products derived from this software without specific prior
    00020   written permission.
    00021
    00022   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00023   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00024   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00025   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    00026   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00027   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00028   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    00029   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    00030   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00031   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00032   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00033 */
    00034
    00037
    00038 #ifndef _mpcdec_math_h_
    00039 #define _mpcdec_math_h_
    00040
    00041 //#define MPC_FIXED_POINT
    00042
    00043 #define MPC_FIXED_POINT_SHIFT 16
    00044
    00045 #ifdef MPC_FIXED_POINT
    00046
    00047
    00048 #ifdef _WIN32_WCE
    00049
    00050 #include <cmnintrin.h>
    00051
    00052 #define MPC_HAVE_MULHIGH
    00053
    00054 #endif
    00055
    00056
    00057 #define MPC_FIXED_POINT_SCALE_SHIFT (MPC_FIXED_POINT_SHIFT + MPC_FIXED_POINT_FRACTPART)
    00058 #define MPC_FIXED_POINT_SCALE (1 << (MPC_FIXED_POINT_SCALE_SHIFT - 1))
    00059
    00060
    00061 //in fixedpoint mode, results in decode output buffer are in -MPC_FIXED_POINT_SCALE ... MPC_FIXED_POINT_SCALE range
    00062
    00063 #define MPC_FIXED_POINT_FRACTPART 14
    00064 typedef mpc_int32_t MPC_SAMPLE_FORMAT;
    00065
    00066 typedef mpc_int64_t MPC_SAMPLE_FORMAT_MULTIPLY;
    00067
    00068 #define MAKE_MPC_SAMPLE(X) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<MPC_FIXED_POINT_FRACTPART))
    00069 #define MAKE_MPC_SAMPLE_EX(X,Y) (MPC_SAMPLE_FORMAT)((double)(X) * (double)(((mpc_int64_t)1)<<(Y)))
    00070
    00071 #define MPC_MULTIPLY_NOTRUNCATE(X,Y) \
    00072         (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> MPC_FIXED_POINT_FRACTPART)
    00073
    00074 #define MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z) \
    00075         (((MPC_SAMPLE_FORMAT_MULTIPLY)(X) * (MPC_SAMPLE_FORMAT_MULTIPLY)(Y)) >> (Z))
    00076
    00077 #ifdef _DEBUG
    00078 static inline MPC_SAMPLE_FORMAT MPC_MULTIPLY(MPC_SAMPLE_FORMAT item1,MPC_SAMPLE_FORMAT item2)
    00079 {
    00080         MPC_SAMPLE_FORMAT_MULTIPLY temp = MPC_MULTIPLY_NOTRUNCATE(item1,item2);
    00081         assert(temp == (MPC_SAMPLE_FORMAT_MULTIPLY)(MPC_SAMPLE_FORMAT)temp);
    00082         return (MPC_SAMPLE_FORMAT)temp;
    00083 }
    00084
    00085 static inline MPC_SAMPLE_FORMAT MPC_MULTIPLY_EX(MPC_SAMPLE_FORMAT item1,MPC_SAMPLE_FORMAT item2,unsigned shift)
    00086 {
    00087         MPC_SAMPLE_FORMAT_MULTIPLY temp = MPC_MULTIPLY_EX_NOTRUNCATE(item1,item2,shift);
    00088         assert(temp == (MPC_SAMPLE_FORMAT_MULTIPLY)(MPC_SAMPLE_FORMAT)temp);
    00089         return (MPC_SAMPLE_FORMAT)temp;
    00090 }
    00091
    00092 #else
    00093
    00094 #define MPC_MULTIPLY(X,Y) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_NOTRUNCATE(X,Y))
    00095 #define MPC_MULTIPLY_EX(X,Y,Z) ((MPC_SAMPLE_FORMAT)MPC_MULTIPLY_EX_NOTRUNCATE(X,Y,Z))
    00096
    00097 #endif
    00098
    00099 #ifdef MPC_HAVE_MULHIGH
    00100 #define MPC_MULTIPLY_FRACT(X,Y) _MulHigh(X,Y)
    00101 #else
    00102 #define MPC_MULTIPLY_FRACT(X,Y) MPC_MULTIPLY_EX(X,Y,32)
    00103 #endif
    00104
    00105 #define MPC_MAKE_FRACT_CONST(X) (MPC_SAMPLE_FORMAT)((X) * (double)(((mpc_int64_t)1)<<32) )
    00106 #define MPC_MULTIPLY_FRACT_CONST(X,Y) MPC_MULTIPLY_FRACT(X,MPC_MAKE_FRACT_CONST(Y))
    00107 #define MPC_MULTIPLY_FRACT_CONST_FIX(X,Y,Z) ( MPC_MULTIPLY_FRACT(X,MPC_MAKE_FRACT_CONST( Y / (1<<(Z)) )) << (Z) )
    00108 #define MPC_MULTIPLY_FRACT_CONST_SHR(X,Y,Z) MPC_MULTIPLY_FRACT(X,MPC_MAKE_FRACT_CONST( Y / (1<<(Z)) ))
    00109
    00110 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
    00111 #define MPC_SCALE_CONST(X,Y,Z) MPC_MULTIPLY_EX(X,MAKE_MPC_SAMPLE_EX(Y,Z),(Z))
    00112 #define MPC_SCALE_CONST_SHL(X,Y,Z,S) MPC_MULTIPLY_EX(X,MAKE_MPC_SAMPLE_EX(Y,Z),(Z)-(S))
    00113 #define MPC_SCALE_CONST_SHR(X,Y,Z,S) MPC_MULTIPLY_EX(X,MAKE_MPC_SAMPLE_EX(Y,Z),(Z)+(S))
    00114 #define MPC_SHR(X,Y) ((X)>>(Y))
    00115 #define MPC_SHL(X,Y) ((X)<<(Y))
    00116
    00117 #else
    00118
    00119 //in floating-point mode, decoded samples are in -1...1 range
    00120
    00121 typedef float MPC_SAMPLE_FORMAT;
    00122
    00123 #define MAKE_MPC_SAMPLE(X) ((MPC_SAMPLE_FORMAT)(X))
    00124 #define MAKE_MPC_SAMPLE_EX(X,Y) ((MPC_SAMPLE_FORMAT)(X))
    00125
    00126 #define MPC_MULTIPLY_FRACT(X,Y) ((X)*(Y))
    00127 #define MPC_MAKE_FRACT_CONST(X) (X)
    00128 #define MPC_MULTIPLY_FRACT_CONST(X,Y) MPC_MULTPLY_FRACT(X,MPC_MAKE_FRACT_CONST(Y))
    00129 #define MPC_MULTIPLY_FRACT_CONST_SHR(X,Y,Z) MPC_MULTIPLY_FRACT(X,MPC_MAKE_FRACT_CONST( Y ))
    00130 #define MPC_MULTIPLY_FRACT_CONST_FIX(X,Y,Z) MPC_MULTIPLY_FRACT(X,MPC_MAKE_FRACT_CONST( Y ))
    00131
    00132 #define MPC_MULTIPLY_FLOAT_INT(X,Y) ((X)*(Y))
    00133 #define MPC_MULTIPLY(X,Y) ((X)*(Y))
    00134 #define MPC_MULTIPLY_EX(X,Y,Z) ((X)*(Y))
    00135 #define MPC_SCALE_CONST(X,Y,Z) ((X)*(Y))
    00136 #define MPC_SCALE_CONST_SHL(X,Y,Z,S) ((X)*(Y))
    00137 #define MPC_SCALE_CONST_SHR(X,Y,Z,S) ((X)*(Y))
    00138 #define MPC_SHR(X,Y) (X)
    00139 #define MPC_SHL(X,Y) (X)
    00140
    00141 #endif
    00142
    00143 #endif // _mpcdec_math_h_
    00144