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