openshot-audio  0.1.4
juce_MathsFunctions.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_MATHSFUNCTIONS_H_INCLUDED
30 #define JUCE_MATHSFUNCTIONS_H_INCLUDED
31 
32 //==============================================================================
33 /*
34  This file sets up some handy mathematical typdefs and functions.
35 */
36 
37 //==============================================================================
38 // Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
39 
41 typedef signed char int8;
43 typedef unsigned char uint8;
45 typedef signed short int16;
47 typedef unsigned short uint16;
49 typedef signed int int32;
51 typedef unsigned int uint32;
52 
53 #if JUCE_MSVC
54 
55  typedef __int64 int64;
57  typedef unsigned __int64 uint64;
58 #else
59 
60  typedef long long int64;
62  typedef unsigned long long uint64;
63 #endif
64 
65 #ifndef DOXYGEN
66 
71  #define literal64bit(longLiteral) (longLiteral##LL)
72 #endif
73 
74 #if JUCE_64BIT
75 
76  typedef int64 pointer_sized_int;
78  typedef uint64 pointer_sized_uint;
79 #elif JUCE_MSVC
80 
81  typedef _W64 int pointer_sized_int;
83  typedef _W64 unsigned int pointer_sized_uint;
84 #else
85 
86  typedef int pointer_sized_int;
88  typedef unsigned int pointer_sized_uint;
89 #endif
90 
91 #if JUCE_MSVC
92  typedef pointer_sized_int ssize_t;
93 #endif
94 
95 //==============================================================================
96 // Some indispensible min/max functions
97 
99 template <typename Type>
100 inline Type jmax (const Type a, const Type b) { return (a < b) ? b : a; }
101 
103 template <typename Type>
104 inline Type jmax (const Type a, const Type b, const Type c) { return (a < b) ? ((b < c) ? c : b) : ((a < c) ? c : a); }
105 
107 template <typename Type>
108 inline Type jmax (const Type a, const Type b, const Type c, const Type d) { return jmax (a, jmax (b, c, d)); }
109 
111 template <typename Type>
112 inline Type jmin (const Type a, const Type b) { return (b < a) ? b : a; }
113 
115 template <typename Type>
116 inline Type jmin (const Type a, const Type b, const Type c) { return (b < a) ? ((c < b) ? c : b) : ((c < a) ? c : a); }
117 
119 template <typename Type>
120 inline Type jmin (const Type a, const Type b, const Type c, const Type d) { return jmin (a, jmin (b, c, d)); }
121 
125 template <class Type>
126 static Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
127 {
128  return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
129 }
130 
132 template <class Type>
133 static Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
134 {
135  return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
136 }
137 
139 template <typename Type>
140 const Type findMinimum (const Type* data, int numValues)
141 {
142  if (numValues <= 0)
143  return Type();
144 
145  Type result (*data++);
146 
147  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
148  {
149  const Type& v = *data++;
150  if (v < result) result = v;
151  }
152 
153  return result;
154 }
155 
157 template <typename Type>
158 const Type findMaximum (const Type* values, int numValues)
159 {
160  if (numValues <= 0)
161  return Type();
162 
163  Type result (*values++);
164 
165  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
166  {
167  const Type& v = *values++;
168  if (result < v) result = v;
169  }
170 
171  return result;
172 }
173 
175 template <typename Type>
176 void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
177 {
178  if (numValues <= 0)
179  {
180  lowest = Type();
181  highest = Type();
182  }
183  else
184  {
185  Type mn (*values++);
186  Type mx (mn);
187 
188  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
189  {
190  const Type& v = *values++;
191 
192  if (mx < v) mx = v;
193  if (v < mn) mn = v;
194  }
195 
196  lowest = mn;
197  highest = mx;
198  }
199 }
200 
201 
202 //==============================================================================
219 template <typename Type>
220 inline Type jlimit (const Type lowerLimit,
221  const Type upperLimit,
222  const Type valueToConstrain) noexcept
223 {
224  jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
225 
226  return (valueToConstrain < lowerLimit) ? lowerLimit
227  : ((upperLimit < valueToConstrain) ? upperLimit
228  : valueToConstrain);
229 }
230 
236 template <typename Type>
237 inline bool isPositiveAndBelow (Type valueToTest, Type upperLimit) noexcept
238 {
239  jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
240  return Type() <= valueToTest && valueToTest < upperLimit;
241 }
242 
243 template <>
244 inline bool isPositiveAndBelow (const int valueToTest, const int upperLimit) noexcept
245 {
246  jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
247  return static_cast <unsigned int> (valueToTest) < static_cast <unsigned int> (upperLimit);
248 }
249 
255 template <typename Type>
256 inline bool isPositiveAndNotGreaterThan (Type valueToTest, Type upperLimit) noexcept
257 {
258  jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
259  return Type() <= valueToTest && valueToTest <= upperLimit;
260 }
261 
262 template <>
263 inline bool isPositiveAndNotGreaterThan (const int valueToTest, const int upperLimit) noexcept
264 {
265  jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
266  return static_cast <unsigned int> (valueToTest) <= static_cast <unsigned int> (upperLimit);
267 }
268 
269 //==============================================================================
271 template <typename Type>
272 inline void swapVariables (Type& variable1, Type& variable2)
273 {
274  std::swap (variable1, variable2);
275 }
276 
278 template <typename Type1>
279 void ignoreUnused (const Type1&) noexcept {}
280 
281 template <typename Type1, typename Type2>
282 void ignoreUnused (const Type1&, const Type2&) noexcept {}
283 
284 template <typename Type1, typename Type2, typename Type3>
285 void ignoreUnused (const Type1&, const Type2&, const Type3&) noexcept {}
286 
287 template <typename Type1, typename Type2, typename Type3, typename Type4>
288 void ignoreUnused (const Type1&, const Type2&, const Type3&, const Type4&) noexcept {}
289 
298 template <typename Type, int N>
299 inline int numElementsInArray (Type (&array)[N])
300 {
301  (void) array; // (required to avoid a spurious warning in MS compilers)
302  (void) sizeof (0[array]); // This line should cause an error if you pass an object with a user-defined subscript operator
303  return N;
304 }
305 
306 //==============================================================================
307 // Some useful maths functions that aren't always present with all compilers and build settings.
308 
311 template <typename Type>
312 inline Type juce_hypot (Type a, Type b) noexcept
313 {
314 #ifndef IGNORE_JUCE_HYPOT
315  #if JUCE_MSVC
316  return static_cast<Type> (_hypot (a, b));
317  #else
318  return static_cast<Type> (hypot (a, b));
319  #endif
320 #endif
321 }
322 
323 #ifndef DOXYGEN
324 template <>
325 inline float juce_hypot (float a, float b) noexcept
326 {
327  #if JUCE_MSVC
328  return (_hypotf (a, b));
329  #else
330  return (hypotf (a, b));
331  #endif
332 }
333 #endif
334 
336 inline int64 abs64 (const int64 n) noexcept
337 {
338  return (n >= 0) ? n : -n;
339 }
340 
341 #if JUCE_MSVC && ! defined (DOXYGEN) // The MSVC libraries omit these functions for some reason...
342  template<typename Type> Type asinh (Type x) noexcept { return std::log (x + std::sqrt (x * x + (Type) 1)); }
343  template<typename Type> Type acosh (Type x) noexcept { return std::log (x + std::sqrt (x * x - (Type) 1)); }
344  template<typename Type> Type atanh (Type x) noexcept { return (std::log (x + (Type) 1) - std::log (((Type) 1) - x)) / (Type) 2; }
345 #endif
346 
347 //==============================================================================
351 const double double_Pi = 3.1415926535897932384626433832795;
352 
356 const float float_Pi = 3.14159265358979323846f;
357 
358 
360 template <typename FloatType>
361 inline FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * static_cast<FloatType> (double_Pi / 180.0); }
362 
364 template <typename FloatType>
365 inline FloatType radiansToDegrees (FloatType radians) noexcept { return radians * static_cast<FloatType> (180.0 / double_Pi); }
366 
367 
368 //==============================================================================
372 template <typename NumericType>
373 inline bool juce_isfinite (NumericType) noexcept
374 {
375  return true; // Integer types are always finite
376 }
377 
378 template <>
379 inline bool juce_isfinite (float value) noexcept
380 {
381  #if JUCE_WINDOWS && !JUCE_MINGW
382  return _finite (value) != 0;
383  #else
384  return std::isfinite (value);
385  #endif
386 }
387 
388 template <>
389 inline bool juce_isfinite (double value) noexcept
390 {
391  #if JUCE_WINDOWS && !JUCE_MINGW
392  return _finite (value) != 0;
393  #else
394  return std::isfinite (value);
395  #endif
396 }
397 
398 //==============================================================================
399 #if JUCE_MSVC
400  #pragma optimize ("t", off)
401  #ifndef __INTEL_COMPILER
402  #pragma float_control (precise, on, push)
403  #endif
404 #endif
405 
416 template <typename FloatType>
417 inline int roundToInt (const FloatType value) noexcept
418 {
419  #ifdef __INTEL_COMPILER
420  #pragma float_control (precise, on, push)
421  #endif
422 
423  union { int asInt[2]; double asDouble; } n;
424  n.asDouble = ((double) value) + 6755399441055744.0;
425 
426  #if JUCE_BIG_ENDIAN
427  return n.asInt [1];
428  #else
429  return n.asInt [0];
430  #endif
431 }
432 
433 inline int roundToInt (int value) noexcept
434 {
435  return value;
436 }
437 
438 #if JUCE_MSVC
439  #ifndef __INTEL_COMPILER
440  #pragma float_control (pop)
441  #endif
442  #pragma optimize ("", on) // resets optimisations to the project defaults
443 #endif
444 
450 inline int roundToIntAccurate (const double value) noexcept
451 {
452  #ifdef __INTEL_COMPILER
453  #pragma float_control (pop)
454  #endif
455 
456  return roundToInt (value + 1.5e-8);
457 }
458 
470 inline int roundDoubleToInt (const double value) noexcept
471 {
472  return roundToInt (value);
473 }
474 
485 inline int roundFloatToInt (const float value) noexcept
486 {
487  return roundToInt (value);
488 }
489 
490 //==============================================================================
492 template <typename IntegerType>
493 bool isPowerOfTwo (IntegerType value)
494 {
495  return (value & (value - 1)) == 0;
496 }
497 
499 inline int nextPowerOfTwo (int n) noexcept
500 {
501  --n;
502  n |= (n >> 1);
503  n |= (n >> 2);
504  n |= (n >> 4);
505  n |= (n >> 8);
506  n |= (n >> 16);
507  return n + 1;
508 }
509 
512 {
513  n -= ((n >> 1) & 0x55555555);
514  n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
515  n = (((n >> 4) + n) & 0x0f0f0f0f);
516  n += (n >> 8);
517  n += (n >> 16);
518  return (int) (n & 0x3f);
519 }
520 
523 {
524  return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
525 }
526 
530 template <typename IntegerType>
531 IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
532 {
533  jassert (divisor > 0);
534  dividend %= divisor;
535  return (dividend < 0) ? (dividend + divisor) : dividend;
536 }
537 
539 template <typename NumericType>
540 NumericType square (NumericType n) noexcept
541 {
542  return n * n;
543 }
544 
545 
546 //==============================================================================
547 #if JUCE_INTEL || defined (DOXYGEN)
548 
552  #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
553 #else
554  #define JUCE_UNDENORMALISE(x)
555 #endif
556 
557 //==============================================================================
560 namespace TypeHelpers
561 {
562  #if JUCE_VC8_OR_EARLIER
563  #define PARAMETER_TYPE(type) const type&
564  #else
565 
577  template <typename Type> struct ParameterType { typedef const Type& type; };
578 
579  #if ! DOXYGEN
580  template <typename Type> struct ParameterType <Type&> { typedef Type& type; };
581  template <typename Type> struct ParameterType <Type*> { typedef Type* type; };
582  template <> struct ParameterType <char> { typedef char type; };
583  template <> struct ParameterType <unsigned char> { typedef unsigned char type; };
584  template <> struct ParameterType <short> { typedef short type; };
585  template <> struct ParameterType <unsigned short> { typedef unsigned short type; };
586  template <> struct ParameterType <int> { typedef int type; };
587  template <> struct ParameterType <unsigned int> { typedef unsigned int type; };
588  template <> struct ParameterType <long> { typedef long type; };
589  template <> struct ParameterType <unsigned long> { typedef unsigned long type; };
590  template <> struct ParameterType <int64> { typedef int64 type; };
591  template <> struct ParameterType <uint64> { typedef uint64 type; };
592  template <> struct ParameterType <bool> { typedef bool type; };
593  template <> struct ParameterType <float> { typedef float type; };
594  template <> struct ParameterType <double> { typedef double type; };
595  #endif
596 
600  #define PARAMETER_TYPE(a) typename TypeHelpers::ParameterType<a>::type
601  #endif
602 
603 
607  template <typename Type> struct SmallestFloatType { typedef float type; };
608  template <> struct SmallestFloatType <double> { typedef double type; };
609 }
610 
611 
612 //==============================================================================
613 
614 #endif // JUCE_MATHSFUNCTIONS_H_INCLUDED
int numElementsInArray(Type(&array)[N])
Definition: juce_MathsFunctions.h:299
double type
Definition: juce_MathsFunctions.h:608
Type juce_hypot(Type a, Type b) noexcept
Definition: juce_MathsFunctions.h:312
const Type & type
Definition: juce_MathsFunctions.h:577
Definition: juce_MathsFunctions.h:577
Definition: juce_MathsFunctions.h:560
unsigned long type
Definition: juce_MathsFunctions.h:589
const double double_Pi
Definition: juce_MathsFunctions.h:351
int pointer_sized_int
Definition: juce_MathsFunctions.h:86
const Type findMinimum(const Type *data, int numValues)
Definition: juce_MathsFunctions.h:140
int nextPowerOfTwo(int n) noexcept
Definition: juce_MathsFunctions.h:499
#define noexcept
Definition: juce_CompilerSupport.h:141
signed short int16
Definition: juce_MathsFunctions.h:45
short type
Definition: juce_MathsFunctions.h:584
unsigned short uint16
Definition: juce_MathsFunctions.h:47
Type jmax(const Type a, const Type b)
Definition: juce_MathsFunctions.h:100
bool isPositiveAndBelow(Type valueToTest, Type upperLimit) noexcept
Definition: juce_MathsFunctions.h:237
bool isPowerOfTwo(IntegerType value)
Definition: juce_MathsFunctions.h:493
NumericType square(NumericType n) noexcept
Definition: juce_MathsFunctions.h:540
unsigned char type
Definition: juce_MathsFunctions.h:583
int roundFloatToInt(const float value) noexcept
Definition: juce_MathsFunctions.h:485
int countNumberOfBits(uint32 n) noexcept
Definition: juce_MathsFunctions.h:511
int roundDoubleToInt(const double value) noexcept
Definition: juce_MathsFunctions.h:470
const float float_Pi
Definition: juce_MathsFunctions.h:356
float type
Definition: juce_MathsFunctions.h:593
Type jmin(const Type a, const Type b)
Definition: juce_MathsFunctions.h:112
uint64 type
Definition: juce_MathsFunctions.h:591
Definition: juce_MathsFunctions.h:607
FloatType radiansToDegrees(FloatType radians) noexcept
Definition: juce_MathsFunctions.h:365
void findMinAndMax(const Type *values, int numValues, Type &lowest, Type &highest)
Definition: juce_MathsFunctions.h:176
double type
Definition: juce_MathsFunctions.h:594
char type
Definition: juce_MathsFunctions.h:582
int type
Definition: juce_MathsFunctions.h:586
int64 type
Definition: juce_MathsFunctions.h:590
int64 abs64(const int64 n) noexcept
Definition: juce_MathsFunctions.h:336
unsigned long long uint64
Definition: juce_MathsFunctions.h:62
unsigned int uint32
Definition: juce_MathsFunctions.h:51
unsigned int type
Definition: juce_MathsFunctions.h:587
int roundToInt(const FloatType value) noexcept
Definition: juce_MathsFunctions.h:417
bool juce_isfinite(NumericType) noexcept
Definition: juce_MathsFunctions.h:373
FloatType degreesToRadians(FloatType degrees) noexcept
Definition: juce_MathsFunctions.h:361
IntegerType negativeAwareModulo(IntegerType dividend, const IntegerType divisor) noexcept
Definition: juce_MathsFunctions.h:531
void ignoreUnused(const Type1 &) noexcept
Definition: juce_MathsFunctions.h:279
bool type
Definition: juce_MathsFunctions.h:592
long long int64
Definition: juce_MathsFunctions.h:60
bool isPositiveAndNotGreaterThan(Type valueToTest, Type upperLimit) noexcept
Definition: juce_MathsFunctions.h:256
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition: juce_MathsFunctions.h:220
Type & type
Definition: juce_MathsFunctions.h:580
signed char int8
Definition: juce_MathsFunctions.h:41
Type * type
Definition: juce_MathsFunctions.h:581
float type
Definition: juce_MathsFunctions.h:607
#define jassert(a)
Definition: juce_PlatformDefs.h:146
int roundToIntAccurate(const double value) noexcept
Definition: juce_MathsFunctions.h:450
const Type findMaximum(const Type *values, int numValues)
Definition: juce_MathsFunctions.h:158
unsigned char uint8
Definition: juce_MathsFunctions.h:43
void swapVariables(Type &variable1, Type &variable2)
Definition: juce_MathsFunctions.h:272
unsigned int pointer_sized_uint
Definition: juce_MathsFunctions.h:88
unsigned short type
Definition: juce_MathsFunctions.h:585
long type
Definition: juce_MathsFunctions.h:588
signed int int32
Definition: juce_MathsFunctions.h:49