35using int8 =
signed char;
37using uint8 =
unsigned char;
39using int16 =
signed short;
41using uint16 =
unsigned short;
43using int32 =
signed int;
45using uint32 =
unsigned int;
49 using int64 = __int64;
51 using uint64 =
unsigned __int64;
54 using int64 =
long long;
56 using uint64 =
unsigned long long;
65 #define literal64bit(longLiteral) (longLiteral##LL)
70 using pointer_sized_int = int64;
72 using pointer_sized_uint = uint64;
75 using pointer_sized_int = _W64 int;
77 using pointer_sized_uint = _W64
unsigned int;
80 using pointer_sized_int = int;
82 using pointer_sized_uint =
unsigned int;
85#if JUCE_WINDOWS && ! JUCE_MINGW
86 using ssize_t = pointer_sized_int;
91template <
typename... Types>
92void ignoreUnused (Types&&...) noexcept {}
102template <
typename Type,
size_t N>
103constexpr int numElementsInArray (Type (&)[N])
noexcept {
return N; }
110template <
typename Type>
111Type juce_hypot (Type a, Type b)
noexcept
114 return static_cast<Type
> (_hypot (a, b));
116 return static_cast<Type
> (hypot (a, b));
122inline float juce_hypot (
float a,
float b)
noexcept
125 return _hypotf (a, b);
127 return hypotf (a, b);
137template <
typename FloatType>
141 static constexpr FloatType
pi =
static_cast<FloatType
> (3.141592653589793238L);
144 static constexpr FloatType
twoPi =
static_cast<FloatType
> (2 * 3.141592653589793238L);
147 static constexpr FloatType
halfPi =
static_cast<FloatType
> (3.141592653589793238L / 2);
150 static constexpr FloatType
euler =
static_cast<FloatType
> (2.71828182845904523536L);
153 static constexpr FloatType
sqrt2 =
static_cast<FloatType
> (1.4142135623730950488L);
158[[deprecated (
"This is deprecated in favour of MathConstants<double>::pi.")]]
162[[deprecated (
"This is deprecated in favour of MathConstants<float>::pi.")]]
167template <
typename FloatType>
171template <
typename FloatType>
178template <
typename NumericType>
179bool juce_isfinite (NumericType value)
noexcept
181 if constexpr (std::numeric_limits<NumericType>::has_infinity
182 || std::numeric_limits<NumericType>::has_quiet_NaN
183 || std::numeric_limits<NumericType>::has_signaling_NaN)
185 return std::isfinite (value);
189 ignoreUnused (value);
200template <
typename Type>
201constexpr bool exactlyEqual (Type a, Type b)
203 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE (
"-Wfloat-equal")
205 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
214template <typename Type>
218 Tolerance() =
default;
229 return withMember (*
this, &Tolerance::absolute, std::abs (newAbsolute));
241 return withMember (*
this, &Tolerance::relative, std::abs (newRelative));
244 [[nodiscard]] Type getAbsolute()
const {
return absolute; }
245 [[nodiscard]] Type getRelative()
const {
return relative; }
256template <
typename Type>
266template <
typename Type>
298template <
typename Type, std::enable_if_t<std::is_
floating_po
int_v<Type>,
int> = 0>
299constexpr bool approximatelyEqual (Type a, Type b,
302 .withRelative (std::numeric_limits<Type>::epsilon()))
304 if (! (juce_isfinite (a) && juce_isfinite (b)))
305 return exactlyEqual (a, b);
307 const auto diff = std::abs (a - b);
309 return diff <= tolerance.getAbsolute()
310 || diff <= tolerance.getRelative() * std::max (std::abs (a), std::abs (b));
314template <
typename Type, std::enable_if_t<! std::is_
floating_po
int_v<Type>,
int> = 0>
315constexpr bool approximatelyEqual (Type a, Type b)
322template <
typename FloatType>
323FloatType nextFloatUp (FloatType value)
noexcept
325 return std::nextafter (value, std::numeric_limits<FloatType>::max());
329template <
typename FloatType>
330FloatType nextFloatDown (FloatType value)
noexcept
332 return std::nextafter (value, std::numeric_limits<FloatType>::lowest());
339template <
typename Type>
340constexpr Type jmax (Type a, Type b) {
return a < b ? b : a; }
343template <
typename Type>
344constexpr Type jmax (Type a, Type b, Type c) {
return a < b ? (b < c ? c : b) : (a < c ? c : a); }
347template <
typename Type>
348constexpr Type jmax (Type a, Type b, Type c, Type d) {
return jmax (a, jmax (b, c, d)); }
351template <
typename Type>
352constexpr Type jmin (Type a, Type b) {
return b < a ? b : a; }
355template <
typename Type>
356constexpr Type jmin (Type a, Type b, Type c) {
return b < a ? (c < b ? c : b) : (c < a ? c : a); }
359template <
typename Type>
360constexpr Type jmin (Type a, Type b, Type c, Type d) {
return jmin (a, jmin (b, c, d)); }
365template <
typename Type>
366constexpr Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
368 return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
372template <
typename Type>
373Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
375 jassert (! approximatelyEqual (sourceRangeMax, sourceRangeMin));
376 return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
389template <
typename Type>
390Type mapToLog10 (Type value0To1, Type logRangeMin, Type logRangeMax)
392 jassert (logRangeMin > 0);
393 jassert (logRangeMax > 0);
395 auto logMin = std::log10 (logRangeMin);
396 auto logMax = std::log10 (logRangeMax);
398 return std::pow ((Type) 10.0, value0To1 * (logMax - logMin) + logMin);
411template <
typename Type>
412Type mapFromLog10 (Type valueInLogRange, Type logRangeMin, Type logRangeMax)
414 jassert (logRangeMin > 0);
415 jassert (logRangeMax > 0);
417 auto logMin = std::log10 (logRangeMin);
418 auto logMax = std::log10 (logRangeMax);
420 return (std::log10 (valueInLogRange) - logMin) / (logMax - logMin);
424template <
typename Type,
typename Size>
425Type findMinimum (
const Type* data, Size numValues)
430 auto result = *data++;
432 while (--numValues > 0)
444template <
typename Type,
typename Size>
445Type findMaximum (
const Type* values, Size numValues)
450 auto result = *values++;
452 while (--numValues > 0)
464template <
typename Type>
465void findMinAndMax (
const Type* values,
int numValues, Type& lowest, Type& highest)
477 while (--numValues > 0)
507template <
typename Type>
508Type jlimit (Type lowerLimit,
510 Type valueToConstrain)
noexcept
512 jassert (lowerLimit <= upperLimit);
514 return valueToConstrain < lowerLimit ? lowerLimit
515 : (upperLimit < valueToConstrain ? upperLimit
524template <
typename Type1,
typename Type2>
525bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit)
noexcept
527 jassert (Type1() <=
static_cast<Type1
> (upperLimit));
528 return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
531template <
typename Type>
532bool isPositiveAndBelow (
int valueToTest, Type upperLimit)
noexcept
534 jassert (upperLimit >= 0);
535 return static_cast<unsigned int> (valueToTest) <
static_cast<unsigned int> (upperLimit);
543template <
typename Type1,
typename Type2>
544bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit)
noexcept
546 jassert (Type1() <=
static_cast<Type1
> (upperLimit));
547 return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
550template <
typename Type>
551bool isPositiveAndNotGreaterThan (
int valueToTest, Type upperLimit)
noexcept
553 jassert (upperLimit >= 0);
554 return static_cast<unsigned int> (valueToTest) <=
static_cast<unsigned int> (upperLimit);
560template <
typename Type>
561bool isWithin (Type a, Type b, Type tolerance)
noexcept
563 return std::abs (a - b) <= tolerance;
568 #pragma optimize ("t", off)
569 #ifndef __INTEL_COMPILER
570 #pragma float_control (precise, on, push)
584template <
typename FloatType>
585int roundToInt (
const FloatType value)
noexcept
587 #ifdef __INTEL_COMPILER
588 #pragma float_control (precise, on, push)
591 union {
int asInt[2];
double asDouble; } n;
592 n.asDouble = ((double) value) + 6755399441055744.0;
601inline int roundToInt (
int value)
noexcept
607 #ifndef __INTEL_COMPILER
608 #pragma float_control (pop)
610 #pragma optimize ("", on)
618inline int roundToIntAccurate (
double value)
noexcept
620 #ifdef __INTEL_COMPILER
621 #pragma float_control (pop)
624 return roundToInt (value + 1.5e-8);
634template <
typename FloatType>
635unsigned int truncatePositiveToUnsignedInt (FloatType value)
noexcept
637 jassert (value >=
static_cast<FloatType
> (0));
638 jassert (
static_cast<FloatType
> (value)
639 <=
static_cast<FloatType
> (std::numeric_limits<unsigned int>::max()));
641 return static_cast<unsigned int> (value);
646template <
typename IntegerType>
647constexpr bool isPowerOfTwo (IntegerType value)
649 return (value & (value - 1)) == 0;
653inline int nextPowerOfTwo (
int n)
noexcept
668int findHighestSetBit (uint32 n)
noexcept;
671constexpr int countNumberOfBits (uint32 n)
noexcept
673 n -= ((n >> 1) & 0x55555555);
674 n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
675 n = (((n >> 4) + n) & 0x0f0f0f0f);
678 return (
int) (n & 0x3f);
682constexpr int countNumberOfBits (uint64 n)
noexcept
684 return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
690template <
typename IntegerType>
691IntegerType negativeAwareModulo (IntegerType dividend,
const IntegerType divisor)
noexcept
693 jassert (divisor > 0);
695 return (dividend < 0) ? (dividend + divisor) : dividend;
699template <
typename NumericType>
700inline constexpr NumericType square (NumericType n)
noexcept
713void writeLittleEndianBitsInBuffer (
void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value)
noexcept;
722uint32 readLittleEndianBitsInBuffer (
const void* sourceBuffer, uint32 startBit, uint32 numBits)
noexcept;
726#if JUCE_INTEL || DOXYGEN
731 #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
733 #define JUCE_UNDENORMALISE(x)
752 template <
typename Type>
struct ParameterType {
using type =
const Type&; };
755 template <
typename Type>
struct ParameterType <Type&> {
using type = Type&; };
756 template <
typename Type>
struct ParameterType <Type*> {
using type = Type*; };
758 template <>
struct ParameterType <unsigned char> {
using type =
unsigned char; };
760 template <>
struct ParameterType <unsigned short> {
using type =
unsigned short; };
762 template <>
struct ParameterType <unsigned int> {
using type =
unsigned int; };
764 template <>
struct ParameterType <unsigned long> {
using type =
unsigned long; };
777 template <
typename Type>
797 [[deprecated (
"Use roundToInt instead.")]]
inline int roundDoubleToInt (
double value)
noexcept {
return roundToInt (value); }
798 [[deprecated (
"Use roundToInt instead.")]]
inline int roundFloatToInt (
float value)
noexcept {
return roundToInt (value); }
799 [[deprecated (
"Use std::abs() instead.")]]
inline int64 abs64 (int64 n)
noexcept {
return std::abs (n); }
806constexpr auto toUnderlyingType (T t) -> std::enable_if_t<std::is_enum_v<T>, std::underlying_type_t<T>>
808 return static_cast<std::underlying_type_t<T>
> (t);
Tolerance withRelative(Type newRelative)
Tolerance withAbsolute(Type newAbsolute)
std::conditional_t< std::is_same_v< Type, double >, double, float > SmallestFloatType
static constexpr FloatType halfPi
static constexpr FloatType twoPi
static constexpr FloatType sqrt2
static constexpr FloatType pi
static constexpr FloatType euler