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