r8brain-free-src
High-quality pro audio sample rate converter library
 
Loading...
Searching...
No Matches
r8bbase.h
Go to the documentation of this file.
1//$ nobt
2
60
61#ifndef R8BBASE_INCLUDED
62#define R8BBASE_INCLUDED
63
64#define R8B_VERSION "7.7"
65
71
77
78#include <cstring>
79#include <cmath>
80#include <new>
81
82#if __cplusplus >= 201103L
83
84 #include <cstdint>
85 #include <mutex>
86
87 #define R8B_CONST constexpr
88 #define R8B_NULL nullptr
89
90#else // __cplusplus >= 201103L
91
92 #include <stdint.h> // A C99 fallback, as C++98 has no cstdint header.
93
94 #if defined( _WIN32 )
95 #include <Windows.h>
96 #else // defined( _WIN32 )
97 #include <pthread.h>
98 #endif // defined( _WIN32 )
99
100 #define R8B_CONST static const
101 #define R8B_NULL NULL
102
103#endif // __cplusplus >= 201103L
104
105#include "r8bconf.h"
106
107#if defined( __aarch64__ ) || defined( __arm64__ ) || \
108 defined( _M_ARM64 ) || defined( _M_ARM64EC )
109
110 #if defined( _MSC_VER )
111 #include <arm64_neon.h>
112 #else // defined( _MSC_VER )
113 #include <arm_neon.h>
114 #endif // defined( _MSC_VER )
115
116 #define R8B_NEON
117
118 #if !defined( __APPLE__ )
119 #define R8B_SIMD_ISH // Shuffled interpolation is inefficient on M1.
120 #endif // !defined( __APPLE__ )
121
122#elif defined( __SSE2__ ) || defined( _M_AMD64 ) || \
123 ( defined( _M_IX86_FP ) && _M_IX86_FP == 2 )
124
125 #if defined( _MSC_VER )
126 #include <intrin.h>
127 #else // defined( _MSC_VER )
128 #include <emmintrin.h>
129 #endif // defined( _MSC_VER )
130
131 #define R8B_SSE2
132 #define R8B_SIMD_ISH
133
134#endif // SSE2
135
140
141#if defined( __LP64__ ) || defined( _LP64 ) || \
142 defined( __x86_64__ ) || defined( __aarch64__ ) || \
143 defined( _M_AMD64 ) || defined( _M_ARM64 )
144
145 #define R8B_ARCH64
146
147#endif // 64-bit availability check
148
155
156#if defined( __clang__ )
157
158 #define R8B_EXITDTOR __attribute__((always_destroy))
159
160#else // defined( __clang__ )
161
162 #define R8B_EXITDTOR
163
164#endif // defined( __clang__ )
165
171
172namespace r8b {
173
174using std :: memcpy;
175using std :: memset;
176using std :: floor;
177using std :: ceil;
178using std :: fabs;
179using std :: sqrt;
180using std :: log;
181using std :: exp;
182using std :: pow;
183using std :: sin;
184using std :: cos;
185using std :: size_t;
186
187#if __cplusplus >= 201103L
188
189 #if defined( R8B_ARCH64 )
190 using std :: int64_t;
191 #endif // defined( R8B_ARCH64 )
192
193 using std :: uintptr_t;
194
195#endif // __cplusplus >= 201103L
196
197R8B_CONST double R8B_PI = 3.14159265358979324;
198R8B_CONST double R8B_2PI = 6.28318530717958648;
199R8B_CONST double R8B_3PI = 9.42477796076937972;
200R8B_CONST double R8B_PId2 = 1.57079632679489662;
201
215
216#if __cplusplus >= 201103L
217
218 #define R8BNOCTOR( ClassName ) \
219 public: \
220 ClassName( const ClassName& ) = delete; \
221 ClassName& operator = ( const ClassName& ) = delete;
222
223#else // __cplusplus >= 201103L
224
225 #define R8BNOCTOR( ClassName ) \
226 private: \
227 ClassName( const ClassName& ) { } \
228 ClassName& operator = ( const ClassName& ) { return( *this ); }
229
230#endif // __cplusplus >= 201103L
231
235
237{
238};
239
245
247{
248public:
255
256 static void* allocmem( const size_t Size )
257 {
258 return( new char[ Size ]);
259 }
260
266
267 static void freemem( void* const p )
268 {
269 delete[] (char*) p;
270 }
271};
272
283
284template< typename T >
285inline T* align_ptr( T* const ptr, const uintptr_t align )
286{
287 return( (T*) (( (uintptr_t) ptr + align - 1 ) & ~( align - 1 )));
288}
289
299
300template< typename T >
301inline T* construct_ptr( void* const ptr, const size_t c )
302{
303 return( :: new( ptr ) T[ c ]);
304}
305
323
324template< typename T >
325class CFixedBuffer : protected R8B_MEMALLOCCLASS
326{
327 R8BNOCTOR( CFixedBuffer )
328
329public:
330 CFixedBuffer()
331 : Data0( R8B_NULL )
332 , Data( R8B_NULL )
333 {
334 }
335
342
343 CFixedBuffer( const int Capacity )
344 {
345 R8BASSERT( Capacity >= 0 );
346
347 Data0 = allocmem( (size_t) Capacity * sizeof( T ) + Alignment );
348 Data = construct_ptr< T >( align_ptr( Data0, Alignment ),
349 (size_t) Capacity );
350
351 R8BASSERT( Data0 != R8B_NULL || Capacity == 0 );
352 }
353
355 {
356 freemem( Data0 );
357 }
358
365
366 void alloc( const int Capacity )
367 {
368 R8BASSERT( Capacity >= 0 );
369
370 freemem( Data0 );
371 Data0 = allocmem( (size_t) Capacity * sizeof( T ) + Alignment );
372 Data = construct_ptr< T >( align_ptr( Data0, Alignment ),
373 (size_t) Capacity );
374
375 R8BASSERT( Data0 != R8B_NULL || Capacity == 0 );
376 }
377
386
387 void realloc( const int PrevCapacity, const int NewCapacity )
388 {
389 R8BASSERT( PrevCapacity >= 0 );
390 R8BASSERT( NewCapacity >= 0 );
391
392 void* const NewData0 = allocmem( (size_t) NewCapacity * sizeof( T ) +
393 Alignment );
394
395 T* const NewData = construct_ptr< T >( align_ptr( NewData0,
396 Alignment ), (size_t) NewCapacity );
397
398 const size_t CopySize = ( PrevCapacity > NewCapacity ?
399 (size_t) NewCapacity : (size_t) PrevCapacity ) * sizeof( T );
400
401 if( CopySize > 0 )
402 {
403 memcpy( NewData, Data, CopySize );
404 }
405
406 freemem( Data0 );
407 Data0 = NewData0;
408 Data = NewData;
409
410 R8BASSERT( Data0 != R8B_NULL || NewCapacity == 0 );
411 }
412
418
419 void moveFrom( CFixedBuffer& s )
420 {
421 freemem( Data0 );
422 Data0 = s.Data0;
423 Data = s.Data;
424 s.Data0 = R8B_NULL;
425 s.Data = R8B_NULL;
426 }
427
431
432 void free()
433 {
434 freemem( Data0 );
435 Data0 = R8B_NULL;
436 Data = R8B_NULL;
437 }
438
443
444 operator T* () const
445 {
446 return( Data );
447 }
448
449private:
450 static const size_t Alignment = 64;
452 void* Data0;
453 T* Data;
454};
455
466
467template< typename T >
468class CPtrKeeper
469{
470 R8BNOCTOR( CPtrKeeper )
471
472public:
473 CPtrKeeper()
474 : Object( R8B_NULL )
475 {
476 }
477
484
485 template< typename T2 >
486 CPtrKeeper( T2 const aObject )
487 : Object( aObject )
488 {
489 }
490
492 {
493 delete Object;
494 }
495
503
504 template< typename T2 >
505 void operator = ( T2 const aObject )
506 {
507 reset();
508 Object = aObject;
509 }
510
515
516 T* operator -> () const
517 {
518 return( Object );
519 }
520
525
526 operator T* () const
527 {
528 return( Object );
529 }
530
534
535 void reset()
536 {
537 T* const DelObj = Object;
538 Object = R8B_NULL;
539 delete DelObj;
540 }
541
546
548 {
549 T* const ResObject = Object;
550 Object = R8B_NULL;
551 return( ResObject );
552 }
553
554private:
555 T* Object;
556};
557
566
567template< typename T >
568class CRefKeeper
569{
570 R8BNOCTOR( CRefKeeper )
571
572public:
573 CRefKeeper()
574 : Object( R8B_NULL )
575 {
576 }
577
584
585 template< typename T2 >
586 CRefKeeper( T2 const aObject )
587 : Object( aObject )
588 {
589 }
590
592 {
593 if( Object != R8B_NULL )
594 {
595 Object -> unref();
596 }
597 }
598
606
607 template< typename T2 >
608 void operator = ( T2 const aObject )
609 {
610 reset();
611 Object = aObject;
612 }
613
618
619 T* operator -> () const
620 {
621 return( Object );
622 }
623
628
629 operator T* () const
630 {
631 return( Object );
632 }
633
637
638 void reset()
639 {
640 if( Object != R8B_NULL )
641 {
642 T* const RefObj = Object;
643 Object = R8B_NULL;
644 RefObj -> unref();
645 }
646 }
647
648private:
649 T* Object;
650};
651
659
660template< class T >
661class CSinglyLinkedListItem
662{
663public:
664 T* Next;
665
666protected:
667 CSinglyLinkedListItem()
668 : Next( R8B_NULL )
669 {
670 }
671
673 {
674 while( Next != R8B_NULL )
675 {
676 T* const nn = Next -> Next;
677 Next -> Next = R8B_NULL;
678 delete Next;
679 Next = nn;
680 }
681 }
682};
683
684#if __cplusplus >= 201103L
685
686typedef std :: mutex CSyncObject;
687typedef std :: lock_guard< std :: mutex > CSyncKeeper;
688
689#else // __cplusplus >= 201103L
690
701
702class CSyncObject
703{
704 R8BNOCTOR( CSyncObject )
705
706public:
707 CSyncObject()
708 {
709 #if defined( _WIN32 )
710 InitializeCriticalSectionAndSpinCount( &CritSec, 2000 );
711 #else // defined( _WIN32 )
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 );
717 #endif // defined( _WIN32 )
718 }
719
720 ~CSyncObject()
721 {
722 #if defined( _WIN32 )
723 DeleteCriticalSection( &CritSec );
724 #else // defined( _WIN32 )
725 pthread_mutex_destroy( &Mutex );
726 #endif // defined( _WIN32 )
727 }
728
733
734 void acquire()
735 {
736 #if defined( _WIN32 )
737 EnterCriticalSection( &CritSec );
738 #else // defined( _WIN32 )
739 pthread_mutex_lock( &Mutex );
740 #endif // defined( _WIN32 )
741 }
742
747
748 void release()
749 {
750 #if defined( _WIN32 )
751 LeaveCriticalSection( &CritSec );
752 #else // defined( _WIN32 )
753 pthread_mutex_unlock( &Mutex );
754 #endif // defined( _WIN32 )
755 }
756
757private:
758 #if defined( _WIN32 )
759 CRITICAL_SECTION CritSec;
761 #else // defined( _WIN32 )
762 pthread_mutex_t Mutex;
763 #endif // defined( _WIN32 )
764};
765
775
776class CSyncKeeper
777{
778 R8BNOCTOR( CSyncKeeper )
779
780public:
781 CSyncKeeper()
782 : SyncObj( R8B_NULL )
783 {
784 }
785
792
793 CSyncKeeper( CSyncObject* const aSyncObj )
794 : SyncObj( aSyncObj )
795 {
796 if( SyncObj != R8B_NULL )
797 {
798 SyncObj -> acquire();
799 }
800 }
801
808
810 : SyncObj( &aSyncObj )
811 {
812 SyncObj -> acquire();
813 }
814
816 {
817 if( SyncObj != R8B_NULL )
818 {
819 SyncObj -> release();
820 }
821 }
822
823private:
824 CSyncObject* SyncObj;
825};
826
827#endif // __cplusplus >= 201103L
828
844
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 )
849
855
856class CSineGen
857{
858public:
859 CSineGen()
860 {
861 }
862
870
871 CSineGen( const double si, const double ph )
872 : svalue1( sin( ph ))
873 , svalue2( sin( ph - si ))
874 , sincr( 2.0 * cos( si ))
875 {
876 }
877
886
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 ))
891 {
892 }
893
901
902 void init( const double si, const double ph )
903 {
904 svalue1 = sin( ph );
905 svalue2 = sin( ph - si );
906 sincr = 2.0 * cos( si );
907 }
908
917
918 void init( const double si, const double ph, const double g )
919 {
920 svalue1 = sin( ph ) * g;
921 svalue2 = sin( ph - si ) * g;
922 sincr = 2.0 * cos( si );
923 }
924
930
931 double generate()
932 {
933 const double res = svalue1;
934
935 svalue1 = sincr * res - svalue2;
936 svalue2 = res;
937
938 return( res );
939 }
940
941private:
942 double svalue1;
943 double svalue2;
944 double sincr;
945};
946
955
956inline int getBitOccupancy( const int v )
957{
958 static const unsigned char OccupancyTable[] =
959 {
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
976 };
977
978 const int tt = v >> 16;
979
980 if( tt != 0 )
981 {
982 const int t = v >> 24;
983
984 return( t != 0 ? 24 + OccupancyTable[ t & 0xFF ] :
985 16 + OccupancyTable[ tt ]);
986 }
987 else
988 {
989 const int t = v >> 8;
990
991 return( t != 0 ? 8 + OccupancyTable[ t ] : OccupancyTable[ v ]);
992 }
993}
994
1008
1009inline void calcFIRFilterResponse( const double* flt, int fltlen,
1010 const double th, double& re0, double& im0, const int fltlat = 0 )
1011{
1012 const double sincr = 2.0 * cos( th );
1013 double cvalue1;
1014 double svalue1;
1015
1016 if( fltlat == 0 )
1017 {
1018 cvalue1 = 1.0;
1019 svalue1 = 0.0;
1020 }
1021 else
1022 {
1023 cvalue1 = cos( -fltlat * th );
1024 svalue1 = sin( -fltlat * th );
1025 }
1026
1027 double cvalue2 = cos( -( fltlat + 1 ) * th );
1028 double svalue2 = sin( -( fltlat + 1 ) * th );
1029
1030 double re = 0.0;
1031 double im = 0.0;
1032
1033 while( fltlen > 0 )
1034 {
1035 re += cvalue1 * flt[ 0 ];
1036 im += svalue1 * flt[ 0 ];
1037 flt++;
1038 fltlen--;
1039
1040 double tmp = cvalue1;
1041 cvalue1 = sincr * cvalue1 - cvalue2;
1042 cvalue2 = tmp;
1043
1044 tmp = svalue1;
1045 svalue1 = sincr * svalue1 - svalue2;
1046 svalue2 = tmp;
1047 }
1048
1049 re0 = re;
1050 im0 = im;
1051}
1052
1065
1066inline double calcFIRFilterGroupDelay( const double* const flt,
1067 const int fltlen, const double th )
1068{
1069 const int Count = 2;
1070 const double thd2 = 1e-9;
1071 double ths[ Count ] = { th - thd2, th + thd2 }; // Side-band frequencies.
1072
1073 if( ths[ 0 ] < 0.0 )
1074 {
1075 ths[ 0 ] = 0.0;
1076 }
1077
1078 if( ths[ 1 ] > R8B_PI )
1079 {
1080 ths[ 1 ] = R8B_PI;
1081 }
1082
1083 double ph1[ Count ];
1084 int i;
1085
1086 for( i = 0; i < Count; i++ )
1087 {
1088 double re1;
1089 double im1;
1090
1091 calcFIRFilterResponse( flt, fltlen, ths[ i ], re1, im1 );
1092 ph1[ i ] = atan2( im1, re1 );
1093 }
1094
1095 if( fabs( ph1[ 1 ] - ph1[ 0 ]) > R8B_PI )
1096 {
1097 if( ph1[ 1 ] > ph1[ 0 ])
1098 {
1099 ph1[ 1 ] -= R8B_2PI;
1100 }
1101 else
1102 {
1103 ph1[ 1 ] += R8B_2PI;
1104 }
1105 }
1106
1107 const double thd = ths[ 1 ] - ths[ 0 ];
1108
1109 return(( ph1[ 1 ] - ph1[ 0 ]) / thd );
1110}
1111
1123
1124inline void normalizeFIRFilter( double* const p, const int l,
1125 const double DCGain, const int pstep = 1 )
1126{
1127 R8BASSERT( l > 0 );
1128 R8BASSERT( pstep != 0 );
1129
1130 double s = 0.0;
1131 double* pp = p;
1132 int i = l;
1133
1134 while( i > 0 )
1135 {
1136 s += *pp;
1137 pp += pstep;
1138 i--;
1139 }
1140
1141 s = DCGain / s;
1142 pp = p;
1143 i = l;
1144
1145 while( i > 0 )
1146 {
1147 *pp *= s;
1148 pp += pstep;
1149 i--;
1150 }
1151}
1152
1169
1170inline void calcSpline3p8Coeffs( double* const c, const double xm3,
1171 const double xm2, const double xm1, const double x0, const double x1,
1172 const double x2, const double x3, const double x4 )
1173{
1174 c[ 0 ] = x0;
1175 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1176 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1177
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;
1180
1181 c[ 3 ] = ( 91.0 * ( x0 - x1 ) + 45.0 * ( x2 - xm1 ) +
1182 13.0 * ( xm2 - x3 ) + 3.0 * ( x4 - xm3 )) * 1.31578947368421052e-2;
1183}
1184
1203
1204inline void calcSpline2p8Coeffs( double* const c, const double xm3,
1205 const double xm2, const double xm1, const double x0, const double x1,
1206 const double x2, const double x3, const double x4 )
1207{
1208 c[ 0 ] = x0;
1209 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1210 3.0 * ( x3 - xm3 )) * 1.31578947368421052e-2;
1211
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;
1214}
1215
1226
1227inline void calcSpline3p4Coeffs( double* const c, const double* const y )
1228{
1229 c[ 0 ] = y[ 1 ];
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 ]);
1233}
1234
1245
1246inline void calcSpline3p6Coeffs( double* const c, const double* const y )
1247{
1248 c[ 0 ] = 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;
1252
1253 c[ 3 ] = ( 17.0 * ( y[ 2 ] - y[ 3 ]) + 9.0 * ( y[ 4 ] - y[ 1 ]) +
1254 2.0 * ( y[ 0 ] - y[ 5 ])) / 14.0;
1255}
1256
1257#if !defined( min )
1258
1267
1268template< typename T >
1269inline T min( const T& v1, const T& v2 )
1270{
1271 return( v1 < v2 ? v1 : v2 );
1272}
1273
1274#endif // !defined( min )
1275
1276#if !defined( max )
1277
1286
1287template< typename T >
1288inline T max( const T& v1, const T& v2 )
1289{
1290 return( v1 > v2 ? v1 : v2 );
1291}
1292
1293#endif // !defined( max )
1294
1306
1307inline double clampr( const double Value, const double minv,
1308 const double maxv )
1309{
1310 if( Value < minv )
1311 {
1312 return( minv );
1313 }
1314
1315 if( Value > maxv )
1316 {
1317 return( maxv );
1318 }
1319
1320 return( Value );
1321}
1322
1329
1330inline double sqr( const double x )
1331{
1332 return( x * x );
1333}
1334
1335#if defined( R8B_ARCH64 )
1336
1341
1342R8B_CONST int64_t LOGEXP_TAB32[ 32 + 1 ] = { // Mantissa bits of 2^(j/32).
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,
1351 0x10000000000000 };
1352
1363
1364inline double exp_fast( const double x )
1365{
1366 const double LOG2E_32 = 46.166241308446828; // 32*log2(e)
1367 const double y = x * LOG2E_32 + 32736.0; // 32*log2(e)*x + 32*1023
1368
1369 if( !( y >= 32.0 ) )
1370 return( y != y ? x : 0.0 );
1371 if( y >= 65504.0 )
1372 return( HUGE_VAL );
1373
1374 int64_t n = (int) y;
1375 const double f = y - n;
1376
1377 const int64_t m = LOGEXP_TAB32[ n & 31 ];
1378 n = ( n >> 5 ) << 52;
1379 n |= m;
1380
1381 double s;
1382 memcpy( &s, &n, sizeof( s ));
1383
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 ) ) );
1388
1389 return( p * s );
1390}
1391
1407
1408inline double log_fast( double x )
1409{
1410 int64_t ix;
1411 memcpy( &ix, &x, sizeof( ix ));
1412
1413 if( !( x > 0.0 ) )
1414 {
1415 if( x != x )
1416 return x; // NaN
1417
1418 int64_t bits;
1419
1420 if( x == 0.0 )
1421 {
1422 // -Inf without an out-of-range signed literal.
1423 bits = -0x0010000000000000;
1424 }
1425 else
1426 {
1427 bits = 0x7ff8000000000000; // NaN
1428 }
1429
1430 double r;
1431 memcpy( &r, &bits, sizeof( r ));
1432
1433 return( r );
1434 }
1435
1436 int e = (int) ( ix >> 52 );
1437
1438 // Normalize subnormals exactly.
1439 if( e == 0 )
1440 {
1441 x *= 9007199254740992.0; // 2^53
1442 memcpy( &ix, &x, sizeof( ix ));
1443 e = (int) (( ix >> 52 ) - 1076 );
1444 }
1445 else
1446 {
1447 if( e == 0x7ff )
1448 return x; // +Inf
1449
1450 e -= 1023;
1451 }
1452
1453 // Mantissa bits of y = x/2^e. The constant 2^52 implicit significand
1454 // component cancels from both sides of every midpoint comparison.
1455 const int64_t sy = ix & 0x000fffffffffffff;
1456
1457 // For each equal-width significand bucket j, this is the nearest
1458 // LOG_TAB32 node at the bucket's center:
1459 //
1460 // round(32*log2(1 + (j + 0.5)/32))
1461 //
1462 // The last value is clamped to 31; index 32 is represented specially
1463 // by incrementing the exponent.
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
1469 };
1470
1471 int k = LOG_CENTER32[ sy >> 47 ]; // Always 1..31.
1472 const int64_t sy2 = sy << 1;
1473 int64_t km = LOGEXP_TAB32[ k ];
1474
1475 // Bit pattern is prepared now, but `y` is not materialized yet.
1476 int64_t ybits = 0x3ff0000000000000 | sy;
1477 const int64_t kl = LOGEXP_TAB32[ k - 1 ];
1478
1479 if( sy2 < kl + km )
1480 {
1481 --k;
1482 km = kl;
1483 }
1484 else
1485 {
1486 const int64_t kr = LOGEXP_TAB32[ k + 1 ];
1487
1488 if( sy2 >= km + kr )
1489 {
1490 ++k;
1491 km = kr;
1492
1493 // Normalize node 2.
1494
1495 if( k == 32 )
1496 {
1497 ++e;
1498
1499 // Decrement the biased exponent: this halves y exactly.
1500 ybits ^= 0x10000000000000;
1501 km = 0;
1502 k = 0;
1503 }
1504 }
1505 }
1506
1507 // `km` is the final mantissa field: LOG_TAB32[ k ], or zero for 1.0.
1508 const int64_t tbits = 0x3ff0000000000000 | km;
1509
1510 // Materialize `y` and `t` only after all selections are complete.
1511 double y, t;
1512 memcpy( &y, &ybits, sizeof( y ));
1513 memcpy( &t, &tbits, sizeof( t ));
1514
1515 // Cubic atanh approximation: ln(y/t) = 2*z + 2/3*z^3 + O(z^5).
1516 const double z = ( y - t ) / ( y + t );
1517 const double z2 = z * z;
1518
1519 const double correction =
1520 z * ( 2.0 + z2 * ( 2.0 / 3.0 ));
1521
1522 return(( e + k * 0.03125 ) * 0.69314718055994531
1523 + correction );
1524}
1525
1526#endif // defined( R8B_ARCH64 )
1527
1536
1537inline double pow_a( const double v, const double p )
1538{
1539#if defined( R8B_ARCH64 )
1540 return( exp_fast( p * log_fast( fabs( v ) + 1e-300 )));
1541#else // defined( R8B_ARCH64 )
1542 return( exp( p * log( fabs( v ) + 1e-300 )));
1543#endif // defined( R8B_ARCH64 )
1544}
1545
1553
1554inline double besselI0( const double x )
1555{
1556 const double ax = fabs( x );
1557 double y, z;
1558
1559 if( ax < 3.75 )
1560 {
1561 y = x * 2.6666666666666667e-1; // 1/3.75
1562 z = y * y; // z = (x/3.75)^2
1563 y = z * z; // y = z^2
1564
1565 // Estrin: (c0 + c1 z) + y (c2 + c3 z) + y^2 (c4 + c5 z) + y^3 c6
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 ) ) );
1570 }
1571
1572 y = 3.75 / ax;
1573 z = y * y;
1574
1575 // Estrin: (b0 + b1 y) + z (b2 + b3 y) + ... + z^4 b8
1576
1577#if defined( R8B_ARCH64 )
1578 return( exp_fast( ax ) / sqrt( ax ) *
1579#else // defined( R8B_ARCH64 )
1580 return( exp( ax ) / sqrt( ax ) *
1581#endif // defined( R8B_ARCH64 )
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 ) ) ) ) );
1587}
1588
1589} // namespace r8b
1590
1591#endif // R8BBASE_INCLUDED
#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