Audio plugin host https://kx.studio/carla
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.

265 lines
11KB

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