LMMS
Loading...
Searching...
No Matches
juce_MathsFunctions.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
27/*
28 This file sets up some handy mathematical typdefs and functions.
29*/
30
31//==============================================================================
32// Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
33
35using int8 = signed char;
37using uint8 = unsigned char;
39using int16 = signed short;
41using uint16 = unsigned short;
43using int32 = signed int;
45using uint32 = unsigned int;
46
47#if JUCE_MSVC
49 using int64 = __int64;
51 using uint64 = unsigned __int64;
52#else
54 using int64 = long long;
56 using uint64 = unsigned long long;
57#endif
58
59#ifndef DOXYGEN
65 #define literal64bit(longLiteral) (longLiteral##LL)
66#endif
67
68#if JUCE_64BIT
70 using pointer_sized_int = int64;
72 using pointer_sized_uint = uint64;
73#elif JUCE_MSVC
75 using pointer_sized_int = _W64 int;
77 using pointer_sized_uint = _W64 unsigned int;
78#else
82 using pointer_sized_uint = unsigned int;
83#endif
84
85#if JUCE_WINDOWS && ! JUCE_MINGW
86 using ssize_t = pointer_sized_int;
87#endif
88
89//==============================================================================
90// Some indispensable min/max functions
91
93template <typename Type>
94constexpr Type jmax (Type a, Type b) { return a < b ? b : a; }
95
97template <typename Type>
98constexpr Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }
99
101template <typename Type>
102constexpr Type jmax (Type a, Type b, Type c, Type d) { return jmax (a, jmax (b, c, d)); }
103
105template <typename Type>
106constexpr Type jmin (Type a, Type b) { return b < a ? b : a; }
107
109template <typename Type>
110constexpr Type jmin (Type a, Type b, Type c) { return b < a ? (c < b ? c : b) : (c < a ? c : a); }
111
113template <typename Type>
114constexpr Type jmin (Type a, Type b, Type c, Type d) { return jmin (a, jmin (b, c, d)); }
115
119template <typename Type>
120constexpr Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
121{
122 return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
123}
124
126template <typename Type>
127Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
128{
129 jassert (sourceRangeMax != sourceRangeMin); // mapping from a range of zero will produce NaN!
130 return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
131}
132
143template <typename Type>
144Type mapToLog10 (Type value0To1, Type logRangeMin, Type logRangeMax)
145{
146 jassert (logRangeMin > 0);
147 jassert (logRangeMax > 0);
148
149 auto logMin = std::log10 (logRangeMin);
150 auto logMax = std::log10 (logRangeMax);
151
152 return std::pow ((Type) 10.0, value0To1 * (logMax - logMin) + logMin);
153}
154
165template <typename Type>
166Type mapFromLog10 (Type valueInLogRange, Type logRangeMin, Type logRangeMax)
167{
168 jassert (logRangeMin > 0);
169 jassert (logRangeMax > 0);
170
171 auto logMin = std::log10 (logRangeMin);
172 auto logMax = std::log10 (logRangeMax);
173
174 return (std::log10 (valueInLogRange) - logMin) / (logMax - logMin);
175}
176
178template <typename Type, typename Size>
179Type findMinimum (const Type* data, Size numValues)
180{
181 if (numValues <= 0)
182 return Type (0);
183
184 auto result = *data++;
185
186 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
187 {
188 auto v = *data++;
189
190 if (v < result)
191 result = v;
192 }
193
194 return result;
195}
196
198template <typename Type, typename Size>
199Type findMaximum (const Type* values, Size numValues)
200{
201 if (numValues <= 0)
202 return Type (0);
203
204 auto result = *values++;
205
206 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
207 {
208 auto v = *values++;
209
210 if (result < v)
211 result = v;
212 }
213
214 return result;
215}
216
218template <typename Type>
219void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
220{
221 if (numValues <= 0)
222 {
223 lowest = Type (0);
224 highest = Type (0);
225 }
226 else
227 {
228 auto mn = *values++;
229 auto mx = mn;
230
231 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
232 {
233 auto v = *values++;
234
235 if (mx < v) mx = v;
236 if (v < mn) mn = v;
237 }
238
239 lowest = mn;
240 highest = mx;
241 }
242}
243
244//==============================================================================
261template <typename Type>
262Type jlimit (Type lowerLimit,
263 Type upperLimit,
264 Type valueToConstrain) noexcept
265{
266 jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
267
268 return valueToConstrain < lowerLimit ? lowerLimit
269 : (upperLimit < valueToConstrain ? upperLimit
270 : valueToConstrain);
271}
272
278template <typename Type1, typename Type2>
279bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit) noexcept
280{
281 jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
282 return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
283}
284
285template <typename Type>
286bool isPositiveAndBelow (int valueToTest, Type upperLimit) noexcept
287{
288 jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
289 return static_cast<unsigned int> (valueToTest) < static_cast<unsigned int> (upperLimit);
290}
291
297template <typename Type1, typename Type2>
298bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit) noexcept
299{
300 jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
301 return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
302}
303
304template <typename Type>
305bool isPositiveAndNotGreaterThan (int valueToTest, Type upperLimit) noexcept
306{
307 jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
308 return static_cast<unsigned int> (valueToTest) <= static_cast<unsigned int> (upperLimit);
309}
310
314template <typename Type>
315bool isWithin (Type a, Type b, Type tolerance) noexcept
316{
317 return std::abs (a - b) <= tolerance;
318}
319
323template <typename Type>
324bool approximatelyEqual (Type a, Type b) noexcept
325{
326 return std::abs (a - b) <= (std::numeric_limits<Type>::epsilon() * std::max (a, b))
327 || std::abs (a - b) < std::numeric_limits<Type>::min();
328}
329
330//==============================================================================
332template <typename... Types>
333void ignoreUnused (Types&&...) noexcept {}
334
343template <typename Type, size_t N>
344constexpr int numElementsInArray (Type (&)[N]) noexcept { return N; }
345
346//==============================================================================
347// Some useful maths functions that aren't always present with all compilers and build settings.
348
351template <typename Type>
352Type juce_hypot (Type a, Type b) noexcept
353{
354 #if JUCE_MSVC
355 return static_cast<Type> (_hypot (a, b));
356 #else
357 return static_cast<Type> (hypot (a, b));
358 #endif
359}
360
361#ifndef DOXYGEN
362template <>
363inline float juce_hypot (float a, float b) noexcept
364{
365 #if JUCE_MSVC
366 return _hypotf (a, b);
367 #else
368 return hypotf (a, b);
369 #endif
370}
371#endif
372
373//==============================================================================
378template <typename FloatType>
380{
382 static constexpr FloatType pi = static_cast<FloatType> (3.141592653589793238L);
383
385 static constexpr FloatType twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
386
388 static constexpr FloatType halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
389
391 static constexpr FloatType euler = static_cast<FloatType> (2.71828182845904523536L);
392
394 static constexpr FloatType sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
395};
396
397#ifndef DOXYGEN
399[[deprecated ("This is deprecated in favour of MathConstants<double>::pi.")]]
400const constexpr double double_Pi = MathConstants<double>::pi;
401
403[[deprecated ("This is deprecated in favour of MathConstants<float>::pi.")]]
404const constexpr float float_Pi = MathConstants<float>::pi;
405#endif
406
408template <typename FloatType>
409constexpr FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (MathConstants<FloatType>::pi / FloatType (180)); }
410
412template <typename FloatType>
413constexpr FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / MathConstants<FloatType>::pi); }
414
415
416//==============================================================================
420template <typename NumericType>
421bool juce_isfinite (NumericType) noexcept
422{
423 return true; // Integer types are always finite
424}
425
426template <>
427inline bool juce_isfinite (float value) noexcept
428{
429 #if JUCE_WINDOWS && ! JUCE_MINGW
430 return _finite (value) != 0;
431 #else
432 return std::isfinite (value);
433 #endif
434}
435
436template <>
437inline bool juce_isfinite (double value) noexcept
438{
439 #if JUCE_WINDOWS && ! JUCE_MINGW
440 return _finite (value) != 0;
441 #else
442 return std::isfinite (value);
443 #endif
444}
445
446//==============================================================================
447#if JUCE_MSVC
448 #pragma optimize ("t", off)
449 #ifndef __INTEL_COMPILER
450 #pragma float_control (precise, on, push)
451 #endif
452#endif
453
464template <typename FloatType>
465int roundToInt (const FloatType value) noexcept
466{
467 #ifdef __INTEL_COMPILER
468 #pragma float_control (precise, on, push)
469 #endif
470
471 union { int asInt[2]; double asDouble; } n;
472 n.asDouble = ((double) value) + 6755399441055744.0;
473
474 #if JUCE_BIG_ENDIAN
475 return n.asInt [1];
476 #else
477 return n.asInt [0];
478 #endif
479}
480
481inline int roundToInt (int value) noexcept
482{
483 return value;
484}
485
486#if JUCE_MSVC
487 #ifndef __INTEL_COMPILER
488 #pragma float_control (pop)
489 #endif
490 #pragma optimize ("", on) // resets optimisations to the project defaults
491#endif
492
498inline int roundToIntAccurate (double value) noexcept
499{
500 #ifdef __INTEL_COMPILER
501 #pragma float_control (pop)
502 #endif
503
504 return roundToInt (value + 1.5e-8);
505}
506
507//==============================================================================
514template <typename FloatType>
515unsigned int truncatePositiveToUnsignedInt (FloatType value) noexcept
516{
517 jassert (value >= static_cast<FloatType> (0));
518 jassert (static_cast<FloatType> (value)
519 <= static_cast<FloatType> (std::numeric_limits<unsigned int>::max()));
520
521 return static_cast<unsigned int> (value);
522}
523
524//==============================================================================
526template <typename IntegerType>
527constexpr bool isPowerOfTwo (IntegerType value)
528{
529 return (value & (value - 1)) == 0;
530}
531
533inline int nextPowerOfTwo (int n) noexcept
534{
535 --n;
536 n |= (n >> 1);
537 n |= (n >> 2);
538 n |= (n >> 4);
539 n |= (n >> 8);
540 n |= (n >> 16);
541 return n + 1;
542}
543
548int findHighestSetBit (uint32 n) noexcept;
549
551inline int countNumberOfBits (uint32 n) noexcept
552{
553 n -= ((n >> 1) & 0x55555555);
554 n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
555 n = (((n >> 4) + n) & 0x0f0f0f0f);
556 n += (n >> 8);
557 n += (n >> 16);
558 return (int) (n & 0x3f);
559}
560
562inline int countNumberOfBits (uint64 n) noexcept
563{
564 return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
565}
566
570template <typename IntegerType>
571IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
572{
573 jassert (divisor > 0);
574 dividend %= divisor;
575 return (dividend < 0) ? (dividend + divisor) : dividend;
576}
577
579template <typename NumericType>
580inline constexpr NumericType square (NumericType n) noexcept
581{
582 return n * n;
583}
584
585//==============================================================================
593void writeLittleEndianBitsInBuffer (void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
594
602uint32 readLittleEndianBitsInBuffer (const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
603
604
605//==============================================================================
606#if JUCE_INTEL || DOXYGEN
611 #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
612#else
613 #define JUCE_UNDENORMALISE(x)
614#endif
615
616//==============================================================================
619namespace TypeHelpers
620{
632 template <typename Type> struct ParameterType { using type = const Type&; };
633
634 #ifndef DOXYGEN
635 template <typename Type> struct ParameterType <Type&> { using type = Type&; };
636 template <typename Type> struct ParameterType <Type*> { using type = Type*; };
637 template <> struct ParameterType <char> { using type = char; };
638 template <> struct ParameterType <unsigned char> { using type = unsigned char; };
639 template <> struct ParameterType <short> { using type = short; };
640 template <> struct ParameterType <unsigned short> { using type = unsigned short; };
641 template <> struct ParameterType <int> { using type = int; };
642 template <> struct ParameterType <unsigned int> { using type = unsigned int; };
643 template <> struct ParameterType <long> { using type = long; };
644 template <> struct ParameterType <unsigned long> { using type = unsigned long; };
645 template <> struct ParameterType <int64> { using type = int64; };
646 template <> struct ParameterType <uint64> { using type = uint64; };
647 template <> struct ParameterType <bool> { using type = bool; };
648 template <> struct ParameterType <float> { using type = float; };
649 template <> struct ParameterType <double> { using type = double; };
650 #endif
651
657 template <typename Type> struct SmallestFloatType { using type = float; };
658
659 #ifndef DOXYGEN
660 template <> struct SmallestFloatType <double> { using type = double; };
661 #endif
662
668 template <int bytes> struct UnsignedTypeWithSize {};
669
670 #ifndef DOXYGEN
671 template <> struct UnsignedTypeWithSize<1> { using type = uint8; };
672 template <> struct UnsignedTypeWithSize<2> { using type = uint16; };
673 template <> struct UnsignedTypeWithSize<4> { using type = uint32; };
674 template <> struct UnsignedTypeWithSize<8> { using type = uint64; };
675 #endif
676}
677
678//==============================================================================
679#ifndef DOXYGEN
680 [[deprecated ("Use roundToInt instead.")]] inline int roundDoubleToInt (double value) noexcept { return roundToInt (value); }
681 [[deprecated ("Use roundToInt instead.")]] inline int roundFloatToInt (float value) noexcept { return roundToInt (value); }
682 [[deprecated ("Use std::abs() instead.")]] inline int64 abs64 (int64 n) noexcept { return std::abs (n); }
683#endif
684
685} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
int64_t int64
Definition basics.h:91
uint64_t uint64
Definition basics.h:92
uint32_t uint32
Definition basics.h:90
int roundToIntAccurate(double value) noexcept
Definition MathsFunctions.h:368
unsigned v[N_MAX]
Definition inflate.c:1584
unsigned d
Definition inflate.c:940
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
#define jassert(expression)
Definition juce_MathsFunctions.h:620
Definition carla_juce.cpp:31
bool isWithin(Type a, Type b, Type tolerance) noexcept
Definition juce_MathsFunctions.h:315
Type mapToLog10(Type value0To1, Type logRangeMin, Type logRangeMax)
Definition juce_MathsFunctions.h:144
unsigned short uint16
Definition juce_MathsFunctions.h:41
unsigned long long uint64
Definition juce_MathsFunctions.h:56
const constexpr float float_Pi
Definition juce_MathsFunctions.h:404
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Definition juce_MathsFunctions.h:120
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
void findMinAndMax(const Type *values, int numValues, Type &lowest, Type &highest)
Definition juce_MathsFunctions.h:219
bool approximatelyEqual(Type a, Type b) noexcept
Definition juce_MathsFunctions.h:324
unsigned int uint32
Definition juce_MathsFunctions.h:45
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
signed short int16
Definition juce_MathsFunctions.h:39
bool juce_isfinite(NumericType) noexcept
Definition juce_MathsFunctions.h:421
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
int nextPowerOfTwo(int n) noexcept
Definition juce_MathsFunctions.h:533
long long int64
Definition juce_MathsFunctions.h:54
constexpr NumericType square(NumericType n) noexcept
Definition juce_MathsFunctions.h:580
void ignoreUnused(Types &&...) noexcept
Definition juce_MathsFunctions.h:333
signed char int8
Definition juce_MathsFunctions.h:35
int countNumberOfBits(uint32 n) noexcept
Definition juce_MathsFunctions.h:551
Type findMaximum(const Type *values, Size numValues)
Definition juce_MathsFunctions.h:199
Type juce_hypot(Type a, Type b) noexcept
Definition juce_MathsFunctions.h:352
constexpr FloatType radiansToDegrees(FloatType radians) noexcept
Definition juce_MathsFunctions.h:413
const constexpr double double_Pi
Definition juce_MathsFunctions.h:400
signed int int32
Definition juce_MathsFunctions.h:43
bool isPositiveAndNotGreaterThan(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:298
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:279
constexpr bool isPowerOfTwo(IntegerType value)
Definition juce_MathsFunctions.h:527
int pointer_sized_int
Definition juce_MathsFunctions.h:80
constexpr FloatType degreesToRadians(FloatType degrees) noexcept
Definition juce_MathsFunctions.h:409
unsigned int truncatePositiveToUnsignedInt(FloatType value) noexcept
Definition juce_MathsFunctions.h:515
unsigned int pointer_sized_uint
Definition juce_MathsFunctions.h:82
IntegerType negativeAwareModulo(IntegerType dividend, const IntegerType divisor) noexcept
Definition juce_MathsFunctions.h:571
Type findMinimum(const Type *data, Size numValues)
Definition juce_MathsFunctions.h:179
unsigned char uint8
Definition juce_MathsFunctions.h:37
int roundFloatToInt(float value) noexcept
Definition juce_MathsFunctions.h:681
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
constexpr int numElementsInArray(Type(&)[N]) noexcept
Definition juce_MathsFunctions.h:344
int64 abs64(int64 n) noexcept
Definition juce_MathsFunctions.h:682
Type mapFromLog10(Type valueInLogRange, Type logRangeMin, Type logRangeMax)
Definition juce_MathsFunctions.h:166
int roundDoubleToInt(double value) noexcept
Definition juce_MathsFunctions.h:680
#define N
Definition nseel-cfunc.c:36
Definition juce_MathsFunctions.h:380
static constexpr FloatType halfPi
Definition juce_MathsFunctions.h:388
static constexpr FloatType twoPi
Definition juce_MathsFunctions.h:385
static constexpr FloatType sqrt2
Definition juce_MathsFunctions.h:394
static constexpr FloatType pi
Definition juce_MathsFunctions.h:382
static constexpr FloatType euler
Definition juce_MathsFunctions.h:391
Type * type
Definition juce_MathsFunctions.h:636
Type & type
Definition juce_MathsFunctions.h:635
bool type
Definition juce_MathsFunctions.h:647
char type
Definition juce_MathsFunctions.h:637
double type
Definition juce_MathsFunctions.h:649
float type
Definition juce_MathsFunctions.h:648
int64 type
Definition juce_MathsFunctions.h:645
int type
Definition juce_MathsFunctions.h:641
long type
Definition juce_MathsFunctions.h:643
short type
Definition juce_MathsFunctions.h:639
uint64 type
Definition juce_MathsFunctions.h:646
unsigned char type
Definition juce_MathsFunctions.h:638
unsigned int type
Definition juce_MathsFunctions.h:642
unsigned long type
Definition juce_MathsFunctions.h:644
unsigned short type
Definition juce_MathsFunctions.h:640
Definition juce_MathsFunctions.h:632
const Type & type
Definition juce_MathsFunctions.h:632
double type
Definition juce_MathsFunctions.h:660
Definition juce_MathsFunctions.h:657
float type
Definition juce_MathsFunctions.h:657
uint8 type
Definition juce_MathsFunctions.h:671
uint16 type
Definition juce_MathsFunctions.h:672
uint32 type
Definition juce_MathsFunctions.h:673
uint64 type
Definition juce_MathsFunctions.h:674
Definition juce_MathsFunctions.h:668
#define __int64
int n
Definition crypt.c:458
return c
Definition crypt.c:175
b
Definition crypt.c:628
int result
Definition process.c:1455
typedef int(UZ_EXP MsgFn)()