pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_InlineUnaryOps.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_INLINEUNARYOPS_H
00040 #define PLONK_INLINEUNARYOPS_H
00041 
00042 #include "plonk_Constants.h"
00043 #include "plonk_InlineCommonOps.h"
00044 
00045 //------------------------------------------------------------------------------
00046 
00047 #define PLONK_UNARYOP(CLASSNAME, OP) \
00048 \
00049         inline CLASSNAME OP() const throw() { return unary<UnaryOpFunctionsType::OP>(); }
00050 
00051 #define PLONK_UNARYOPS(CLASSNAME) \
00052         PLONK_UNARYOP(CLASSNAME, move)\
00053         PLONK_UNARYOP(CLASSNAME, inc)\
00054         PLONK_UNARYOP(CLASSNAME, dec)\
00055         PLONK_UNARYOP(CLASSNAME, abs)\
00056         PLONK_UNARYOP(CLASSNAME, log2)\
00057         PLONK_UNARYOP(CLASSNAME, neg)\
00058         PLONK_UNARYOP(CLASSNAME, reciprocal)\
00059         PLONK_UNARYOP(CLASSNAME, sin)\
00060         PLONK_UNARYOP(CLASSNAME, cos)\
00061         PLONK_UNARYOP(CLASSNAME, tan)\
00062         PLONK_UNARYOP(CLASSNAME, asin)\
00063         PLONK_UNARYOP(CLASSNAME, acos)\
00064         PLONK_UNARYOP(CLASSNAME, atan)\
00065         PLONK_UNARYOP(CLASSNAME, sinh)\
00066         PLONK_UNARYOP(CLASSNAME, cosh)\
00067         PLONK_UNARYOP(CLASSNAME, tanh)\
00068         PLONK_UNARYOP(CLASSNAME, sqrt)\
00069         PLONK_UNARYOP(CLASSNAME, log)\
00070         PLONK_UNARYOP(CLASSNAME, log10)\
00071         PLONK_UNARYOP(CLASSNAME, exp)\
00072         PLONK_UNARYOP(CLASSNAME, squared)\
00073         PLONK_UNARYOP(CLASSNAME, cubed)\
00074         PLONK_UNARYOP(CLASSNAME, ceil)\
00075         PLONK_UNARYOP(CLASSNAME, floor)\
00076         PLONK_UNARYOP(CLASSNAME, frac)\
00077         PLONK_UNARYOP(CLASSNAME, sign)\
00078         PLONK_UNARYOP(CLASSNAME, m2f)\
00079         PLONK_UNARYOP(CLASSNAME, f2m)\
00080         PLONK_UNARYOP(CLASSNAME, a2dB)\
00081         PLONK_UNARYOP(CLASSNAME, dB2a)\
00082         PLONK_UNARYOP(CLASSNAME, d2r)\
00083         PLONK_UNARYOP(CLASSNAME, r2d)\
00084         PLONK_UNARYOP(CLASSNAME, distort)\
00085         PLONK_UNARYOP(CLASSNAME, zap)\
00086 \
00087         inline CLASSNAME operator-() const throw()  { return unary<UnaryOpFunctionsType::neg>(); }\
00088 \
00089         inline CLASSNAME operator++() const throw()  { return unary<UnaryOpFunctionsType::inc>(); }\
00090 \
00091         inline CLASSNAME operator++ (int) const throw()  { CLASSNAME temp = *this; operator= (unary<UnaryOpFunctionsType::inc>()); return temp; }
00092 
00093 
00094 #define PLONK_UNARYOPGLOBAL(CLASSNAME,T,OP)\
00095 \
00096     CLASSNAME OP (CLASSNAME const& operand) throw() { return operand.OP(); }
00097 
00098 #define PLONK_UNARYOPGLOBAL_TEMPLATE(CLASSNAME,T,OP)\
00099     template<class T> PLONK_UNARYOPGLOBAL(CLASSNAME<T>,PLONK_EMPTYDEFINE,OP)
00100 
00101 #define PLONK_UNARYOPGLOBALS_DEFINE(UNARYOPGLOBAL,CLASSNAME,T) \
00102         UNARYOPGLOBAL(CLASSNAME, T, move)\
00103         UNARYOPGLOBAL(CLASSNAME, T, inc)\
00104         UNARYOPGLOBAL(CLASSNAME, T, dec)\
00105         UNARYOPGLOBAL(CLASSNAME, T, abs)\
00106         UNARYOPGLOBAL(CLASSNAME, T, log2)\
00107         UNARYOPGLOBAL(CLASSNAME, T, neg)\
00108         UNARYOPGLOBAL(CLASSNAME, T, reciprocal)\
00109         UNARYOPGLOBAL(CLASSNAME, T, sin)\
00110         UNARYOPGLOBAL(CLASSNAME, T, cos)\
00111         UNARYOPGLOBAL(CLASSNAME, T, tan)\
00112         UNARYOPGLOBAL(CLASSNAME, T, asin)\
00113         UNARYOPGLOBAL(CLASSNAME, T, acos)\
00114         UNARYOPGLOBAL(CLASSNAME, T, atan)\
00115         UNARYOPGLOBAL(CLASSNAME, T, sinh)\
00116         UNARYOPGLOBAL(CLASSNAME, T, cosh)\
00117         UNARYOPGLOBAL(CLASSNAME, T, tanh)\
00118         UNARYOPGLOBAL(CLASSNAME, T, sqrt)\
00119         UNARYOPGLOBAL(CLASSNAME, T, log)\
00120         UNARYOPGLOBAL(CLASSNAME, T, log10)\
00121         UNARYOPGLOBAL(CLASSNAME, T, exp)\
00122         UNARYOPGLOBAL(CLASSNAME, T, squared)\
00123         UNARYOPGLOBAL(CLASSNAME, T, cubed)\
00124         UNARYOPGLOBAL(CLASSNAME, T, ceil)\
00125         UNARYOPGLOBAL(CLASSNAME, T, floor)\
00126         UNARYOPGLOBAL(CLASSNAME, T, frac)\
00127         UNARYOPGLOBAL(CLASSNAME, T, sign)\
00128         UNARYOPGLOBAL(CLASSNAME, T, m2f)\
00129         UNARYOPGLOBAL(CLASSNAME, T, f2m)\
00130         UNARYOPGLOBAL(CLASSNAME, T, a2dB)\
00131         UNARYOPGLOBAL(CLASSNAME, T, dB2a)\
00132         UNARYOPGLOBAL(CLASSNAME, T, d2r)\
00133         UNARYOPGLOBAL(CLASSNAME, T, r2d)\
00134         UNARYOPGLOBAL(CLASSNAME, T, distort)\
00135         UNARYOPGLOBAL(CLASSNAME, T, zap)
00136 
00137 #define PLONK_UNARYOPGLOBALS(CLASSNAME) PLONK_UNARYOPGLOBALS_DEFINE(PLONK_UNARYOPGLOBAL,CLASSNAME,PLONK_EMPTYDEFINE)
00138 #define PLONK_UNARYOPGLOBALS_TEMPLATE(CLASSNAME,T) PLONK_UNARYOPGLOBALS_DEFINE(PLONK_UNARYOPGLOBAL_TEMPLATE,CLASSNAME,T)
00139 
00140 //------------------------------------------------------------------------------
00141 
00150 template<class Type> inline Type move (Type const& a) throw()          { return a; }
00151 
00153 template<class Type> inline Type inc (Type const& a) throw()           { return a + Math<Type>::get1(); }
00154 
00156 template<class Type> inline Type dec (Type const& a) throw()           { return a - Math<Type>::get1(); }
00157 
00159 template<class Type> inline Type log2 (Type const& a) throw()          { return static_cast<Type> (pl_Log2D (double (a)));}
00160 
00161 inline float log2 (float const& a) throw()                             { return pl_Log2F (a); }
00162 inline double log2 (double const& a) throw()                           { return pl_Log2D (a); }
00163 
00165 template<class Type> inline Type neg (Type const& a) throw()           { return -a; }
00166 
00168 template<class Type> inline Type reciprocal (Type const& a) throw()    { return Math<Type>::get1() / a; }
00169 
00171 template<class Type> inline Type sin (Type const& a) throw()           { return static_cast<Type> (::sin (double (a))); }
00172 
00173 inline float sin (float const& a) throw()                              { return pl_SinF (a); }
00174 inline double sin (double const& a) throw()                            { return pl_SinD (a); }
00175 
00177 template<class Type> inline Type cos (Type const& a) throw()           { return static_cast<Type> (::cos (double (a))); }
00178 
00179 inline float cos (float const& a) throw()                              { return pl_CosF (a); }
00180 inline double cos (double const& a) throw()                            { return pl_CosD (a); }
00181 
00183 template<class Type> inline Type tan (Type const& a) throw()           { return static_cast<Type> (::tan (double (a))); }
00184 
00185 inline float tan (float const& a) throw()                              { return pl_TanF (a); }
00186 inline double tan (double const& a) throw()                            { return pl_TanD (a); }
00187 
00189 template<class Type> inline Type asin (Type const& a) throw()          { return static_cast<Type> (::asin (double (a))); }
00190 
00191 inline float asin (float const& a) throw()                              { return pl_AsinF (a); }
00192 inline double asin (double const& a) throw()                            { return pl_AsinD (a); }
00193 
00195 template<class Type> inline Type acos (Type const& a) throw()          { return static_cast<Type> (::acos (double (a))); }
00196 
00197 inline float acos (float const& a) throw()                              { return pl_AcosF (a); }
00198 inline double acos (double const& a) throw()                            { return pl_AcosD (a); }
00199 
00201 template<class Type> inline Type atan (Type const& a) throw()          { return static_cast<Type> (::atan (double (a))); }
00202 
00203 inline float atan (float const& a) throw()                              { return pl_AtanF (a); }
00204 inline double atan (double const& a) throw()                            { return pl_AtanD (a); }
00205 
00207 template<class Type> inline Type sinh (Type const& a) throw()          { return static_cast<Type> (::sinh (double (a))); }
00208 
00209 inline float sinh (float const& a) throw()                              { return pl_SinhF (a); }
00210 inline double sinh (double const& a) throw()                            { return pl_SinhD (a); }
00211 
00213 template<class Type> inline Type cosh (Type const& a) throw()          { return static_cast<Type> (::cosh (double (a))); }
00214 
00215 inline float cosh (float const& a) throw()                              { return pl_CoshF (a); }
00216 inline double cosh (double const& a) throw()                            { return pl_CoshD (a); }
00217 
00219 template<class Type> inline Type tanh (Type const& a) throw()          { return static_cast<Type> (::tanh (double (a))); }
00220 
00221 inline float tanh (float const& a) throw()                              { return pl_TanhF (a); }
00222 inline double tanh (double const& a) throw()                            { return pl_TanhD (a); }
00223 
00225 template<class Type> inline Type sqrt (Type const& a) throw()          { return static_cast<Type> (::sqrt (double (a))); }
00226 
00227 inline float sqrt (float const& a) throw()                              { return pl_SqrtF (a); }
00228 inline double sqrt (double const& a) throw()                            { return pl_SqrtD (a); }
00229 
00231 template<class Type> inline Type log (Type const& a) throw()           { return static_cast<Type> (::log (double (a))); }
00232 
00233 inline float log (float const& a) throw()                              { return pl_LogF (a); }
00234 inline double log (double const& a) throw()                            { return pl_LogD (a); }
00235 
00237 template<class Type> inline Type log10 (Type const& a) throw()         { return static_cast<Type> (::log10 (double (a))); }
00238 
00239 inline float log10 (float const& a) throw()                              { return pl_Log10F (a); }
00240 inline double log10 (double const& a) throw()                            { return pl_Log10D (a); }
00241 
00243 template<class Type> inline Type exp (Type const& a) throw()           { return static_cast<Type> (::exp (double (a))); }
00244 
00245 inline float exp (float const& a) throw()                              { return pl_ExpF (a); }
00246 inline double exp (double const& a) throw()                            { return pl_ExpD (a); }
00247 
00249 template<class Type> inline Type ceil (Type const& a) throw()          { const long n = long (a); return Type (n + 1); }
00250 
00252 template<class Type> inline Type floor (Type const& a) throw()         { const long n = long (a); return Type (n); }
00253 
00255 template<class Type> inline Type frac (Type const& a) throw()          { const long n = long (a); return a - Type (n); }
00256 
00258 template<class Type> inline Type sign (Type const& a) throw()          { const long n = (a == Math<Type>::get0()) ? 0 : (a < Math<Type>::get0()) ? -1 : 1; return Type (n); }
00259 
00261 template<class Type> inline Type m2f (Type const& a) throw()           { return Math<Type>::get440() * pow(Math<Type>::get2(), (a - Math<Type>::get69()) * Math<Type>::get1_12()); }
00262 
00264 template<class Type> inline Type f2m (Type const& a) throw()           { return log2 (a * Math<Type>::get1_440()) * Math<Type>::get12() + Math<Type>::get69(); }
00265 
00267 template<class Type> inline Type a2dB (Type const& amp) throw()        { return log10 (amp) * Math<Type>::get20(); }
00268 
00270 template<class Type> inline Type dB2a (Type const& db) throw()         { return pow (Math<Type>::get10(), db / Math<Type>::get20()); }
00271 
00273 template<class Type> inline Type d2r (Type const& deg) throw()         { return deg * Math<Type>::get1_360() * Math<Type>::get2Pi(); }
00274 
00276 template<class Type> inline Type r2d (Type const& deg) throw()         { return deg * Math<Type>::get1_2Pi() * Math<Type>::get360(); }
00277 
00279 template<class Type> inline Type distort (Type const& a) throw()       { return a / (Math<Type>::get1() + abs (a)); }
00280 
00283 template<class Type>
00284 inline Type zap (Type const& x) throw()
00285 {
00286     static const Type smallest (static_cast<Type> (1e-15));
00287     static const Type biggest (static_cast<Type> (1e15));
00288     const Type absx = abs (x);
00289     return ((absx > smallest) && (absx < biggest)) ? x : Math<Type>::get0();
00290 }
00291 
00293 
00294 #define PLONK_UNARYOPFUNCTION_DEFINE(OP)\
00295     static inline OperandType OP (OperandType const& a) throw() { return plonk::OP (a); }
00296 
00297 template<class OperandType>
00298 class UnaryOpFunctions
00299 {
00300 public:        
00301     PLONK_UNARYOPFUNCTION_DEFINE(move)
00302     PLONK_UNARYOPFUNCTION_DEFINE(inc)
00303     PLONK_UNARYOPFUNCTION_DEFINE(dec)
00304     PLONK_UNARYOPFUNCTION_DEFINE(abs)
00305     PLONK_UNARYOPFUNCTION_DEFINE(log2)
00306     PLONK_UNARYOPFUNCTION_DEFINE(neg)
00307     PLONK_UNARYOPFUNCTION_DEFINE(reciprocal)
00308     PLONK_UNARYOPFUNCTION_DEFINE(sin)
00309     PLONK_UNARYOPFUNCTION_DEFINE(cos)
00310     PLONK_UNARYOPFUNCTION_DEFINE(tan)
00311     PLONK_UNARYOPFUNCTION_DEFINE(asin)
00312     PLONK_UNARYOPFUNCTION_DEFINE(acos)
00313     PLONK_UNARYOPFUNCTION_DEFINE(atan)
00314     PLONK_UNARYOPFUNCTION_DEFINE(sinh)
00315     PLONK_UNARYOPFUNCTION_DEFINE(cosh)
00316     PLONK_UNARYOPFUNCTION_DEFINE(tanh)
00317     PLONK_UNARYOPFUNCTION_DEFINE(sqrt)
00318     PLONK_UNARYOPFUNCTION_DEFINE(log)
00319     PLONK_UNARYOPFUNCTION_DEFINE(log10)
00320     PLONK_UNARYOPFUNCTION_DEFINE(exp)
00321     PLONK_UNARYOPFUNCTION_DEFINE(squared)
00322     PLONK_UNARYOPFUNCTION_DEFINE(cubed)
00323     PLONK_UNARYOPFUNCTION_DEFINE(ceil)
00324     PLONK_UNARYOPFUNCTION_DEFINE(floor)
00325     PLONK_UNARYOPFUNCTION_DEFINE(frac)
00326     PLONK_UNARYOPFUNCTION_DEFINE(sign)
00327     PLONK_UNARYOPFUNCTION_DEFINE(m2f)
00328     PLONK_UNARYOPFUNCTION_DEFINE(f2m)
00329     PLONK_UNARYOPFUNCTION_DEFINE(a2dB)
00330     PLONK_UNARYOPFUNCTION_DEFINE(dB2a)
00331     PLONK_UNARYOPFUNCTION_DEFINE(d2r)
00332     PLONK_UNARYOPFUNCTION_DEFINE(r2d)
00333     PLONK_UNARYOPFUNCTION_DEFINE(distort)
00334     PLONK_UNARYOPFUNCTION_DEFINE(zap)
00335 };
00336 
00337 template<class OperandType>
00338 class UnaryOpFunctionsHelper
00339 {
00340 public:
00341     typedef UnaryOpFunctions<OperandType> UnaryOpFunctionsType;
00342 };
00343 
00344 
00345 #endif // PLONK_INLINEUNARYOPS_H
 All Classes Functions Typedefs Enumerations Enumerator Properties