lmms_math.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * lmms_math.h - defines math functions
  3. *
  4. * Copyright (c) 2004-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef LMMS_MATH_H
  25. #define LMMS_MATH_H
  26. #include <stdint.h>
  27. #include "lmms_constants.h"
  28. #include "lmmsconfig.h"
  29. #include <QtCore/QtGlobal>
  30. #include <cmath>
  31. using namespace std;
  32. #ifndef __GLIBC__
  33. #ifndef isnanf
  34. #define isnanf(x) isnan(x)
  35. #endif
  36. #ifndef isinff
  37. #define isinff(x) isinf(x)
  38. #endif
  39. #ifndef _isnanf
  40. #define _isnanf(x) isnan(x)
  41. #endif
  42. #ifndef _isinff
  43. #define _isinff(x) isinf(x)
  44. #endif
  45. #ifndef exp10
  46. #define exp10(x) pow( 10.0, x )
  47. #endif
  48. #ifndef exp10f
  49. #define exp10f(x) powf( 10.0f, x )
  50. #endif
  51. #endif
  52. #ifdef __INTEL_COMPILER
  53. static inline float absFraction( const float _x )
  54. {
  55. return( _x - ( _x >= 0.0f ? floorf( _x ) : floorf( _x ) - 1 ) );
  56. }
  57. static inline float fraction( const float _x )
  58. {
  59. return( _x - floorf( _x ) );
  60. }
  61. #else
  62. static inline float absFraction( const float _x )
  63. {
  64. return( _x - ( _x >= 0.0f ? static_cast<int>( _x ) :
  65. static_cast<int>( _x ) - 1 ) );
  66. }
  67. static inline float fraction( const float _x )
  68. {
  69. return( _x - static_cast<int>( _x ) );
  70. }
  71. #if 0
  72. // SSE3-version
  73. static inline float absFraction( float _x )
  74. {
  75. unsigned int tmp;
  76. asm(
  77. "fld %%st\n\t"
  78. "fisttp %1\n\t"
  79. "fild %1\n\t"
  80. "ftst\n\t"
  81. "sahf\n\t"
  82. "jae 1f\n\t"
  83. "fld1\n\t"
  84. "fsubrp %%st, %%st(1)\n\t"
  85. "1:\n\t"
  86. "fsubrp %%st, %%st(1)"
  87. : "+t"( _x ), "=m"( tmp )
  88. :
  89. : "st(1)", "cc" );
  90. return( _x );
  91. }
  92. static inline float absFraction( float _x )
  93. {
  94. unsigned int tmp;
  95. asm(
  96. "fld %%st\n\t"
  97. "fisttp %1\n\t"
  98. "fild %1\n\t"
  99. "fsubrp %%st, %%st(1)"
  100. : "+t"( _x ), "=m"( tmp )
  101. :
  102. : "st(1)" );
  103. return( _x );
  104. }
  105. #endif
  106. #endif
  107. #define FAST_RAND_MAX 32767
  108. static inline int fast_rand()
  109. {
  110. static unsigned long next = 1;
  111. next = next * 1103515245 + 12345;
  112. return( (unsigned)( next / 65536 ) % 32768 );
  113. }
  114. static inline double fastRand( double range )
  115. {
  116. static const double fast_rand_ratio = 1.0 / FAST_RAND_MAX;
  117. return fast_rand() * range * fast_rand_ratio;
  118. }
  119. static inline float fastRandf( float range )
  120. {
  121. static const float fast_rand_ratio = 1.0f / FAST_RAND_MAX;
  122. return fast_rand() * range * fast_rand_ratio;
  123. }
  124. //! @brief Takes advantage of fmal() function if present in hardware
  125. static inline long double fastFmal( long double a, long double b, long double c )
  126. {
  127. #ifdef FP_FAST_FMAL
  128. #ifdef __clang__
  129. return fma( a, b, c );
  130. #else
  131. return fmal( a, b, c );
  132. #endif
  133. #else
  134. return a * b + c;
  135. #endif
  136. }
  137. //! @brief Takes advantage of fmaf() function if present in hardware
  138. static inline float fastFmaf( float a, float b, float c )
  139. {
  140. #ifdef FP_FAST_FMAF
  141. #ifdef __clang__
  142. return fma( a, b, c );
  143. #else
  144. return fmaf( a, b, c );
  145. #endif
  146. #else
  147. return a * b + c;
  148. #endif
  149. }
  150. //! @brief Takes advantage of fma() function if present in hardware
  151. static inline double fastFma( double a, double b, double c )
  152. {
  153. #ifdef FP_FAST_FMA
  154. return fma( a, b, c );
  155. #else
  156. return a * b + c;
  157. #endif
  158. }
  159. // source: http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/
  160. static inline double fastPow( double a, double b )
  161. {
  162. union
  163. {
  164. double d;
  165. int32_t x[2];
  166. } u = { a };
  167. u.x[1] = static_cast<int32_t>( b * ( u.x[1] - 1072632447 ) + 1072632447 );
  168. u.x[0] = 0;
  169. return u.d;
  170. }
  171. // sinc function
  172. static inline double sinc( double _x )
  173. {
  174. return _x == 0.0 ? 1.0 : sin( F_PI * _x ) / ( F_PI * _x );
  175. }
  176. //! @brief Exponential function that deals with negative bases
  177. static inline float signedPowf( float v, float e )
  178. {
  179. return v < 0
  180. ? powf( -v, e ) * -1.0f
  181. : powf( v, e );
  182. }
  183. //! @brief Scales @value from linear to logarithmic.
  184. //! Value should be within [0,1]
  185. static inline float logToLinearScale( float min, float max, float value )
  186. {
  187. if( min < 0 )
  188. {
  189. const float mmax = qMax( qAbs( min ), qAbs( max ) );
  190. const float val = value * ( max - min ) + min;
  191. float result = signedPowf( val / mmax, F_E ) * mmax;
  192. return isnan( result ) ? 0 : result;
  193. }
  194. float result = powf( value, F_E ) * ( max - min ) + min;
  195. return isnan( result ) ? 0 : result;
  196. }
  197. //! @brief Scales value from logarithmic to linear. Value should be in min-max range.
  198. static inline float linearToLogScale( float min, float max, float value )
  199. {
  200. static const float EXP = 1.0f / F_E;
  201. const float valueLimited = qBound( min, value, max);
  202. const float val = ( valueLimited - min ) / ( max - min );
  203. if( min < 0 )
  204. {
  205. const float mmax = qMax( qAbs( min ), qAbs( max ) );
  206. float result = signedPowf( valueLimited / mmax, EXP ) * mmax;
  207. return isnan( result ) ? 0 : result;
  208. }
  209. float result = powf( val, EXP ) * ( max - min ) + min;
  210. return isnan( result ) ? 0 : result;
  211. }
  212. //! @brief Converts linear amplitude (0-1.0) to dBFS scale. Handles zeroes as -inf.
  213. //! @param amp Linear amplitude, where 1.0 = 0dBFS.
  214. //! @return Amplitude in dBFS. -inf for 0 amplitude.
  215. static inline float safeAmpToDbfs( float amp )
  216. {
  217. return amp == 0.0f
  218. ? -INFINITY
  219. : log10f( amp ) * 20.0f;
  220. }
  221. //! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0. Handles infinity as zero.
  222. //! @param dbfs The dBFS value to convert: all infinites are treated as -inf and result in 0
  223. //! @return Linear amplitude
  224. static inline float safeDbfsToAmp( float dbfs )
  225. {
  226. return isinff( dbfs )
  227. ? 0.0f
  228. : exp10f( dbfs * 0.05f );
  229. }
  230. //! @brief Converts linear amplitude (>0-1.0) to dBFS scale.
  231. //! @param amp Linear amplitude, where 1.0 = 0dBFS. ** Must be larger than zero! **
  232. //! @return Amplitude in dBFS.
  233. static inline float ampToDbfs( float amp )
  234. {
  235. return log10f( amp ) * 20.0f;
  236. }
  237. //! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0
  238. //! @param dbfs The dBFS value to convert. ** Must be a real number - not inf/nan! **
  239. //! @return Linear amplitude
  240. static inline float dbfsToAmp( float dbfs )
  241. {
  242. return exp10f( dbfs * 0.05f );
  243. }
  244. //! returns 1.0f if val >= 0.0f, -1.0 else
  245. static inline float sign( float val )
  246. {
  247. return val >= 0.0f ? 1.0f : -1.0f;
  248. }
  249. //! if val >= 0.0f, returns sqrtf(val), else: -sqrtf(-val)
  250. static inline float sqrt_neg( float val )
  251. {
  252. return sqrtf( fabs( val ) ) * sign( val );
  253. }
  254. // fast approximation of square root
  255. static inline float fastSqrt( float n )
  256. {
  257. union
  258. {
  259. int32_t i;
  260. float f;
  261. } u;
  262. u.f = n;
  263. u.i = ( u.i + ( 127 << 23 ) ) >> 1;
  264. return u.f;
  265. }
  266. //! returns value furthest from zero
  267. template<class T>
  268. static inline T absMax( T a, T b )
  269. {
  270. return qAbs<T>(a) > qAbs<T>(b) ? a : b;
  271. }
  272. //! returns value nearest to zero
  273. template<class T>
  274. static inline T absMin( T a, T b )
  275. {
  276. return qAbs<T>(a) < qAbs<T>(b) ? a : b;
  277. }
  278. #endif