39 using int8 =
signed char;
41 using uint8 =
unsigned char;
43 using int16 =
signed short;
45 using uint16 =
unsigned short;
47 using int32 =
signed int;
49 using uint32 =
unsigned int;
53 using int64 = __int64;
55 using uint64 =
unsigned __int64;
58 using int64 =
long long;
60 using uint64 =
unsigned long long;
69 #define literal64bit(longLiteral) (longLiteral##LL)
74 using pointer_sized_int = int64;
76 using pointer_sized_uint = uint64;
79 using pointer_sized_int = _W64 int;
81 using pointer_sized_uint = _W64
unsigned int;
84 using pointer_sized_int = int;
86 using pointer_sized_uint =
unsigned int;
89 #if JUCE_WINDOWS && ! JUCE_MINGW
90 using ssize_t = pointer_sized_int;
97 template <
typename Type>
98 JUCE_CONSTEXPR Type jmax (Type a, Type b) {
return a < b ? b : a; }
101 template <
typename Type>
102 JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c) {
return a < b ? (b < c ? c : b) : (a < c ? c : a); }
105 template <
typename Type>
106 JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c, Type d) {
return jmax (a, jmax (b, c, d)); }
109 template <
typename Type>
110 JUCE_CONSTEXPR Type jmin (Type a, Type b) {
return b < a ? b : a; }
113 template <
typename Type>
114 JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c) {
return b < a ? (c < b ? c : b) : (c < a ? c : a); }
117 template <
typename Type>
118 JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c, Type d) {
return jmin (a, jmin (b, c, d)); }
123 template <
typename Type>
124 JUCE_CONSTEXPR Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
126 return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
130 template <
typename Type>
131 Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
133 jassert (sourceRangeMax != sourceRangeMin);
134 return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
138 template <
typename Type>
139 Type findMinimum (
const Type* data,
int numValues)
144 auto result = *data++;
146 while (--numValues > 0)
158 template <
typename Type>
159 Type findMaximum (
const Type* values,
int numValues)
164 auto result = *values++;
166 while (--numValues > 0)
178 template <
typename Type>
179 void findMinAndMax (
const Type* values,
int numValues, Type& lowest, Type& highest)
191 while (--numValues > 0)
221 template <
typename Type>
222 Type jlimit (Type lowerLimit,
224 Type valueToConstrain) noexcept
226 jassert (lowerLimit <= upperLimit);
228 return valueToConstrain < lowerLimit ? lowerLimit
229 : (upperLimit < valueToConstrain ? upperLimit
238 template <
typename Type1,
typename Type2>
239 bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit) noexcept
241 jassert (Type1() <=
static_cast<Type1
> (upperLimit));
242 return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
245 template <
typename Type>
246 bool isPositiveAndBelow (
int valueToTest, Type upperLimit) noexcept
248 jassert (upperLimit >= 0);
249 return static_cast<unsigned int> (valueToTest) <
static_cast<unsigned int> (upperLimit);
257 template <
typename Type1,
typename Type2>
258 bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit) noexcept
260 jassert (Type1() <=
static_cast<Type1
> (upperLimit));
261 return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
264 template <
typename Type>
265 bool isPositiveAndNotGreaterThan (
int valueToTest, Type upperLimit) noexcept
267 jassert (upperLimit >= 0);
268 return static_cast<unsigned int> (valueToTest) <=
static_cast<unsigned int> (upperLimit);
274 template <
typename Type>
275 bool isWithin (Type a, Type b, Type tolerance) noexcept
277 return std::abs (a - b) <= tolerance;
283 template <
typename Type>
284 bool approximatelyEqual (Type a, Type b) noexcept
286 return std::abs (a - b) <= (std::numeric_limits<Type>::epsilon() * std::max (a, b))
287 || std::abs (a - b) < std::numeric_limits<Type>::min();
292 template <
typename... Types>
293 void ignoreUnused (Types&&...) noexcept {}
303 template <
typename Type,
int N>
304 int numElementsInArray (Type (&array)[N])
307 (void)
sizeof (0[array]);
316 template <
typename Type>
317 Type juce_hypot (Type a, Type b) noexcept
320 return static_cast<Type
> (_hypot (a, b));
322 return static_cast<Type
> (hypot (a, b));
328 inline float juce_hypot (
float a,
float b) noexcept
331 return _hypotf (a, b);
333 return hypotf (a, b);
338 #if JUCE_MSVC && ! defined (DOXYGEN) // The MSVC libraries omit these functions for some reason...
339 template<
typename Type> Type asinh (Type x) {
return std::log (x + std::sqrt (x * x + (Type) 1)); }
340 template<
typename Type> Type acosh (Type x) {
return std::log (x + std::sqrt (x * x - (Type) 1)); }
341 template<
typename Type> Type atanh (Type x) {
return (std::log (x + (Type) 1) - std::log (((Type) 1) - x)) / (Type) 2; }
345 #if JUCE_HAS_CONSTEXPR
351 template <
typename FloatType>
355 static constexpr FloatType
pi =
static_cast<FloatType
> (3.141592653589793238L);
358 static constexpr FloatType
twoPi =
static_cast<FloatType
> (2 * 3.141592653589793238L);
361 static constexpr FloatType
halfPi =
static_cast<FloatType
> (3.141592653589793238L / 2);
364 static constexpr FloatType
euler =
static_cast<FloatType
> (2.71828182845904523536L);
367 static constexpr FloatType
sqrt2 =
static_cast<FloatType
> (1.4142135623730950488L);
376 template <
typename FloatType>
380 static const FloatType
pi;
395 template <
typename FloatType>
398 template <
typename FloatType>
401 template <
typename FloatType>
404 template <
typename FloatType>
407 template <
typename FloatType>
429 template <
typename FloatType>
430 JUCE_CONSTEXPR FloatType degreesToRadians (FloatType degrees) noexcept {
return degrees * (
MathConstants<FloatType>::pi / FloatType (180)); }
433 template <
typename FloatType>
434 JUCE_CONSTEXPR FloatType radiansToDegrees (FloatType radians) noexcept {
return radians * (FloatType (180) /
MathConstants<FloatType>::pi); }
441 template <
typename NumericType>
442 bool juce_isfinite (NumericType) noexcept
448 inline bool juce_isfinite (
float value) noexcept
450 #if JUCE_WINDOWS && ! JUCE_MINGW
451 return _finite (value) != 0;
454 return isfinite (value);
459 inline bool juce_isfinite (
double value) noexcept
461 #if JUCE_WINDOWS && ! JUCE_MINGW
462 return _finite (value) != 0;
465 return isfinite (value);
471 #pragma optimize ("t", off)
472 #ifndef __INTEL_COMPILER
473 #pragma float_control (precise, on, push)
487 template <
typename FloatType>
488 int roundToInt (
const FloatType value) noexcept
490 #ifdef __INTEL_COMPILER
491 #pragma float_control (precise, on, push)
494 union {
int asInt[2];
double asDouble; } n;
495 n.asDouble = ((double) value) + 6755399441055744.0;
504 inline int roundToInt (
int value) noexcept
510 #ifndef __INTEL_COMPILER
511 #pragma float_control (pop)
513 #pragma optimize ("", on) // resets optimisations to the project defaults
521 inline int roundToIntAccurate (
double value) noexcept
523 #ifdef __INTEL_COMPILER
524 #pragma float_control (pop)
527 return roundToInt (value + 1.5e-8);
537 template <
typename FloatType>
538 unsigned int truncatePositiveToUnsignedInt (FloatType value) noexcept
540 jassert (value >=
static_cast<FloatType
> (0));
541 jassert (
static_cast<FloatType
> (value) <= std::numeric_limits<unsigned int>::max());
543 return static_cast<unsigned int> (value);
548 template <
typename IntegerType>
549 JUCE_CONSTEXPR
bool isPowerOfTwo (IntegerType value)
551 return (value & (value - 1)) == 0;
555 inline int nextPowerOfTwo (
int n) noexcept
570 int findHighestSetBit (uint32 n) noexcept;
573 inline int countNumberOfBits (uint32 n) noexcept
575 n -= ((n >> 1) & 0x55555555);
576 n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
577 n = (((n >> 4) + n) & 0x0f0f0f0f);
580 return (
int) (n & 0x3f);
584 inline int countNumberOfBits (uint64 n) noexcept
586 return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
592 template <
typename IntegerType>
593 IntegerType negativeAwareModulo (IntegerType dividend,
const IntegerType divisor) noexcept
595 jassert (divisor > 0);
597 return (dividend < 0) ? (dividend + divisor) : dividend;
601 template <
typename NumericType>
602 inline JUCE_CONSTEXPR NumericType square (NumericType n) noexcept
615 void writeLittleEndianBitsInBuffer (
void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
624 uint32 readLittleEndianBitsInBuffer (
const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
628 #if JUCE_INTEL || defined (DOXYGEN)
633 #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
635 #define JUCE_UNDENORMALISE(x)
641 namespace TypeHelpers
654 template <
typename Type>
struct ParameterType {
using type =
const Type&; };
657 template <
typename Type>
struct ParameterType <Type&> {
using type = Type&; };
658 template <
typename Type>
struct ParameterType <Type*> {
using type = Type*; };
659 template <>
struct ParameterType <char> {
using type = char; };
660 template <>
struct ParameterType <unsigned char> {
using type =
unsigned char; };
661 template <>
struct ParameterType <short> {
using type = short; };
662 template <>
struct ParameterType <unsigned short> {
using type =
unsigned short; };
663 template <>
struct ParameterType <int> {
using type = int; };
664 template <>
struct ParameterType <unsigned int> {
using type =
unsigned int; };
665 template <>
struct ParameterType <long> {
using type = long; };
666 template <>
struct ParameterType <unsigned long> {
using type =
unsigned long; };
667 template <>
struct ParameterType <int64> {
using type = int64; };
668 template <>
struct ParameterType <uint64> {
using type = uint64; };
669 template <>
struct ParameterType <bool> {
using type = bool; };
670 template <>
struct ParameterType <float> {
using type = float; };
671 template <>
struct ParameterType <double> {
using type = double; };
694 template <>
struct UnsignedTypeWithSize<2> {
using type = uint16; };
695 template <>
struct UnsignedTypeWithSize<4> {
using type = uint32; };
696 template <>
struct UnsignedTypeWithSize<8> {
using type = uint64; };
703 JUCE_DEPRECATED_ATTRIBUTE
inline int roundDoubleToInt (
double value) noexcept {
return roundToInt (value); }
704 JUCE_DEPRECATED_ATTRIBUTE
inline int roundFloatToInt (
float value) noexcept {
return roundToInt (value); }
707 JUCE_DEPRECATED_ATTRIBUTE
inline int64 abs64 (int64 n) noexcept {
return std::abs (n); }