lmms_math.h 7.5 KB

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