AAX SDK  2.4.1
Avid Audio Extensions Development Kit
AAX_MiscUtils.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  * Copyright 2013-2015, 2018 by Avid Technology, Inc.
4  * All rights reserved.
5  *
6  * CONFIDENTIAL: This document contains confidential information. Do not
7  * read or examine this document unless you are an Avid Technology employee
8  * or have signed a non-disclosure agreement with Avid Technology which protects
9  * the confidentiality of this document. DO NOT DISCLOSE ANY INFORMATION
10  * CONTAINED IN THIS DOCUMENT TO ANY THIRD-PARTY WITHOUT THE PRIOR WRITTEN CONSENT
11  * OF Avid Technology, INC.
12  */
13 
20 /*================================================================================================*/
21 #pragma once
22 
23 #ifndef AAX_MISCUTILS_H
24 #define AAX_MISCUTILS_H
25 
27 #include "AAX_Constants.h"
28 
29 #if defined(_MSC_VER)
30  #define DECLARE_ALIGNED(t,v,x) __declspec(align(x)) t v
31 // #define DECLARE_ALIGNED(t,v,x) t v
32 #elif defined (__GNUC__)
33  #define DECLARE_ALIGNED(t,v,x) t v __attribute__ ((aligned (x)))
34 // #define DECLARE_ALIGNED(t,v,x) t v
35 #elif defined (_TMS320C6X)
36  #define DECLARE_ALIGNED(t,v,x) t v
37  #warn "DECLARE_ALIGNED macro does not currently align data on TI"
38 #else
39  #error "DigiError: Please port the DECLARE_ALIGNED macro to this platform"
40 #endif
41 
42 
48 #ifdef _TMS320C6X
49 #define AAX_ALIGNMENT_HINT(a,b) std::_nassert(((int)(a) % b) == 0)
50 #define AAX_WORD_ALIGNED_HINT(a) AAX_ALIGNMENT_HINT(a,4)
51 #define AAX_DWORD_ALIGNED_HINT(a) AAX_ALIGNMENT_HINT(a,8)
52 #else
53 #define AAX_ALIGNMENT_HINT(a,b)
54 #define AAX_WORD_ALIGNED_HINT(a)
55 #define AAX_DWORD_ALIGNED_HINT(a)
56 #endif
57 
63 #ifdef _TMS320C6X
64 #define AAX_LO(x) (_itof(_lo((_amemd8_const(&x)))))
65 #define AAX_HI(x) (_itof(_hi((_amemd8_const(&x)))))
66 #define AAX_INT_LO(x) (_lo((_amemd8_const(&x))))
67 #define AAX_INT_HI(x) (_hi((_amemd8_const(&x))))
68 #else //for non-TI systems increment pointer by 4-bytes to simulate hi-word access
69 #define AAX_LO(x) x
70 #define AAX_HI(x) *((const_cast<float*>(&x))+1)
71 #define AAX_INT_LO(x) x
72 #define AAX_INT_HI(x) *((const_cast<int32_t*>(reinterpret_cast<const int32_t*>(&x)))+1)
73 #endif
74 
75 
76 
77 
78 namespace AAX
79 {
80 
81 template<class GFLOAT>
82 inline GFLOAT ClampToZero(GFLOAT iValue, GFLOAT iClampThreshold)
83 {
84  return (iValue < iClampThreshold && iValue > -iClampThreshold) ? 0.0 : iValue;
85 }
86 
87 /*template<class GFLOAT>
88 class SmoothingFilter
89 {
90 public:
91  SmoothingFilter() { Reset(); }
92  void Reset(void) { mYnm1=0.0; }
93  void Reset(double iInitialState) { mYnm1=iInitialState; }
94  void SetSmothingRate(double iFrequency, double iSampleRate) { mZeroCoef = 1.0-std::exp(iFrequency*-cTwoPi/iSampleRate); }
95  void SetSmoothingCoeff(GFLOAT b0) { mZeroCoef = b0; }
96  inline GFLOAT Smooth(GFLOAT iInput)
97  {
98  mYnm1 += mZeroCoef * (iInput - mYnm1);
99  DeDenormal(mYnm1);
100  return (GFLOAT)mYnm1;
101  }
102 
103 private:
104  double mYnm1; // y[n-1]
105  GFLOAT mZeroCoef;
106 };*/
107 
108 inline void ZeroMemory(void* iPointer, int iNumBytes)
109 {
110  char* aCharPointer=static_cast<char*>(iPointer);
111 
112  for(int aIndex=0; aIndex<iNumBytes; aIndex++)
113  {
114  *aCharPointer=0;
115  aCharPointer++;
116  }
117 }
118 
119 //Warning: the number of bytes passed in must be multiple of 4
120 inline void ZeroMemoryDW(void* iPointer, int iNumBytes)
121 {
122  int* aDWPointer=static_cast<int*>(iPointer);
123 
124  int numDWords=iNumBytes/sizeof(int);
125  for(int aIndex=0; aIndex<numDWords; aIndex++)
126  {
127  *aDWPointer=0;
128  aDWPointer++;
129  }
130 }
131 
132 template<typename T, int N> void Fill( T *iArray, const T* iVal )
133 {
134 // std::fill_n( iArray, N, iVal );
135  for (int i = 0; i != N; ++i)
136  {
137  *(iArray + i) = *iVal;
138  }
139 }
140 
141 template< typename T, int M, int N > inline void Fill( T *iArray, const T* iVal )//Fill( T (&iArray)[M][N], const T& iVal )
142 {
143  for ( int i = 0; i != M; ++i )
144  {
145  Fill( iArray + i, iVal );
146  }
147 }
148 
149 template< typename T, int L, int M, int N > inline void Fill( T *iArray, const T* iVal ) //Fill( T (&iArray)[L][M][N], const T& iVal )
150 {
151  for ( int i = 0; i != L; ++i )
152  {
153  Fill( iArray + i, iVal );
154  }
155 }
156 
157 static const int cSignBitWord = cLittleEndian;
158 
159 double
160 inline fabs(double iVal)
161 {
162  //On Windows this version is slower than std::fabs so use that instead.
163 #if defined(MAC_VERSION)
164  double aVal = iVal;
165  int* tempptr = (reinterpret_cast<int*>(&aVal))+cSignBitWord;
166  *tempptr &= 0x7fffffff;
167 
168  return aVal;
169 #else
170  return std::fabs(iVal);
171 #endif
172 }
173 
174 float
175 inline fabs(float iVal)
176 {
177  // Call intrinsic if on TI c6727. fabs is about the same speed as std::fabs,
178  // although metrowerks v9.5 MSL libraries are much slower so for the time
179  // being were using this.
180  int temp = (*(reinterpret_cast<int*>(&iVal)) & 0x7fffffff);
181  return *reinterpret_cast<float*>(&temp);
182 }
183 
184 float
185 inline fabsf(float iVal)
186 {
187  return AAX::fabs (iVal);
188 }
189 
190 template <class T>
191 inline T AbsMax(const T& iValue, const T& iMax)
192 {
193  return std::fabs(iValue) < std::fabs(iMax) ? iMax : iValue;
194 }
195 
196 template <class T>
197 inline T MinMax(const T& iValue, const T& iMin, const T& iMax)
198 {
199  return iValue > iMax ? iMax : (iValue < iMin ? iMin : iValue);
200 }
201 
202 template <class T>
203 inline T Max(const T& iValue1, const T& iValue2)
204 {
205  return iValue1 > iValue2 ? iValue1 : iValue2;
206 }
207 
208 template <class T>
209 inline T Min(const T& iValue1, const T& iValue2)
210 {
211  return iValue1 < iValue2 ? iValue1 : iValue2;
212 }
213 
214 template <class T>
215 inline T Sign(const T& iValue)
216 {
217  return iValue < (T)(0.0) ? (T)(-1.0) : (T)(1.0);
218 }
219 
220 //Polynomial evaluation.
221 inline double PolyEval(double x, const double* coefs, int numCoefs)
222 {
223  // Coefs are ordered from highest degree to lowest (Matlab convention)
224  if(numCoefs < 1) return 0.0;
225 
226  double result = coefs[0];
227  for(int i = 1; i < numCoefs; ++i)
228  {
229  result = result*x + coefs[i];
230  };
231 
232  return result;
233 }
234 
235 inline double CeilLog2(double iValue)
236 {
237  return std::pow(2.0f, (float)(std::ceil(std::log(iValue)/std::log(2.0f))));
238 }
239 
240 inline void SinCosMix(float aLinearMix, float &aSinMix, float &aCosMix)
241 {
242  aSinMix=static_cast< float >( std::sin(aLinearMix*cHalfPi) );
243  aCosMix=static_cast< float >( std::cos(aLinearMix*cHalfPi) );
244 }
245 
246 
247 } // namespace AAX
248 
249 
250 // Some methods have been broken off into AAX_Denormal.h; including
251 // AAX_Denormal.h here for compatibility with projects that have not
252 // yet been updated to include this new header.
253 #include "AAX_Denormal.h"
254 
255 #endif // AAX_MISCUTILS_H
Signal processing constants.
Signal processing utilities for denormal/subnormal floating point numbers.
Definition: AAX_Exception.h:42
void ZeroMemoryDW(void *iPointer, int iNumBytes)
Definition: AAX_MiscUtils.h:120
void Fill(T *iArray, const T *iVal)
Definition: AAX_MiscUtils.h:132
void SinCosMix(float aLinearMix, float &aSinMix, float &aCosMix)
Definition: AAX_MiscUtils.h:240
T Min(const T &iValue1, const T &iValue2)
Definition: AAX_MiscUtils.h:209
const double cHalfPi
Definition: AAX_Constants.h:53
T Max(const T &iValue1, const T &iValue2)
Definition: AAX_MiscUtils.h:203
GFLOAT ClampToZero(GFLOAT iValue, GFLOAT iClampThreshold)
Definition: AAX_MiscUtils.h:82
float fabsf(float iVal)
Definition: AAX_MiscUtils.h:185
double PolyEval(double x, const double *coefs, int numCoefs)
Definition: AAX_MiscUtils.h:221
T AbsMax(const T &iValue, const T &iMax)
Definition: AAX_MiscUtils.h:191
double fabs(double iVal)
Definition: AAX_MiscUtils.h:160
double CeilLog2(double iValue)
Definition: AAX_MiscUtils.h:235
T Sign(const T &iValue)
Definition: AAX_MiscUtils.h:215
T MinMax(const T &iValue, const T &iMin, const T &iMax)
Definition: AAX_MiscUtils.h:197
void ZeroMemory(void *iPointer, int iNumBytes)
Definition: AAX_MiscUtils.h:108
float fabs(float iVal)
Definition: AAX_MiscUtils.h:175
const int cLittleEndian
Definition: AAX_Constants.h:48