pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_InlineBinaryOps.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_INLINEBINARYOPS_H
00040 #define PLONK_INLINEBINARYOPS_H
00041 
00042 #include "plonk_Constants.h"
00043 #include "plonk_InlineCommonOps.h"
00044 #include "plonk_InlineUnaryOps.h"
00045 #include "plonk_InlineMiscOps.h"
00046 
00047 //------------------------------------------------------------------------------
00048 
00049 
00050 #define PLONK_BINARYOP(CLASSNAME, OP) \
00051 \
00052         inline CLASSNAME OP (CLASSNAME const& right) const throw() { return binary< BinaryOpFunctionsType::OP > (right); } 
00053 
00054 
00055 
00056 #define PLONK_BINARYOPS(CLASSNAME) \
00057         PLONK_BINARYOP(CLASSNAME, addop)\
00058         PLONK_BINARYOP(CLASSNAME, subop)\
00059         PLONK_BINARYOP(CLASSNAME, mulop)\
00060         PLONK_BINARYOP(CLASSNAME, divop)\
00061         PLONK_BINARYOP(CLASSNAME, modop)\
00062         PLONK_BINARYOP(CLASSNAME, isEqualTo)\
00063         PLONK_BINARYOP(CLASSNAME, isNotEqualTo)\
00064         PLONK_BINARYOP(CLASSNAME, isGreaterThan)\
00065         PLONK_BINARYOP(CLASSNAME, isGreaterThanOrEqualTo)\
00066         PLONK_BINARYOP(CLASSNAME, isLessThan)\
00067         PLONK_BINARYOP(CLASSNAME, isLessThanOrEqualTo)\
00068         PLONK_BINARYOP(CLASSNAME, hypot)\
00069         PLONK_BINARYOP(CLASSNAME, pow)\
00070         PLONK_BINARYOP(CLASSNAME, atan2)\
00071         PLONK_BINARYOP(CLASSNAME, min)\
00072         PLONK_BINARYOP(CLASSNAME, max)\
00073         PLONK_BINARYOP(CLASSNAME, sumsqr)\
00074         PLONK_BINARYOP(CLASSNAME, difsqr)\
00075         PLONK_BINARYOP(CLASSNAME, sqrsum)\
00076         PLONK_BINARYOP(CLASSNAME, sqrdif)\
00077         PLONK_BINARYOP(CLASSNAME, absdif)\
00078         PLONK_BINARYOP(CLASSNAME, thresh)\
00079         PLONK_BINARYOP(CLASSNAME, round)\
00080         PLONK_BINARYOP(CLASSNAME, trunc)\
00081         PLONK_BINARYOP(CLASSNAME, clip2)\
00082         PLONK_BINARYOP(CLASSNAME, decayFeedback)\
00083 \
00084         inline CLASSNAME operator+   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::addop> (right); }\
00085 \
00086         inline CLASSNAME operator-   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::subop> (right); }\
00087 \
00088         inline CLASSNAME operator*   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::mulop> (right); }\
00089 \
00090         inline CLASSNAME operator/   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::divop> (right); }\
00091 \
00092         inline CLASSNAME operator%   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::modop> (right); }\
00093 \
00094         inline CLASSNAME operator<   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::isLessThan> (right); }\
00095 \
00096         inline CLASSNAME operator<=  (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::isLessThanOrEqualTo> (right); }\
00097 \
00098         inline CLASSNAME operator>   (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::isGreaterThan> (right); }\
00099 \
00100         inline CLASSNAME operator>=  (CLASSNAME const& right) const throw() { return binary<BinaryOpFunctionsType::isGreaterThanOrEqualTo> (right); }\
00101 \
00102         template<class RightType> inline CLASSNAME operator+   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::addop> (CLASSNAME (right)); }\
00103 \
00104         template<class RightType> inline CLASSNAME operator-   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::subop> (CLASSNAME (right)); }\
00105 \
00106         template<class RightType> inline CLASSNAME operator*   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::mulop> (CLASSNAME (right)); }\
00107 \
00108         template<class RightType> inline CLASSNAME operator/   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::divop> (CLASSNAME (right)); }\
00109 \
00110         template<class RightType> inline CLASSNAME operator%   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::modop> (CLASSNAME (right)); }\
00111 \
00112         template<class RightType> inline CLASSNAME& operator+= (RightType const& right) throw() { return operator= (*this + right); }\
00113 \
00114         template<class RightType> inline CLASSNAME& operator-= (RightType const& right) throw() { return operator= (*this - right); }\
00115 \
00116         template<class RightType> inline CLASSNAME& operator*= (RightType const& right) throw() { return operator= (*this * right); }\
00117 \
00118         template<class RightType> inline CLASSNAME& operator/= (RightType const& right) throw() { return operator= (*this / right); }\
00119 \
00120         template<class RightType> inline CLASSNAME& operator%= (RightType const& right) throw() { return operator= (*this % right); }\
00121 \
00122         template<class RightType> inline CLASSNAME operator<   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::isLessThan> (CLASSNAME (right)); }\
00123 \
00124         template<class RightType> inline CLASSNAME operator<=  (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::isLessThanOrEqualTo> (CLASSNAME (right)); }\
00125 \
00126         template<class RightType> inline CLASSNAME operator>   (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::isGreaterThan> (CLASSNAME (right)); }\
00127 \
00128         template<class RightType> inline CLASSNAME operator>=  (RightType const& right) const throw() { return binary<BinaryOpFunctionsType::isGreaterThanOrEqualTo> (CLASSNAME (right)); }
00129 
00130 #define PLONK_BINARYOPGLOBAL(CLASSNAME,T,OP)\
00131 \
00132     inline CLASSNAME OP (CLASSNAME const& left, CLASSNAME const& right) throw() { return left.OP (right); }
00133 
00134 #define PLONK_BINARYOPGLOBAL_TEMPLATE(CLASSNAME,T,OP)\
00135     template<class T> PLONK_BINARYOPGLOBAL(CLASSNAME<T>,PLONK_EMPTYDEFINE,OP)
00136 
00137 #define PLONK_BINARYOPGLOBALS_DEFINE(BINARYOPGLOBAL,CLASSNAME,T) \
00138         BINARYOPGLOBAL(CLASSNAME, T, addop)\
00139         BINARYOPGLOBAL(CLASSNAME, T, subop)\
00140         BINARYOPGLOBAL(CLASSNAME, T, mulop)\
00141         BINARYOPGLOBAL(CLASSNAME, T, divop)\
00142         BINARYOPGLOBAL(CLASSNAME, T, modop)\
00143         BINARYOPGLOBAL(CLASSNAME, T, isEqualTo)\
00144         BINARYOPGLOBAL(CLASSNAME, T, isNotEqualTo)\
00145         BINARYOPGLOBAL(CLASSNAME, T, isGreaterThan)\
00146         BINARYOPGLOBAL(CLASSNAME, T, isGreaterThanOrEqualTo)\
00147         BINARYOPGLOBAL(CLASSNAME, T, isLessThan)\
00148         BINARYOPGLOBAL(CLASSNAME, T, isLessThanOrEqualTo)\
00149         BINARYOPGLOBAL(CLASSNAME, T, hypot)\
00150         BINARYOPGLOBAL(CLASSNAME, T, pow)\
00151         BINARYOPGLOBAL(CLASSNAME, T, atan2)\
00152         BINARYOPGLOBAL(CLASSNAME, T, min)\
00153         BINARYOPGLOBAL(CLASSNAME, T, max)\
00154         BINARYOPGLOBAL(CLASSNAME, T, sumsqr)\
00155         BINARYOPGLOBAL(CLASSNAME, T, difsqr)\
00156         BINARYOPGLOBAL(CLASSNAME, T, sqrsum)\
00157         BINARYOPGLOBAL(CLASSNAME, T, sqrdif)\
00158         BINARYOPGLOBAL(CLASSNAME, T, absdif)\
00159         BINARYOPGLOBAL(CLASSNAME, T, thresh)\
00160         BINARYOPGLOBAL(CLASSNAME, T, round)\
00161         BINARYOPGLOBAL(CLASSNAME, T, trunc)\
00162         BINARYOPGLOBAL(CLASSNAME, T, clip2)\
00163         BINARYOPGLOBAL(CLASSNAME, T, decayFeedback)
00164 
00165 
00166 #define PLONK_BINARYOPGLOBALS(CLASSNAME)            PLONK_BINARYOPGLOBALS_DEFINE(PLONK_BINARYOPGLOBAL,CLASSNAME,PLONK_EMPTYDEFINE)
00167 #define PLONK_BINARYOPGLOBALS_TEMPLATE(CLASSNAME,T) PLONK_BINARYOPGLOBALS_DEFINE(PLONK_BINARYOPGLOBAL_TEMPLATE,CLASSNAME,T)
00168 
00169 
00170 //------------------------------------------------------------------------------
00171 
00180 template<class Type> inline Type addop (Type const& a, Type const& b) throw() { return a + b; }
00181 
00183 template<class Type> inline Type subop (Type const& a, Type const& b) throw() { return a - b; }
00184 
00186 template<class Type> inline Type mulop (Type const& a, Type const& b) throw() { return a * b; }
00187 
00189 template<class Type> inline Type divop (Type const& a, Type const& b) throw() { return a / b; }
00190 
00192 template<class Type> inline Type modop (Type const& a, Type const& b) throw() { return (Type)((LongLong)a % (LongLong)b); }
00193 
00195 inline float modop (float const& a, float const& b) throw() { return pl_ModF (a, b); }
00196 
00198 inline double modop (double const& a, double const& b) throw() { return pl_ModD (a, b); }
00199 
00201 template<class Type> inline Type isEqualTo              (Type const& a, Type const& b) throw()  { return (a == b) ? Math<Type>::get1() : Math<Type>::get0(); }
00202 
00204 template<class Type> inline Type isNotEqualTo           (Type const& a, Type const& b) throw()  { return (a != b) ? Math<Type>::get1() : Math<Type>::get0(); }
00205 
00207 template<class Type> inline Type isGreaterThan          (Type const& a, Type const& b) throw()  { return (a > b)  ? Math<Type>::get1() : Math<Type>::get0(); }
00208 
00210 template<class Type> inline Type isGreaterThanOrEqualTo (Type const& a, Type const& b) throw()  { return (a >= b) ? Math<Type>::get1() : Math<Type>::get0(); }
00211 
00213 template<class Type> inline Type isLessThan             (Type const& a, Type const& b) throw()  { return (a < b)  ? Math<Type>::get1() : Math<Type>::get0(); }
00214 
00216 template<class Type> inline Type isLessThanOrEqualTo    (Type const& a, Type const& b) throw()  { return (a <= b) ? Math<Type>::get1() : Math<Type>::get0(); }
00217 
00219 template<class Type> inline Type hypot  (Type const& a, Type const& b) throw() { return static_cast<Type> (::hypot (double (a), double (b))); }
00220 
00221 inline float hypot  (float const& a, float const& b) throw() { return pl_HypotF (a, b); }
00222 inline double hypot  (double const& a, double const& b) throw() { return pl_HypotD (a, b); }
00223 
00225 template<class Type> inline Type atan2  (Type const& a, Type const& b) throw() { return static_cast<Type> (::atan2 (double (a), double (b))); }
00226 
00227 inline float atan2  (float const& a, float const& b) throw() { return pl_Atan2F (a, b); }
00228 inline double atan2  (double const& a, double const& b) throw() { return pl_Atan2D (a, b); }
00229 
00231 template<class Type> inline Type sumsqr (Type const& a, Type const& b) throw() { return squared (a) + squared (b); }
00232 
00234 template<class Type> inline Type difsqr (Type const& a, Type const& b) throw() { return squared (a) - squared (b);  }
00235 
00237 template<class Type> inline Type sqrsum (Type const& a, Type const& b) throw() { const Type c (a + b); return squared (c); }
00238 
00240 template<class Type> inline Type sqrdif (Type const& a, Type const& b) throw() { const Type c (a - b); return squared (c); }
00241 
00243 template<class Type> inline Type absdif (Type const& a, Type const& b) throw() { return abs (a - b); }
00244 
00246 template<class Type> inline Type thresh (Type const& a, Type const& b) throw() { return (a < b) ? Math<Type>::get0() : a; }
00247 
00248 template<class Type>
00249 inline Type round (Type const& a, Type const& b) throw()
00250 {
00251         const Type offset = a < Math<Type>::get0() ? Math<Type>::get_0_5() : Math<Type>::get0_5();
00252         const int n = int (a / b + offset);
00253         return b * Type (n);
00254 }
00255 
00256 template<class Type>
00257 inline Type trunc (Type const& a, Type const& b) throw()
00258 {
00259         const int n = int (a / b);
00260         return b * Type (n);
00261 }
00262 
00263 template<class Type> inline Type clip2 (Type const& value, Type const& range) throw()     { return clip<Type> (value, -range, range); }
00264 template<class Type> inline Type wrap (Type const& value, Type const& upper) throw()      { return wrap (value, Math<Type>::get0(), upper); }
00265 
00266 template<class Type> inline Type decayFeedback (Type const& delayTime, Type const& decayTime) throw()
00267 {
00268     const Type zero (Math<Type>::get0());
00269     const Type log001 (Math<Type>::getLog0_001());
00270         
00271     if (delayTime > zero)       return Type (exp (log001 * delayTime / decayTime));
00272     else if (delayTime < zero)  return -Type (exp (log001 * delayTime / -decayTime));
00273     else                        return zero;
00274 
00275 }
00276 
00278 
00279 #define PLONK_BINARYOPFUNCTION_DEFINE(OP)\
00280     static inline OperandType OP (OperandType const& a, OperandType const& b) throw() { return plonk::OP (a, b); }
00281 
00282 template<class OperandType>
00283 class BinaryOpFunctions
00284 {
00285 public:    
00286     PLONK_BINARYOPFUNCTION_DEFINE(addop)
00287     PLONK_BINARYOPFUNCTION_DEFINE(subop)
00288     PLONK_BINARYOPFUNCTION_DEFINE(mulop)
00289     PLONK_BINARYOPFUNCTION_DEFINE(divop)
00290     PLONK_BINARYOPFUNCTION_DEFINE(modop)
00291     PLONK_BINARYOPFUNCTION_DEFINE(isEqualTo)
00292     PLONK_BINARYOPFUNCTION_DEFINE(isNotEqualTo)
00293     PLONK_BINARYOPFUNCTION_DEFINE(isGreaterThan)
00294     PLONK_BINARYOPFUNCTION_DEFINE(isGreaterThanOrEqualTo)
00295     PLONK_BINARYOPFUNCTION_DEFINE(isLessThan)
00296     PLONK_BINARYOPFUNCTION_DEFINE(isLessThanOrEqualTo)
00297     PLONK_BINARYOPFUNCTION_DEFINE(hypot)
00298     PLONK_BINARYOPFUNCTION_DEFINE(atan2)
00299     PLONK_BINARYOPFUNCTION_DEFINE(pow)
00300     PLONK_BINARYOPFUNCTION_DEFINE(min)
00301     PLONK_BINARYOPFUNCTION_DEFINE(max)
00302     PLONK_BINARYOPFUNCTION_DEFINE(sumsqr)
00303     PLONK_BINARYOPFUNCTION_DEFINE(difsqr)
00304     PLONK_BINARYOPFUNCTION_DEFINE(sqrsum)
00305     PLONK_BINARYOPFUNCTION_DEFINE(sqrdif)
00306     PLONK_BINARYOPFUNCTION_DEFINE(absdif)
00307     PLONK_BINARYOPFUNCTION_DEFINE(thresh)
00308     PLONK_BINARYOPFUNCTION_DEFINE(round)
00309     PLONK_BINARYOPFUNCTION_DEFINE(trunc)
00310     PLONK_BINARYOPFUNCTION_DEFINE(clip2)
00311     PLONK_BINARYOPFUNCTION_DEFINE(decayFeedback)
00312 };
00313 
00314 template<class OperandType>
00315 class BinaryOpFunctionsHelper
00316 {
00317 public:
00318     typedef BinaryOpFunctions<OperandType> BinaryOpFunctionsType;
00319 };
00320 
00321 
00322 #endif // PLONK_INLINEBINARYOPS_H
00323 
 All Classes Functions Typedefs Enumerations Enumerator Properties