61#ifndef R8BBASE_INCLUDED
62#define R8BBASE_INCLUDED
64#define R8B_VERSION "7.5"
82#if __cplusplus >= 201103L
87 #define R8B_CONST constexpr
88 #define R8B_NULL nullptr
100 #define R8B_CONST static const
101 #define R8B_NULL NULL
107#if defined( __aarch64__ ) || defined( __arm64__ ) || \
108 defined( _M_ARM64 ) || defined( _M_ARM64EC )
110 #if defined( _MSC_VER )
111 #include <arm64_neon.h>
113 #include <arm_neon.h>
118 #if !defined( __APPLE__ )
122#elif defined( __SSE2__ ) || defined( _M_AMD64 ) || \
123 ( defined( _M_IX86_FP ) && _M_IX86_FP == 2 )
125 #if defined( _MSC_VER )
128 #include <emmintrin.h>
143#if defined( __clang__ )
145 #define R8B_EXITDTOR __attribute__((always_destroy))
179#if __cplusplus >= 201103L
181 using std :: uintptr_t;
204#if __cplusplus >= 201103L
206 #define R8BNOCTOR( ClassName ) \
208 ClassName( const ClassName& ) = delete; \
209 ClassName& operator = ( const ClassName& ) = delete;
213 #define R8BNOCTOR( ClassName ) \
215 ClassName( const ClassName& ) { } \
216 ClassName& operator = ( const ClassName& ) { return( *this ); }
246 return(
new char[ Size ]);
272template<
typename T >
273inline T*
align_ptr( T*
const ptr,
const uintptr_t align )
275 return( (T*) (( (uintptr_t) ptr + align - 1 ) & ~( align - 1 )));
288template<
typename T >
291 return( ::
new( ptr ) T[ c ]);
312template<
typename T >
335 Data0 = allocmem( (
size_t) Capacity *
sizeof( T ) + Alignment );
359 Data0 = allocmem( (
size_t) Capacity *
sizeof( T ) + Alignment );
375 void realloc(
const int PrevCapacity,
const int NewCapacity )
380 void*
const NewData0 = allocmem( (
size_t) NewCapacity *
sizeof( T ) +
384 Alignment ), (
size_t) NewCapacity );
386 const size_t CopySize = ( PrevCapacity > NewCapacity ?
387 (size_t) NewCapacity : (size_t) PrevCapacity ) *
sizeof( T );
391 memcpy( NewData, Data, CopySize );
438 static const size_t Alignment = 64;
455template<
typename T >
473 template<
typename T2 >
492 template<
typename T2 >
525 T*
const DelObj = Object;
537 T*
const ResObject = Object;
555template<
typename T >
573 template<
typename T2 >
595 template<
typename T2 >
630 T*
const RefObj = Object;
649class CSinglyLinkedListItem
655 CSinglyLinkedListItem()
672#if __cplusplus >= 201103L
675typedef std :: lock_guard< std :: mutex >
CSyncKeeper;
697 #if defined( _WIN32 )
698 InitializeCriticalSectionAndSpinCount( &CritSec, 2000 );
700 pthread_mutexattr_t MutexAttrs;
701 pthread_mutexattr_init( &MutexAttrs );
702 pthread_mutexattr_settype( &MutexAttrs, PTHREAD_MUTEX_RECURSIVE );
703 pthread_mutex_init( &Mutex, &MutexAttrs );
704 pthread_mutexattr_destroy( &MutexAttrs );
710 #if defined( _WIN32 )
711 DeleteCriticalSection( &CritSec );
713 pthread_mutex_destroy( &Mutex );
724 #if defined( _WIN32 )
725 EnterCriticalSection( &CritSec );
727 pthread_mutex_lock( &Mutex );
738 #if defined( _WIN32 )
739 LeaveCriticalSection( &CritSec );
741 pthread_mutex_unlock( &Mutex );
746 #if defined( _WIN32 )
747 CRITICAL_SECTION CritSec;
750 pthread_mutex_t Mutex;
782 : SyncObj( aSyncObj )
786 SyncObj -> acquire();
798 : SyncObj( &aSyncObj )
800 SyncObj -> acquire();
807 SyncObj -> release();
812 CSyncObject* SyncObj;
833#define R8BSYNC( SyncObject ) R8BSYNC1( SyncObject, __LINE__ )
834#define R8BSYNC1( SyncObject, id ) R8BSYNC2( SyncObject, id )
835#define R8BSYNC2( SyncObject, id ) \
836 const CSyncKeeper SyncKeeper##id( SyncObject )
860 : svalue1( sin( ph ))
861 , svalue2( sin( ph - si ))
862 , sincr( 2.0 * cos( si ))
875 CSineGen(
const double si,
const double ph,
const double g )
876 : svalue1( sin( ph ) * g )
877 , svalue2( sin( ph - si ) * g )
878 , sincr( 2.0 * cos( si ))
890 void init(
const double si,
const double ph )
893 svalue2 = sin( ph - si );
894 sincr = 2.0 * cos( si );
906 void init(
const double si,
const double ph,
const double g )
908 svalue1 = sin( ph ) * g;
909 svalue2 = sin( ph - si ) * g;
910 sincr = 2.0 * cos( si );
921 const double res = svalue1;
923 svalue1 = sincr * res - svalue2;
946 static const unsigned char OccupancyTable[] =
948 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
949 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
950 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
951 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
952 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
953 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
954 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
955 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
956 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
957 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
958 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
959 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
960 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
961 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
962 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
963 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
966 const int tt = v >> 16;
970 const int t = v >> 24;
972 return( t != 0 ? 24 + OccupancyTable[ t & 0xFF ] :
973 16 + OccupancyTable[ tt ]);
977 const int t = v >> 8;
979 return( t != 0 ? 8 + OccupancyTable[ t ] : OccupancyTable[ v ]);
998 const double th,
double& re0,
double& im0,
const int fltlat = 0 )
1000 const double sincr = 2.0 * cos( th );
1011 cvalue1 = cos( -fltlat * th );
1012 svalue1 = sin( -fltlat * th );
1015 double cvalue2 = cos( -( fltlat + 1 ) * th );
1016 double svalue2 = sin( -( fltlat + 1 ) * th );
1023 re += cvalue1 * flt[ 0 ];
1024 im += svalue1 * flt[ 0 ];
1028 double tmp = cvalue1;
1029 cvalue1 = sincr * cvalue1 - cvalue2;
1033 svalue1 = sincr * svalue1 - svalue2;
1055 const int fltlen,
const double th )
1057 const int Count = 2;
1058 const double thd2 = 1e-9;
1059 double ths[ Count ] = { th - thd2, th + thd2 };
1061 if( ths[ 0 ] < 0.0 )
1071 double ph1[ Count ];
1074 for( i = 0; i < Count; i++ )
1080 ph1[ i ] = atan2( im1, re1 );
1083 if( fabs( ph1[ 1 ] - ph1[ 0 ]) >
R8B_PI )
1085 if( ph1[ 1 ] > ph1[ 0 ])
1095 const double thd = ths[ 1 ] - ths[ 0 ];
1097 return(( ph1[ 1 ] - ph1[ 0 ]) / thd );
1113 const double DCGain,
const int pstep = 1 )
1159 const double xm2,
const double xm1,
const double x0,
const double x1,
1160 const double x2,
const double x3,
const double x4 )
1163 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1164 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1166 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
1167 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) * 1.31578947368421052e-2;
1169 c[ 3 ] = ( 91.0 * ( x0 - x1 ) + 45.0 * ( x2 - xm1 ) +
1170 13.0 * ( xm2 - x3 ) + 3.0 * ( x4 - xm3 )) * 1.31578947368421052e-2;
1193 const double xm2,
const double xm1,
const double x0,
const double x1,
1194 const double x2,
const double x3,
const double x4 )
1197 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1198 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1200 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
1201 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) * 1.31578947368421052e-2;
1218 c[ 1 ] = 0.5 * ( y[ 2 ] - y[ 0 ]);
1219 c[ 2 ] = y[ 0 ] - 2.5 * y[ 1 ] + y[ 2 ] + y[ 2 ] - 0.5 * y[ 3 ];
1220 c[ 3 ] = 0.5 * ( y[ 3 ] - y[ 0 ] ) + 1.5 * ( y[ 1 ] - y[ 2 ]);
1237 c[ 1 ] = ( 11.0 * ( y[ 3 ] - y[ 1 ]) + 2.0 * ( y[ 0 ] - y[ 4 ])) / 14.0;
1238 c[ 2 ] = ( 20.0 * ( y[ 1 ] + y[ 3 ]) + 2.0 * y[ 5 ] - 4.0 * y[ 0 ] -
1239 7.0 * y[ 4 ] - 31.0 * y[ 2 ]) / 14.0;
1241 c[ 3 ] = ( 17.0 * ( y[ 2 ] - y[ 3 ]) + 9.0 * ( y[ 4 ] - y[ 1 ]) +
1242 2.0 * ( y[ 0 ] - y[ 5 ])) / 14.0;
1256template<
typename T >
1257inline T
min(
const T& v1,
const T& v2 )
1259 return( v1 < v2 ? v1 : v2 );
1275template<
typename T >
1276inline T
max(
const T& v1,
const T& v2 )
1278 return( v1 > v2 ? v1 : v2 );
1295inline double clampr(
const double Value,
const double minv,
1318inline double sqr(
const double x )
1332inline double pow_a(
const double v,
const double p )
1334 return( exp( p * log( fabs( v ) + 1e-300 )));
1346 return( exp( -( v * v )));
1358 return( log( v + sqrt( v * v + 1.0 )));
1372 const double ax = fabs( x );
1380 return( 1.0 + y * ( 3.5156229 + y * ( 3.0899424 + y * ( 1.2067492 +
1381 y * ( 0.2659732 + y * ( 0.360768e-1 + y * 0.45813e-2 ))))));
1386 return( exp( ax ) / sqrt( ax ) * ( 0.39894228 + y * ( 0.1328592e-1 +
1387 y * ( 0.225319e-2 + y * ( -0.157565e-2 + y * ( 0.916281e-2 +
1388 y * ( -0.2057706e-1 + y * ( 0.2635537e-1 + y * ( -0.1647633e-1 +
1389 y * 0.392377e-2 )))))))));
#define R8B_NULL
The "null pointer" value, portable between C++11 and earlier C++ versions.
Definition r8bbase.h:101
#define R8B_CONST
The prefix for constant definitions, portable between C++11 and earlier C++ versions.
Definition r8bbase.h:100
#define R8BNOCTOR(ClassName)
Macro that defines empty copy-constructor and copy operator.
Definition r8bbase.h:213
The "configuration" inclusion file you can modify.
#define R8BASSERT(e)
Assertion macro used to check for certain run-time conditions. By default, no action is taken if asse...
Definition r8bconf.h:28
#define R8B_MEMALLOCCLASS
Macro defines the name of the class that implements raw memory allocation functions,...
Definition r8bconf.h:65
The "r8brain-free-src" library namespace.
Definition CDSPBlockConvolver.h:22
double calcFIRFilterGroupDelay(const double *const flt, const int fltlen, const double th)
FIR filter's group delay calculation function.
Definition r8bbase.h:1054
void calcSpline3p4Coeffs(double *const c, const double *const y)
Calculates 3rd order spline coefficients, using 4 points.
Definition r8bbase.h:1215
double pow_a(const double v, const double p)
Power of an absolute value.
Definition r8bbase.h:1332
R8B_CONST double R8B_PI
Equals pi.
Definition r8bbase.h:185
void calcFIRFilterResponse(const double *flt, int fltlen, const double th, double &re0, double &im0, const int fltlat=0)
FIR filter's frequency response calculation.
Definition r8bbase.h:997
R8B_CONST double R8B_3PI
Equals 3*pi.
Definition r8bbase.h:187
void calcSpline3p6Coeffs(double *const c, const double *const y)
Calculates 3rd order spline coefficients, using 6 points.
Definition r8bbase.h:1234
R8B_CONST double R8B_2PI
Equals 2*pi.
Definition r8bbase.h:186
R8B_CONST double R8B_PId2
Equals 0.5*pi.
Definition r8bbase.h:188
T min(const T &v1, const T &v2)
Returns minimum of two values.
Definition r8bbase.h:1257
void calcSpline3p8Coeffs(double *const c, const double xm3, const double xm2, const double xm1, const double x0, const double x1, const double x2, const double x3, const double x4)
Calculates 3rd order spline coefficients, using 8 points.
Definition r8bbase.h:1158
int getBitOccupancy(const int v)
Calculate the exact number of bits a value needs for representation.
Definition r8bbase.h:944
double asinh(const double v)
Hyperbolic sine of a value.
Definition r8bbase.h:1356
T * construct_ptr(void *const ptr, const size_t c)
Performs placement new to turn a block of unoccupied memory into a "constructed" array of elements of...
Definition r8bbase.h:289
T max(const T &v1, const T &v2)
Returns maximum of two values.
Definition r8bbase.h:1276
double clampr(const double Value, const double minv, const double maxv)
Clamps a value to be within the specified min-max range.
Definition r8bbase.h:1295
void calcSpline2p8Coeffs(double *const c, const double xm3, const double xm2, const double xm1, const double x0, const double x1, const double x2, const double x3, const double x4)
Calculates 2nd order spline coefficients, using 8 points.
Definition r8bbase.h:1192
T * align_ptr(T *const ptr, const uintptr_t align)
Forces the provided ptr pointer to be aligned to align bytes.
Definition r8bbase.h:273
double gauss(const double v)
Single-argument Gaussian function of a value.
Definition r8bbase.h:1344
double besselI0(const double x)
1st kind, 0th order modified Bessel function of a value.
Definition r8bbase.h:1370
void normalizeFIRFilter(double *const p, const int l, const double DCGain, const int pstep=1)
FIR filter's gain normalization.
Definition r8bbase.h:1112
double sqr(const double x)
Returns square ot a value.
Definition r8bbase.h:1318
The default base class for objects created on heap.
Definition r8bbase.h:225
The default base class for objects that allocate blocks of memory.
Definition r8bbase.h:235
static void * allocmem(const size_t Size)
Allocates a memory block.
Definition r8bbase.h:244
static void freemem(void *const p)
Frees a previously allocated memory block.
Definition r8bbase.h:255
Templated memory buffer class for element buffers of fixed capacity.
Definition r8bbase.h:314
void alloc(const int Capacity)
Allocates memory so that the specified number of elements of type T can be stored in this buffer obje...
Definition r8bbase.h:354
void moveFrom(CFixedBuffer &s)
Moves buffer from another object to this object.
Definition r8bbase.h:407
void free()
Deallocates a previously allocated buffer.
Definition r8bbase.h:420
CFixedBuffer(const int Capacity)
Constructor allocates memory so that the specified number of elements of type T can be stored in this...
Definition r8bbase.h:331
void realloc(const int PrevCapacity, const int NewCapacity)
Reallocates memory so that the specified number of elements of type T can be stored in this buffer ob...
Definition r8bbase.h:375
Pointer-to-object "keeper" class with automatic deletion.
Definition r8bbase.h:457
T * operator->() const
Returns pointer to keeped object, or nullptr, if no object is being kept.
Definition r8bbase.h:504
T * unkeep()
Returns the keeped pointer and resets it in this keeper without object deletion.
Definition r8bbase.h:535
void operator=(T2 const aObject)
Assigns a pointer to object to this keeper. A previously keeped pointer will be reset and object dele...
Definition r8bbase.h:493
CPtrKeeper(T2 const aObject)
Constructor assigns a pointer to object to this keeper.
Definition r8bbase.h:474
void reset()
Resets the keeped pointer and deletes the keeped object.
Definition r8bbase.h:523
Reference "keeper" class with automatic unref() call.
Definition r8bbase.h:557
T * operator->() const
Returns pointer to keeped object, or nullptr, if no object is being kept.
Definition r8bbase.h:607
void operator=(T2 const aObject)
Assigns a pointer to object to this keeper. A previously keeped pointer will be reset and object unre...
Definition r8bbase.h:596
CRefKeeper(T2 const aObject)
Constructor assigns a pointer to object to this keeper.
Definition r8bbase.h:574
void reset()
Resets the keeped pointer and unreferences the keeped object.
Definition r8bbase.h:626
Class for items of a singly-linked list.
Definition r8bbase.h:650
T * Next
Next object of type T in a singly-linked list.
Definition r8bbase.h:652
Multi-threaded synchronization object class.
Definition r8bbase.h:691
void acquire()
Acquires this thread synchronizer object immediately or waits until another thread releases it.
Definition r8bbase.h:722
void release()
Releases this, previously acquired, thread synchronizer object.
Definition r8bbase.h:736
A "keeper" class for CSyncObject-based synchronization.
Definition r8bbase.h:765
CSyncKeeper(CSyncObject &aSyncObj)
Constructor acquires a specified synchronization object.
Definition r8bbase.h:797
CSyncKeeper(CSyncObject *const aSyncObj)
Constructor acquires a specified synchronization object.
Definition r8bbase.h:781
CSineGen(const double si, const double ph)
Constructor initializes this sine signal generator, with unity gain output.
Definition r8bbase.h:859
CSineGen(const double si, const double ph, const double g)
Constructor initializes this sine signal generator.
Definition r8bbase.h:875
void init(const double si, const double ph, const double g)
Function initializes this sine signal generator.
Definition r8bbase.h:906
void init(const double si, const double ph)
Function initializes this sine signal generator, with unity gain output.
Definition r8bbase.h:890
double generate()
Generates the next sample.
Definition r8bbase.h:919