The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /**
  24. This class contains various fast mathematical function approximations.
  25. @tags{DSP}
  26. */
  27. struct FastMathApproximations
  28. {
  29. /** Provides a fast approximation of the function cosh(x) using a Pade approximant
  30. continued fraction, calculated sample by sample.
  31. Note : this is an approximation which works on a limited range. You are
  32. advised to use input values only between -5 and +5 for limiting the error.
  33. */
  34. template <typename FloatType>
  35. static FloatType cosh (FloatType x) noexcept
  36. {
  37. auto x2 = x * x;
  38. auto numerator = -(39251520 + x2 * (18471600 + x2 * (1075032 + 14615 * x2)));
  39. auto denominator = -39251520 + x2 * (1154160 + x2 * (-16632 + 127 * x2));
  40. return numerator / denominator;
  41. }
  42. /** Provides a fast approximation of the function cosh(x) using a Pade approximant
  43. continued fraction, calculated on a whole buffer.
  44. Note : this is an approximation which works on a limited range. You are
  45. advised to use input values only between -5 and +5 for limiting the error.
  46. */
  47. template <typename FloatType>
  48. static void cosh (FloatType* values, size_t numValues) noexcept
  49. {
  50. for (size_t i = 0; i < numValues; ++i)
  51. values[i] = FastMathApproximations::cosh (values[i]);
  52. }
  53. /** Provides a fast approximation of the function sinh(x) using a Pade approximant
  54. continued fraction, calculated sample by sample.
  55. Note : this is an approximation which works on a limited range. You are
  56. advised to use input values only between -5 and +5 for limiting the error.
  57. */
  58. template <typename FloatType>
  59. static FloatType sinh (FloatType x) noexcept
  60. {
  61. auto x2 = x * x;
  62. auto numerator = -x * (11511339840 + x2 * (1640635920 + x2 * (52785432 + x2 * 479249)));
  63. auto denominator = -11511339840 + x2 * (277920720 + x2 * (-3177720 + x2 * 18361));
  64. return numerator / denominator;
  65. }
  66. /** Provides a fast approximation of the function sinh(x) using a Pade approximant
  67. continued fraction, calculated on a whole buffer.
  68. Note : this is an approximation which works on a limited range. You are
  69. advised to use input values only between -5 and +5 for limiting the error.
  70. */
  71. template <typename FloatType>
  72. static void sinh (FloatType* values, size_t numValues) noexcept
  73. {
  74. for (size_t i = 0; i < numValues; ++i)
  75. values[i] = FastMathApproximations::sinh (values[i]);
  76. }
  77. /** Provides a fast approximation of the function tanh(x) using a Pade approximant
  78. continued fraction, calculated sample by sample.
  79. Note : this is an approximation which works on a limited range. You are
  80. advised to use input values only between -5 and +5 for limiting the error.
  81. */
  82. template <typename FloatType>
  83. static FloatType tanh (FloatType x) noexcept
  84. {
  85. auto x2 = x * x;
  86. auto numerator = x * (135135 + x2 * (17325 + x2 * (378 + x2)));
  87. auto denominator = 135135 + x2 * (62370 + x2 * (3150 + 28 * x2));
  88. return numerator / denominator;
  89. }
  90. /** Provides a fast approximation of the function tanh(x) using a Pade approximant
  91. continued fraction, calculated on a whole buffer.
  92. Note : this is an approximation which works on a limited range. You are
  93. advised to use input values only between -5 and +5 for limiting the error.
  94. */
  95. template <typename FloatType>
  96. static void tanh (FloatType* values, size_t numValues) noexcept
  97. {
  98. for (size_t i = 0; i < numValues; ++i)
  99. values[i] = FastMathApproximations::tanh (values[i]);
  100. }
  101. //==============================================================================
  102. /** Provides a fast approximation of the function cos(x) using a Pade approximant
  103. continued fraction, calculated sample by sample.
  104. Note : this is an approximation which works on a limited range. You are
  105. advised to use input values only between -pi and +pi for limiting the error.
  106. */
  107. template <typename FloatType>
  108. static FloatType cos (FloatType x) noexcept
  109. {
  110. auto x2 = x * x;
  111. auto numerator = -(-39251520 + x2 * (18471600 + x2 * (-1075032 + 14615 * x2)));
  112. auto denominator = 39251520 + x2 * (1154160 + x2 * (16632 + x2 * 127));
  113. return numerator / denominator;
  114. }
  115. /** Provides a fast approximation of the function cos(x) using a Pade approximant
  116. continued fraction, calculated on a whole buffer.
  117. Note : this is an approximation which works on a limited range. You are
  118. advised to use input values only between -pi and +pi for limiting the error.
  119. */
  120. template <typename FloatType>
  121. static void cos (FloatType* values, size_t numValues) noexcept
  122. {
  123. for (size_t i = 0; i < numValues; ++i)
  124. values[i] = FastMathApproximations::cos (values[i]);
  125. }
  126. /** Provides a fast approximation of the function sin(x) using a Pade approximant
  127. continued fraction, calculated sample by sample.
  128. Note : this is an approximation which works on a limited range. You are
  129. advised to use input values only between -pi and +pi for limiting the error.
  130. */
  131. template <typename FloatType>
  132. static FloatType sin (FloatType x) noexcept
  133. {
  134. auto x2 = x * x;
  135. auto numerator = -x * (-11511339840 + x2 * (1640635920 + x2 * (-52785432 + x2 * 479249)));
  136. auto denominator = 11511339840 + x2 * (277920720 + x2 * (3177720 + x2 * 18361));
  137. return numerator / denominator;
  138. }
  139. /** Provides a fast approximation of the function sin(x) using a Pade approximant
  140. continued fraction, calculated on a whole buffer.
  141. Note : this is an approximation which works on a limited range. You are
  142. advised to use input values only between -pi and +pi for limiting the error.
  143. */
  144. template <typename FloatType>
  145. static void sin (FloatType* values, size_t numValues) noexcept
  146. {
  147. for (size_t i = 0; i < numValues; ++i)
  148. values[i] = FastMathApproximations::sin (values[i]);
  149. }
  150. /** Provides a fast approximation of the function tan(x) using a Pade approximant
  151. continued fraction, calculated sample by sample.
  152. Note : this is an approximation which works on a limited range. You are
  153. advised to use input values only between -pi/2 and +pi/2 for limiting the error.
  154. */
  155. template <typename FloatType>
  156. static FloatType tan (FloatType x) noexcept
  157. {
  158. auto x2 = x * x;
  159. auto numerator = x * (-135135 + x2 * (17325 + x2 * (-378 + x2)));
  160. auto denominator = -135135 + x2 * (62370 + x2 * (-3150 + 28 * x2));
  161. return numerator / denominator;
  162. }
  163. /** Provides a fast approximation of the function tan(x) using a Pade approximant
  164. continued fraction, calculated on a whole buffer.
  165. Note : this is an approximation which works on a limited range. You are
  166. advised to use input values only between -pi/2 and +pi/2 for limiting the error.
  167. */
  168. template <typename FloatType>
  169. static void tan (FloatType* values, size_t numValues) noexcept
  170. {
  171. for (size_t i = 0; i < numValues; ++i)
  172. values[i] = FastMathApproximations::tan (values[i]);
  173. }
  174. //==============================================================================
  175. /** Provides a fast approximation of the function exp(x) using a Pade approximant
  176. continued fraction, calculated sample by sample.
  177. Note : this is an approximation which works on a limited range. You are
  178. advised to use input values only between -6 and +4 for limiting the error.
  179. */
  180. template <typename FloatType>
  181. static FloatType exp (FloatType x) noexcept
  182. {
  183. auto numerator = 1680 + x * (840 + x * (180 + x * (20 + x)));
  184. auto denominator = 1680 + x *(-840 + x * (180 + x * (-20 + x)));
  185. return numerator / denominator;
  186. }
  187. /** Provides a fast approximation of the function exp(x) using a Pade approximant
  188. continued fraction, calculated on a whole buffer.
  189. Note : this is an approximation which works on a limited range. You are
  190. advised to use input values only between -6 and +4 for limiting the error.
  191. */
  192. template <typename FloatType>
  193. static void exp (FloatType* values, size_t numValues) noexcept
  194. {
  195. for (size_t i = 0; i < numValues; ++i)
  196. values[i] = FastMathApproximations::exp (values[i]);
  197. }
  198. /** Provides a fast approximation of the function log(x+1) using a Pade approximant
  199. continued fraction, calculated sample by sample.
  200. Note : this is an approximation which works on a limited range. You are
  201. advised to use input values only between -0.8 and +5 for limiting the error.
  202. */
  203. template <typename FloatType>
  204. static FloatType logNPlusOne (FloatType x) noexcept
  205. {
  206. auto numerator = x * (7560 + x * (15120 + x * (9870 + x * (2310 + x * 137))));
  207. auto denominator = 7560 + x * (18900 + x * (16800 + x * (6300 + x * (900 + 30 * x))));
  208. return numerator / denominator;
  209. }
  210. /** Provides a fast approximation of the function log(x+1) using a Pade approximant
  211. continued fraction, calculated on a whole buffer.
  212. Note : this is an approximation which works on a limited range. You are
  213. advised to use input values only between -0.8 and +5 for limiting the error.
  214. */
  215. template <typename FloatType>
  216. static void logNPlusOne (FloatType* values, size_t numValues) noexcept
  217. {
  218. for (size_t i = 0; i < numValues; ++i)
  219. values[i] = FastMathApproximations::logNPlusOne (values[i]);
  220. }
  221. };
  222. } // namespace dsp
  223. } // namespace juce