61#ifndef R8BBASE_INCLUDED
62#define R8BBASE_INCLUDED
64#define R8B_VERSION "7.7"
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>
141#if defined( __LP64__ ) || defined( _LP64 ) || \
142 defined( __x86_64__ ) || defined( __aarch64__ ) || \
143 defined( _M_AMD64 ) || defined( _M_ARM64 )
156#if defined( __clang__ )
158 #define R8B_EXITDTOR __attribute__((always_destroy))
187#if __cplusplus >= 201103L
189 #if defined( R8B_ARCH64 )
190 using std :: int64_t;
193 using std :: uintptr_t;
216#if __cplusplus >= 201103L
218 #define R8BNOCTOR( ClassName ) \
220 ClassName( const ClassName& ) = delete; \
221 ClassName& operator = ( const ClassName& ) = delete;
225 #define R8BNOCTOR( ClassName ) \
227 ClassName( const ClassName& ) { } \
228 ClassName& operator = ( const ClassName& ) { return( *this ); }
258 return(
new char[ Size ]);
284template<
typename T >
285inline T*
align_ptr( T*
const ptr,
const uintptr_t align )
287 return( (T*) (( (uintptr_t) ptr + align - 1 ) & ~( align - 1 )));
300template<
typename T >
303 return( ::
new( ptr ) T[ c ]);
324template<
typename T >
347 Data0 = allocmem( (
size_t) Capacity *
sizeof( T ) + Alignment );
371 Data0 = allocmem( (
size_t) Capacity *
sizeof( T ) + Alignment );
387 void realloc(
const int PrevCapacity,
const int NewCapacity )
392 void*
const NewData0 = allocmem( (
size_t) NewCapacity *
sizeof( T ) +
396 Alignment ), (
size_t) NewCapacity );
398 const size_t CopySize = ( PrevCapacity > NewCapacity ?
399 (size_t) NewCapacity : (size_t) PrevCapacity ) *
sizeof( T );
403 memcpy( NewData, Data, CopySize );
450 static const size_t Alignment = 64;
467template<
typename T >
485 template<
typename T2 >
504 template<
typename T2 >
537 T*
const DelObj = Object;
549 T*
const ResObject = Object;
567template<
typename T >
585 template<
typename T2 >
607 template<
typename T2 >
642 T*
const RefObj = Object;
661class CSinglyLinkedListItem
667 CSinglyLinkedListItem()
684#if __cplusplus >= 201103L
687typedef std :: lock_guard< std :: mutex >
CSyncKeeper;
709 #if defined( _WIN32 )
710 InitializeCriticalSectionAndSpinCount( &CritSec, 2000 );
712 pthread_mutexattr_t MutexAttrs;
713 pthread_mutexattr_init( &MutexAttrs );
714 pthread_mutexattr_settype( &MutexAttrs, PTHREAD_MUTEX_RECURSIVE );
715 pthread_mutex_init( &Mutex, &MutexAttrs );
716 pthread_mutexattr_destroy( &MutexAttrs );
722 #if defined( _WIN32 )
723 DeleteCriticalSection( &CritSec );
725 pthread_mutex_destroy( &Mutex );
736 #if defined( _WIN32 )
737 EnterCriticalSection( &CritSec );
739 pthread_mutex_lock( &Mutex );
750 #if defined( _WIN32 )
751 LeaveCriticalSection( &CritSec );
753 pthread_mutex_unlock( &Mutex );
758 #if defined( _WIN32 )
759 CRITICAL_SECTION CritSec;
762 pthread_mutex_t Mutex;
794 : SyncObj( aSyncObj )
798 SyncObj -> acquire();
810 : SyncObj( &aSyncObj )
812 SyncObj -> acquire();
819 SyncObj -> release();
824 CSyncObject* SyncObj;
845#define R8BSYNC( SyncObject ) R8BSYNC1( SyncObject, __LINE__ )
846#define R8BSYNC1( SyncObject, id ) R8BSYNC2( SyncObject, id )
847#define R8BSYNC2( SyncObject, id ) \
848 const CSyncKeeper SyncKeeper##id( SyncObject )
872 : svalue1( sin( ph ))
873 , svalue2( sin( ph - si ))
874 , sincr( 2.0 * cos( si ))
887 CSineGen(
const double si,
const double ph,
const double g )
888 : svalue1( sin( ph ) * g )
889 , svalue2( sin( ph - si ) * g )
890 , sincr( 2.0 * cos( si ))
902 void init(
const double si,
const double ph )
905 svalue2 = sin( ph - si );
906 sincr = 2.0 * cos( si );
918 void init(
const double si,
const double ph,
const double g )
920 svalue1 = sin( ph ) * g;
921 svalue2 = sin( ph - si ) * g;
922 sincr = 2.0 * cos( si );
933 const double res = svalue1;
935 svalue1 = sincr * res - svalue2;
958 static const unsigned char OccupancyTable[] =
960 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
961 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
962 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
963 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
964 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
965 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
966 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
967 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
968 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
969 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
970 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
971 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
972 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
973 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
974 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
975 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
978 const int tt = v >> 16;
982 const int t = v >> 24;
984 return( t != 0 ? 24 + OccupancyTable[ t & 0xFF ] :
985 16 + OccupancyTable[ tt ]);
989 const int t = v >> 8;
991 return( t != 0 ? 8 + OccupancyTable[ t ] : OccupancyTable[ v ]);
1010 const double th,
double& re0,
double& im0,
const int fltlat = 0 )
1012 const double sincr = 2.0 * cos( th );
1023 cvalue1 = cos( -fltlat * th );
1024 svalue1 = sin( -fltlat * th );
1027 double cvalue2 = cos( -( fltlat + 1 ) * th );
1028 double svalue2 = sin( -( fltlat + 1 ) * th );
1035 re += cvalue1 * flt[ 0 ];
1036 im += svalue1 * flt[ 0 ];
1040 double tmp = cvalue1;
1041 cvalue1 = sincr * cvalue1 - cvalue2;
1045 svalue1 = sincr * svalue1 - svalue2;
1067 const int fltlen,
const double th )
1069 const int Count = 2;
1070 const double thd2 = 1e-9;
1071 double ths[ Count ] = { th - thd2, th + thd2 };
1073 if( ths[ 0 ] < 0.0 )
1083 double ph1[ Count ];
1086 for( i = 0; i < Count; i++ )
1092 ph1[ i ] = atan2( im1, re1 );
1095 if( fabs( ph1[ 1 ] - ph1[ 0 ]) >
R8B_PI )
1097 if( ph1[ 1 ] > ph1[ 0 ])
1107 const double thd = ths[ 1 ] - ths[ 0 ];
1109 return(( ph1[ 1 ] - ph1[ 0 ]) / thd );
1125 const double DCGain,
const int pstep = 1 )
1171 const double xm2,
const double xm1,
const double x0,
const double x1,
1172 const double x2,
const double x3,
const double x4 )
1175 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1176 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1178 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
1179 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) * 1.31578947368421052e-2;
1181 c[ 3 ] = ( 91.0 * ( x0 - x1 ) + 45.0 * ( x2 - xm1 ) +
1182 13.0 * ( xm2 - x3 ) + 3.0 * ( x4 - xm3 )) * 1.31578947368421052e-2;
1205 const double xm2,
const double xm1,
const double x0,
const double x1,
1206 const double x2,
const double x3,
const double x4 )
1209 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1210 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1212 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
1213 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) * 1.31578947368421052e-2;
1230 c[ 1 ] = 0.5 * ( y[ 2 ] - y[ 0 ]);
1231 c[ 2 ] = y[ 0 ] - 2.5 * y[ 1 ] + y[ 2 ] + y[ 2 ] - 0.5 * y[ 3 ];
1232 c[ 3 ] = 0.5 * ( y[ 3 ] - y[ 0 ] ) + 1.5 * ( y[ 1 ] - y[ 2 ]);
1249 c[ 1 ] = ( 11.0 * ( y[ 3 ] - y[ 1 ]) + 2.0 * ( y[ 0 ] - y[ 4 ])) / 14.0;
1250 c[ 2 ] = ( 20.0 * ( y[ 1 ] + y[ 3 ]) + 2.0 * y[ 5 ] - 4.0 * y[ 0 ] -
1251 7.0 * y[ 4 ] - 31.0 * y[ 2 ]) / 14.0;
1253 c[ 3 ] = ( 17.0 * ( y[ 2 ] - y[ 3 ]) + 9.0 * ( y[ 4 ] - y[ 1 ]) +
1254 2.0 * ( y[ 0 ] - y[ 5 ])) / 14.0;
1268template<
typename T >
1269inline T
min(
const T& v1,
const T& v2 )
1271 return( v1 < v2 ? v1 : v2 );
1287template<
typename T >
1288inline T
max(
const T& v1,
const T& v2 )
1290 return( v1 > v2 ? v1 : v2 );
1307inline double clampr(
const double Value,
const double minv,
1330inline double sqr(
const double x )
1335#if defined( R8B_ARCH64 )
1342R8B_CONST int64_t LOGEXP_TAB32[ 32 + 1 ] = {
1343 0x0000000000000, 0x059B0D3158574, 0x0B5586CF9890F, 0x11301D0125B51,
1344 0x172B83C7D517B, 0x1D4873168B9AA, 0x2387A6E756238, 0x29E9DF51FDEE1,
1345 0x306FE0A31B715, 0x371A7373AA9CB, 0x3DEA64C123422, 0x44E086061892D,
1346 0x4BFDAD5362A27, 0x5342B569D4F82, 0x5AB07DD485429, 0x6247EB03A5585,
1347 0x6A09E667F3BCD, 0x71F75E8EC5F74, 0x7A11473EB0187, 0x82589994CCE13,
1348 0x8ACE5422AA0DB, 0x93737B0CDC5E5, 0x9C49182A3F090, 0xA5503B23E255D,
1349 0xAE89F995AD3AD, 0xB7F76F2FB5E47, 0xC199BDD85529C, 0xCB720DCEF9069,
1350 0xD5818DCFBA487, 0xDFC97337B9B5F, 0xEA4AFA2A490DA, 0xF50765B6E4540,
1364inline double exp_fast(
const double x )
1366 const double LOG2E_32 = 46.166241308446828;
1367 const double y = x * LOG2E_32 + 32736.0;
1369 if( !( y >= 32.0 ) )
1370 return( y != y ? x : 0.0 );
1374 int64_t n = (int) y;
1375 const double f = y - n;
1377 const int64_t m = LOGEXP_TAB32[ n & 31 ];
1378 n = ( n >> 5 ) << 52;
1382 memcpy( &s, &n,
sizeof( s ));
1384 const double p = 1.0 + f * ( 2.1660849389716318e-02
1385 + f * ( 2.3459622527899164e-04
1386 + f * ( 1.6937691434532236e-06
1387 + f * 9.2698881870148910e-09 ) ) );
1408inline double log_fast(
double x )
1411 memcpy( &ix, &x,
sizeof( ix ));
1423 bits = -0x0010000000000000;
1427 bits = 0x7ff8000000000000;
1431 memcpy( &r, &bits,
sizeof( r ));
1436 int e = (int) ( ix >> 52 );
1441 x *= 9007199254740992.0;
1442 memcpy( &ix, &x,
sizeof( ix ));
1443 e = (int) (( ix >> 52 ) - 1076 );
1455 const int64_t sy = ix & 0x000fffffffffffff;
1464 static const unsigned char LOG_CENTER32[ 32 ] = {
1465 1, 2, 3, 5, 6, 7, 9, 10,
1466 11, 12, 13, 14, 15, 16, 17, 18,
1467 19, 20, 21, 22, 23, 24, 25, 25,
1468 26, 27, 28, 29, 29, 30, 31, 31
1471 int k = LOG_CENTER32[ sy >> 47 ];
1472 const int64_t sy2 = sy << 1;
1473 int64_t km = LOGEXP_TAB32[ k ];
1476 int64_t ybits = 0x3ff0000000000000 | sy;
1477 const int64_t kl = LOGEXP_TAB32[ k - 1 ];
1486 const int64_t kr = LOGEXP_TAB32[ k + 1 ];
1488 if( sy2 >= km + kr )
1500 ybits ^= 0x10000000000000;
1508 const int64_t tbits = 0x3ff0000000000000 | km;
1512 memcpy( &y, &ybits,
sizeof( y ));
1513 memcpy( &t, &tbits,
sizeof( t ));
1516 const double z = ( y - t ) / ( y + t );
1517 const double z2 = z * z;
1519 const double correction =
1520 z * ( 2.0 + z2 * ( 2.0 / 3.0 ));
1522 return(( e + k * 0.03125 ) * 0.69314718055994531
1537inline double pow_a(
const double v,
const double p )
1539#if defined( R8B_ARCH64 )
1540 return( exp_fast( p * log_fast( fabs( v ) + 1e-300 )));
1542 return( exp( p * log( fabs( v ) + 1e-300 )));
1556 const double ax = fabs( x );
1561 y = x * 2.6666666666666667e-1;
1566 return( ( 1.0000000116e+00 + z * 3.5156234257e+00 ) +
1567 y * ( ( 3.0899368029e+00 + z * 1.2067640177e+00 ) +
1568 y * ( ( 2.6596186713e-01 + z * 3.6073913744e-02 ) +
1569 y * 4.5857130152e-03 ) ) );
1577#if defined( R8B_ARCH64 )
1578 return( exp_fast( ax ) / sqrt( ax ) *
1580 return( exp( ax ) / sqrt( ax ) *
1582 ( ( 3.9894232649e-01 + y * 1.3293145671e-02 ) +
1583 z * ( ( 2.1133548948e-03 + y * -5.8935267150e-04 ) +
1584 z * ( ( 5.6653456233e-03 + y * -1.3682399954e-02 ) +
1585 z * ( ( 1.8689111903e-02 + y * -1.1978264040e-02 ) +
1586 z * 2.8410020052e-03 ) ) ) ) );
#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:225
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:1066
void calcSpline3p4Coeffs(double *const c, const double *const y)
Calculates 3rd order spline coefficients, using 4 points.
Definition r8bbase.h:1227
double pow_a(const double v, const double p)
Power of an absolute value.
Definition r8bbase.h:1537
R8B_CONST double R8B_PI
Equals pi.
Definition r8bbase.h:197
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:1009
R8B_CONST double R8B_3PI
Equals 3*pi.
Definition r8bbase.h:199
void calcSpline3p6Coeffs(double *const c, const double *const y)
Calculates 3rd order spline coefficients, using 6 points.
Definition r8bbase.h:1246
R8B_CONST double R8B_2PI
Equals 2*pi.
Definition r8bbase.h:198
R8B_CONST double R8B_PId2
Equals 0.5*pi.
Definition r8bbase.h:200
T min(const T &v1, const T &v2)
Returns minimum of two values.
Definition r8bbase.h:1269
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:1170
int getBitOccupancy(const int v)
Calculate the exact number of bits a value needs for representation.
Definition r8bbase.h:956
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:301
T max(const T &v1, const T &v2)
Returns maximum of two values.
Definition r8bbase.h:1288
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:1307
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:1204
T * align_ptr(T *const ptr, const uintptr_t align)
Forces the provided ptr pointer to be aligned to align bytes.
Definition r8bbase.h:285
double besselI0(const double x)
1st kind, 0th order modified Bessel function of a value.
Definition r8bbase.h:1554
void normalizeFIRFilter(double *const p, const int l, const double DCGain, const int pstep=1)
FIR filter's gain normalization.
Definition r8bbase.h:1124
double sqr(const double x)
Returns square ot a value.
Definition r8bbase.h:1330
The default base class for objects created on heap.
Definition r8bbase.h:237
The default base class for objects that allocate blocks of memory.
Definition r8bbase.h:247
static void * allocmem(const size_t Size)
Allocates a memory block.
Definition r8bbase.h:256
static void freemem(void *const p)
Frees a previously allocated memory block.
Definition r8bbase.h:267
Templated memory buffer class for element buffers of fixed capacity.
Definition r8bbase.h:326
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:366
void moveFrom(CFixedBuffer &s)
Moves buffer from another object to this object.
Definition r8bbase.h:419
void free()
Deallocates a previously allocated buffer.
Definition r8bbase.h:432
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:343
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:387
Pointer-to-object "keeper" class with automatic deletion.
Definition r8bbase.h:469
T * operator->() const
Returns pointer to keeped object, or nullptr, if no object is being kept.
Definition r8bbase.h:516
T * unkeep()
Returns the keeped pointer and resets it in this keeper without object deletion.
Definition r8bbase.h:547
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:505
CPtrKeeper(T2 const aObject)
Constructor assigns a pointer to object to this keeper.
Definition r8bbase.h:486
void reset()
Resets the keeped pointer and deletes the keeped object.
Definition r8bbase.h:535
Reference "keeper" class with automatic unref() call.
Definition r8bbase.h:569
T * operator->() const
Returns pointer to keeped object, or nullptr, if no object is being kept.
Definition r8bbase.h:619
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:608
CRefKeeper(T2 const aObject)
Constructor assigns a pointer to object to this keeper.
Definition r8bbase.h:586
void reset()
Resets the keeped pointer and unreferences the keeped object.
Definition r8bbase.h:638
Class for items of a singly-linked list.
Definition r8bbase.h:662
T * Next
Next object of type T in a singly-linked list.
Definition r8bbase.h:664
Multi-threaded synchronization object class.
Definition r8bbase.h:703
void acquire()
Acquires this thread synchronizer object immediately or waits until another thread releases it.
Definition r8bbase.h:734
void release()
Releases this, previously acquired, thread synchronizer object.
Definition r8bbase.h:748
A "keeper" class for CSyncObject-based synchronization.
Definition r8bbase.h:777
CSyncKeeper(CSyncObject &aSyncObj)
Constructor acquires a specified synchronization object.
Definition r8bbase.h:809
CSyncKeeper(CSyncObject *const aSyncObj)
Constructor acquires a specified synchronization object.
Definition r8bbase.h:793
CSineGen(const double si, const double ph)
Constructor initializes this sine signal generator, with unity gain output.
Definition r8bbase.h:871
CSineGen(const double si, const double ph, const double g)
Constructor initializes this sine signal generator.
Definition r8bbase.h:887
void init(const double si, const double ph, const double g)
Function initializes this sine signal generator.
Definition r8bbase.h:918
void init(const double si, const double ph)
Function initializes this sine signal generator, with unity gain output.
Definition r8bbase.h:902
double generate()
Generates the next sample.
Definition r8bbase.h:931