lmms_math.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <cstdint>
  27. #include "lmms_constants.h"
  28. #include "lmmsconfig.h"
  29. #include <QtCore/QtGlobal>
  30. #include <cmath>
  31. using namespace std;
  32. #ifndef exp10
  33. #define exp10(x) std::pow( 10.0, x )
  34. #endif
  35. #ifdef __INTEL_COMPILER
  36. static inline float absFraction( const float _x )
  37. {
  38. return( _x - ( _x >= 0.0f ? floorf( _x ) : floorf( _x ) - 1 ) );
  39. }
  40. static inline float fraction( const float _x )
  41. {
  42. return( _x - floorf( _x ) );
  43. }
  44. #else
  45. static inline float absFraction( const float _x )
  46. {
  47. return( _x - ( _x >= 0.0f ? static_cast<int>( _x ) :
  48. static_cast<int>( _x ) - 1 ) );
  49. }
  50. static inline float fraction( const float _x )
  51. {
  52. return( _x - static_cast<int>( _x ) );
  53. }
  54. #if 0
  55. // SSE3-version
  56. static inline float absFraction( float _x )
  57. {
  58. unsigned int tmp;
  59. asm(
  60. "fld %%st\n\t"
  61. "fisttp %1\n\t"
  62. "fild %1\n\t"
  63. "ftst\n\t"
  64. "sahf\n\t"
  65. "jae 1f\n\t"
  66. "fld1\n\t"
  67. "fsubrp %%st, %%st(1)\n\t"
  68. "1:\n\t"
  69. "fsubrp %%st, %%st(1)"
  70. : "+t"( _x ), "=m"( tmp )
  71. :
  72. : "st(1)", "cc" );
  73. return( _x );
  74. }
  75. static inline float absFraction( float _x )
  76. {
  77. unsigned int tmp;
  78. asm(
  79. "fld %%st\n\t"
  80. "fisttp %1\n\t"
  81. "fild %1\n\t"
  82. "fsubrp %%st, %%st(1)"
  83. : "+t"( _x ), "=m"( tmp )
  84. :
  85. : "st(1)" );
  86. return( _x );
  87. }
  88. #endif
  89. #endif
  90. #define FAST_RAND_MAX 32767
  91. static inline int fast_rand()
  92. {
  93. static unsigned long next = 1;
  94. next = next * 1103515245 + 12345;
  95. return( (unsigned)( next / 65536 ) % 32768 );
  96. }
  97. static inline double fastRand( double range )
  98. {
  99. static const double fast_rand_ratio = 1.0 / FAST_RAND_MAX;
  100. return fast_rand() * range * fast_rand_ratio;
  101. }
  102. static inline float fastRandf( float range )
  103. {
  104. static const float fast_rand_ratio = 1.0f / FAST_RAND_MAX;
  105. return fast_rand() * range * fast_rand_ratio;
  106. }
  107. //! @brief Takes advantage of fmal() function if present in hardware
  108. static inline long double fastFmal( long double a, long double b, long double c )
  109. {
  110. #ifdef FP_FAST_FMAL
  111. #ifdef __clang__
  112. return fma( a, b, c );
  113. #else
  114. return fmal( a, b, c );
  115. #endif
  116. #else
  117. return a * b + c;
  118. #endif
  119. }
  120. //! @brief Takes advantage of fmaf() function if present in hardware
  121. static inline float fastFmaf( float a, float b, float c )
  122. {
  123. #ifdef FP_FAST_FMAF
  124. #ifdef __clang__
  125. return fma( a, b, c );
  126. #else
  127. return fmaf( a, b, c );
  128. #endif
  129. #else
  130. return a * b + c;
  131. #endif
  132. }
  133. //! @brief Takes advantage of fma() function if present in hardware
  134. static inline double fastFma( double a, double b, double c )
  135. {
  136. #ifdef FP_FAST_FMA
  137. return fma( a, b, c );
  138. #else
  139. return a * b + c;
  140. #endif
  141. }
  142. // source: http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/
  143. static inline double fastPow( double a, double b )
  144. {
  145. union
  146. {
  147. double d;
  148. int32_t x[2];
  149. } u = { a };
  150. u.x[1] = static_cast<int32_t>( b * ( u.x[1] - 1072632447 ) + 1072632447 );
  151. u.x[0] = 0;
  152. return u.d;
  153. }
  154. // sinc function
  155. static inline double sinc( double _x )
  156. {
  157. return _x == 0.0 ? 1.0 : sin( F_PI * _x ) / ( F_PI * _x );
  158. }
  159. //! @brief Exponential function that deals with negative bases
  160. static inline float signedPowf( float v, float e )
  161. {
  162. return v < 0
  163. ? powf( -v, e ) * -1.0f
  164. : powf( v, e );
  165. }
  166. //! @brief Scales @value from linear to logarithmic.
  167. //! Value should be within [0,1]
  168. static inline float logToLinearScale( float min, float max, float value )
  169. {
  170. if( min < 0 )
  171. {
  172. const float mmax = qMax( qAbs( min ), qAbs( max ) );
  173. const float val = value * ( max - min ) + min;
  174. float result = signedPowf( val / mmax, F_E ) * mmax;
  175. return isnan( result ) ? 0 : result;
  176. }
  177. float result = powf( value, F_E ) * ( max - min ) + min;
  178. return isnan( result ) ? 0 : result;
  179. }
  180. //! @brief Scales value from logarithmic to linear. Value should be in min-max range.
  181. static inline float linearToLogScale( float min, float max, float value )
  182. {
  183. static const float EXP = 1.0f / F_E;
  184. const float valueLimited = qBound( min, value, max);
  185. const float val = ( valueLimited - min ) / ( max - min );
  186. if( min < 0 )
  187. {
  188. const float mmax = qMax( qAbs( min ), qAbs( max ) );
  189. float result = signedPowf( valueLimited / mmax, EXP ) * mmax;
  190. return isnan( result ) ? 0 : result;
  191. }
  192. float result = powf( val, EXP ) * ( max - min ) + min;
  193. return isnan( result ) ? 0 : result;
  194. }
  195. //! @brief Converts linear amplitude (0-1.0) to dBFS scale. Handles zeroes as -inf.
  196. //! @param amp Linear amplitude, where 1.0 = 0dBFS.
  197. //! @return Amplitude in dBFS. -inf for 0 amplitude.
  198. static inline float safeAmpToDbfs( float amp )
  199. {
  200. return amp == 0.0f
  201. ? -INFINITY
  202. : log10f( amp ) * 20.0f;
  203. }
  204. //! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0. Handles infinity as zero.
  205. //! @param dbfs The dBFS value to convert: all infinites are treated as -inf and result in 0
  206. //! @return Linear amplitude
  207. static inline float safeDbfsToAmp( float dbfs )
  208. {
  209. return isinf( dbfs )
  210. ? 0.0f
  211. : exp10( dbfs * 0.05f );
  212. }
  213. //! @brief Converts linear amplitude (>0-1.0) to dBFS scale.
  214. //! @param amp Linear amplitude, where 1.0 = 0dBFS. ** Must be larger than zero! **
  215. //! @return Amplitude in dBFS.
  216. static inline float ampToDbfs( float amp )
  217. {
  218. return log10f( amp ) * 20.0f;
  219. }
  220. //! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0
  221. //! @param dbfs The dBFS value to convert. ** Must be a real number - not inf/nan! **
  222. //! @return Linear amplitude
  223. static inline float dbfsToAmp( float dbfs )
  224. {
  225. return exp10( dbfs * 0.05f );
  226. }
  227. //! returns 1.0f if val >= 0.0f, -1.0 else
  228. static inline float sign( float val )
  229. {
  230. return val >= 0.0f ? 1.0f : -1.0f;
  231. }
  232. //! if val >= 0.0f, returns sqrtf(val), else: -sqrtf(-val)
  233. static inline float sqrt_neg( float val )
  234. {
  235. return sqrtf( fabs( val ) ) * sign( val );
  236. }
  237. // fast approximation of square root
  238. static inline float fastSqrt( float n )
  239. {
  240. union
  241. {
  242. int32_t i;
  243. float f;
  244. } u;
  245. u.f = n;
  246. u.i = ( u.i + ( 127 << 23 ) ) >> 1;
  247. return u.f;
  248. }
  249. //! returns value furthest from zero
  250. template<class T>
  251. static inline T absMax( T a, T b )
  252. {
  253. return qAbs<T>(a) > qAbs<T>(b) ? a : b;
  254. }
  255. //! returns value nearest to zero
  256. template<class T>
  257. static inline T absMin( T a, T b )
  258. {
  259. return qAbs<T>(a) < qAbs<T>(b) ? a : b;
  260. }
  261. #endif