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.

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