The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /*
  21. This file sets up some handy mathematical typdefs and functions.
  22. */
  23. //==============================================================================
  24. // Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
  25. /** A platform-independent 8-bit signed integer type. */
  26. using int8 = signed char;
  27. /** A platform-independent 8-bit unsigned integer type. */
  28. using uint8 = unsigned char;
  29. /** A platform-independent 16-bit signed integer type. */
  30. using int16 = signed short;
  31. /** A platform-independent 16-bit unsigned integer type. */
  32. using uint16 = unsigned short;
  33. /** A platform-independent 32-bit signed integer type. */
  34. using int32 = signed int;
  35. /** A platform-independent 32-bit unsigned integer type. */
  36. using uint32 = unsigned int;
  37. #if JUCE_MSVC
  38. /** A platform-independent 64-bit integer type. */
  39. using int64 = __int64;
  40. /** A platform-independent 64-bit unsigned integer type. */
  41. using uint64 = unsigned __int64;
  42. #else
  43. /** A platform-independent 64-bit integer type. */
  44. using int64 = long long;
  45. /** A platform-independent 64-bit unsigned integer type. */
  46. using uint64 = unsigned long long;
  47. #endif
  48. #ifndef DOXYGEN
  49. /** A macro for creating 64-bit literals.
  50. Historically, this was needed to support portability with MSVC6, and is kept here
  51. so that old code will still compile, but nowadays every compiler will support the
  52. LL and ULL suffixes, so you should use those in preference to this macro.
  53. */
  54. #define literal64bit(longLiteral) (longLiteral##LL)
  55. #endif
  56. #if JUCE_64BIT
  57. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  58. using pointer_sized_int = int64;
  59. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  60. using pointer_sized_uint = uint64;
  61. #elif JUCE_MSVC
  62. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  63. using pointer_sized_int = _W64 int;
  64. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  65. using pointer_sized_uint = _W64 unsigned int;
  66. #else
  67. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  68. using pointer_sized_int = int;
  69. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  70. using pointer_sized_uint = unsigned int;
  71. #endif
  72. #if JUCE_WINDOWS && ! JUCE_MINGW
  73. using ssize_t = pointer_sized_int;
  74. #endif
  75. /** Returns true if the two numbers are approximately equal. This is useful for floating-point
  76. and double comparisons.
  77. */
  78. template <typename Type>
  79. bool approximatelyEqual (Type a, Type b) noexcept
  80. {
  81. return std::abs (a - b) <= (std::numeric_limits<Type>::epsilon() * std::max (a, b))
  82. || std::abs (a - b) < std::numeric_limits<Type>::min();
  83. }
  84. /** Equivalent to operator==, but suppresses float-equality warnings.
  85. This allows code to be explicit about float-equality checks that are known to have the correct
  86. semantics.
  87. */
  88. template <typename Type>
  89. constexpr bool exactlyEqual (Type a, Type b)
  90. {
  91. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wfloat-equal")
  92. return a == b;
  93. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  94. }
  95. //==============================================================================
  96. // Some indispensable min/max functions
  97. /** Returns the larger of two values. */
  98. template <typename Type>
  99. constexpr Type jmax (Type a, Type b) { return a < b ? b : a; }
  100. /** Returns the larger of three values. */
  101. template <typename Type>
  102. constexpr Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }
  103. /** Returns the larger of four values. */
  104. template <typename Type>
  105. constexpr Type jmax (Type a, Type b, Type c, Type d) { return jmax (a, jmax (b, c, d)); }
  106. /** Returns the smaller of two values. */
  107. template <typename Type>
  108. constexpr Type jmin (Type a, Type b) { return b < a ? b : a; }
  109. /** Returns the smaller of three values. */
  110. template <typename Type>
  111. constexpr Type jmin (Type a, Type b, Type c) { return b < a ? (c < b ? c : b) : (c < a ? c : a); }
  112. /** Returns the smaller of four values. */
  113. template <typename Type>
  114. constexpr Type jmin (Type a, Type b, Type c, Type d) { return jmin (a, jmin (b, c, d)); }
  115. /** Remaps a normalised value (between 0 and 1) to a target range.
  116. This effectively returns (targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin)).
  117. */
  118. template <typename Type>
  119. constexpr Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
  120. {
  121. return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
  122. }
  123. /** Remaps a value from a source range to a target range. */
  124. template <typename Type>
  125. Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
  126. {
  127. jassert (! approximatelyEqual (sourceRangeMax, sourceRangeMin)); // mapping from a range of zero will produce NaN!
  128. return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
  129. }
  130. /** Remaps a normalised value (between 0 and 1) to a logarithmic target range.
  131. The entire target range must be greater than zero.
  132. @see mapFromLog10
  133. @code
  134. mapToLog10 (0.5, 0.4, 40.0) == 4.0
  135. @endcode
  136. */
  137. template <typename Type>
  138. Type mapToLog10 (Type value0To1, Type logRangeMin, Type logRangeMax)
  139. {
  140. jassert (logRangeMin > 0);
  141. jassert (logRangeMax > 0);
  142. auto logMin = std::log10 (logRangeMin);
  143. auto logMax = std::log10 (logRangeMax);
  144. return std::pow ((Type) 10.0, value0To1 * (logMax - logMin) + logMin);
  145. }
  146. /** Remaps a logarithmic value in a target range to a normalised value (between 0 and 1).
  147. The entire target range must be greater than zero.
  148. @see mapToLog10
  149. @code
  150. mapFromLog10 (4.0, 0.4, 40.0) == 0.5
  151. @endcode
  152. */
  153. template <typename Type>
  154. Type mapFromLog10 (Type valueInLogRange, Type logRangeMin, Type logRangeMax)
  155. {
  156. jassert (logRangeMin > 0);
  157. jassert (logRangeMax > 0);
  158. auto logMin = std::log10 (logRangeMin);
  159. auto logMax = std::log10 (logRangeMax);
  160. return (std::log10 (valueInLogRange) - logMin) / (logMax - logMin);
  161. }
  162. /** Scans an array of values, returning the minimum value that it contains. */
  163. template <typename Type, typename Size>
  164. Type findMinimum (const Type* data, Size numValues)
  165. {
  166. if (numValues <= 0)
  167. return Type (0);
  168. auto result = *data++;
  169. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  170. {
  171. auto v = *data++;
  172. if (v < result)
  173. result = v;
  174. }
  175. return result;
  176. }
  177. /** Scans an array of values, returning the maximum value that it contains. */
  178. template <typename Type, typename Size>
  179. Type findMaximum (const Type* values, Size numValues)
  180. {
  181. if (numValues <= 0)
  182. return Type (0);
  183. auto result = *values++;
  184. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  185. {
  186. auto v = *values++;
  187. if (result < v)
  188. result = v;
  189. }
  190. return result;
  191. }
  192. /** Scans an array of values, returning the minimum and maximum values that it contains. */
  193. template <typename Type>
  194. void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
  195. {
  196. if (numValues <= 0)
  197. {
  198. lowest = Type (0);
  199. highest = Type (0);
  200. }
  201. else
  202. {
  203. auto mn = *values++;
  204. auto mx = mn;
  205. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  206. {
  207. auto v = *values++;
  208. if (mx < v) mx = v;
  209. if (v < mn) mn = v;
  210. }
  211. lowest = mn;
  212. highest = mx;
  213. }
  214. }
  215. //==============================================================================
  216. /** Constrains a value to keep it within a given range.
  217. This will check that the specified value lies between the lower and upper bounds
  218. specified, and if not, will return the nearest value that would be in-range. Effectively,
  219. it's like calling jmax (lowerLimit, jmin (upperLimit, value)).
  220. Note that it expects that lowerLimit <= upperLimit. If this isn't true,
  221. the results will be unpredictable.
  222. @param lowerLimit the minimum value to return
  223. @param upperLimit the maximum value to return
  224. @param valueToConstrain the value to try to return
  225. @returns the closest value to valueToConstrain which lies between lowerLimit
  226. and upperLimit (inclusive)
  227. @see jmin, jmax, jmap
  228. */
  229. template <typename Type>
  230. Type jlimit (Type lowerLimit,
  231. Type upperLimit,
  232. Type valueToConstrain) noexcept
  233. {
  234. jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
  235. return valueToConstrain < lowerLimit ? lowerLimit
  236. : (upperLimit < valueToConstrain ? upperLimit
  237. : valueToConstrain);
  238. }
  239. /** Returns true if a value is at least zero, and also below a specified upper limit.
  240. This is basically a quicker way to write:
  241. @code valueToTest >= 0 && valueToTest < upperLimit
  242. @endcode
  243. */
  244. template <typename Type1, typename Type2>
  245. bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit) noexcept
  246. {
  247. jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
  248. return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
  249. }
  250. template <typename Type>
  251. bool isPositiveAndBelow (int valueToTest, Type upperLimit) noexcept
  252. {
  253. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  254. return static_cast<unsigned int> (valueToTest) < static_cast<unsigned int> (upperLimit);
  255. }
  256. /** Returns true if a value is at least zero, and also less than or equal to a specified upper limit.
  257. This is basically a quicker way to write:
  258. @code valueToTest >= 0 && valueToTest <= upperLimit
  259. @endcode
  260. */
  261. template <typename Type1, typename Type2>
  262. bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit) noexcept
  263. {
  264. jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
  265. return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
  266. }
  267. template <typename Type>
  268. bool isPositiveAndNotGreaterThan (int valueToTest, Type upperLimit) noexcept
  269. {
  270. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  271. return static_cast<unsigned int> (valueToTest) <= static_cast<unsigned int> (upperLimit);
  272. }
  273. /** Computes the absolute difference between two values and returns true if it is less than or equal
  274. to a given tolerance, otherwise it returns false.
  275. */
  276. template <typename Type>
  277. bool isWithin (Type a, Type b, Type tolerance) noexcept
  278. {
  279. return std::abs (a - b) <= tolerance;
  280. }
  281. //==============================================================================
  282. /** Handy function for avoiding unused variables warning. */
  283. template <typename... Types>
  284. void ignoreUnused (Types&&...) noexcept {}
  285. /** Handy function for getting the number of elements in a simple const C array.
  286. E.g.
  287. @code
  288. static int myArray[] = { 1, 2, 3 };
  289. int numElements = numElementsInArray (myArray) // returns 3
  290. @endcode
  291. */
  292. template <typename Type, size_t N>
  293. constexpr int numElementsInArray (Type (&)[N]) noexcept { return N; }
  294. //==============================================================================
  295. // Some useful maths functions that aren't always present with all compilers and build settings.
  296. /** Using juce_hypot is easier than dealing with the different types of hypot function
  297. that are provided by the various platforms and compilers. */
  298. template <typename Type>
  299. Type juce_hypot (Type a, Type b) noexcept
  300. {
  301. #if JUCE_MSVC
  302. return static_cast<Type> (_hypot (a, b));
  303. #else
  304. return static_cast<Type> (hypot (a, b));
  305. #endif
  306. }
  307. #ifndef DOXYGEN
  308. template <>
  309. inline float juce_hypot (float a, float b) noexcept
  310. {
  311. #if JUCE_MSVC
  312. return _hypotf (a, b);
  313. #else
  314. return hypotf (a, b);
  315. #endif
  316. }
  317. #endif
  318. //==============================================================================
  319. /** Commonly used mathematical constants
  320. @tags{Core}
  321. */
  322. template <typename FloatType>
  323. struct MathConstants
  324. {
  325. /** A predefined value for Pi */
  326. static constexpr FloatType pi = static_cast<FloatType> (3.141592653589793238L);
  327. /** A predefined value for 2 * Pi */
  328. static constexpr FloatType twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
  329. /** A predefined value for Pi / 2 */
  330. static constexpr FloatType halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
  331. /** A predefined value for Euler's number */
  332. static constexpr FloatType euler = static_cast<FloatType> (2.71828182845904523536L);
  333. /** A predefined value for sqrt(2) */
  334. static constexpr FloatType sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
  335. };
  336. #ifndef DOXYGEN
  337. /** A double-precision constant for pi. */
  338. [[deprecated ("This is deprecated in favour of MathConstants<double>::pi.")]]
  339. const constexpr double double_Pi = MathConstants<double>::pi;
  340. /** A single-precision constant for pi. */
  341. [[deprecated ("This is deprecated in favour of MathConstants<float>::pi.")]]
  342. const constexpr float float_Pi = MathConstants<float>::pi;
  343. #endif
  344. /** Converts an angle in degrees to radians. */
  345. template <typename FloatType>
  346. constexpr FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (MathConstants<FloatType>::pi / FloatType (180)); }
  347. /** Converts an angle in radians to degrees. */
  348. template <typename FloatType>
  349. constexpr FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / MathConstants<FloatType>::pi); }
  350. //==============================================================================
  351. /** The isfinite() method seems to vary between platforms, so this is a
  352. platform-independent function for it.
  353. */
  354. template <typename NumericType>
  355. bool juce_isfinite (NumericType) noexcept
  356. {
  357. return true; // Integer types are always finite
  358. }
  359. template <>
  360. inline bool juce_isfinite (float value) noexcept
  361. {
  362. #if JUCE_WINDOWS && ! JUCE_MINGW
  363. return _finite (value) != 0;
  364. #else
  365. return std::isfinite (value);
  366. #endif
  367. }
  368. template <>
  369. inline bool juce_isfinite (double value) noexcept
  370. {
  371. #if JUCE_WINDOWS && ! JUCE_MINGW
  372. return _finite (value) != 0;
  373. #else
  374. return std::isfinite (value);
  375. #endif
  376. }
  377. //==============================================================================
  378. #if JUCE_MSVC
  379. #pragma optimize ("t", off)
  380. #ifndef __INTEL_COMPILER
  381. #pragma float_control (precise, on, push)
  382. #endif
  383. #endif
  384. /** Fast floating-point-to-integer conversion.
  385. This is faster than using the normal c++ cast to convert a float to an int, and
  386. it will round the value to the nearest integer, rather than rounding it down
  387. like the normal cast does.
  388. Note that this routine gets its speed at the expense of some accuracy, and when
  389. rounding values whose floating point component is exactly 0.5, odd numbers and
  390. even numbers will be rounded up or down differently.
  391. */
  392. template <typename FloatType>
  393. int roundToInt (const FloatType value) noexcept
  394. {
  395. #ifdef __INTEL_COMPILER
  396. #pragma float_control (precise, on, push)
  397. #endif
  398. union { int asInt[2]; double asDouble; } n;
  399. n.asDouble = ((double) value) + 6755399441055744.0;
  400. #if JUCE_BIG_ENDIAN
  401. return n.asInt [1];
  402. #else
  403. return n.asInt [0];
  404. #endif
  405. }
  406. inline int roundToInt (int value) noexcept
  407. {
  408. return value;
  409. }
  410. #if JUCE_MSVC
  411. #ifndef __INTEL_COMPILER
  412. #pragma float_control (pop)
  413. #endif
  414. #pragma optimize ("", on) // resets optimisations to the project defaults
  415. #endif
  416. /** Fast floating-point-to-integer conversion.
  417. This is a slightly slower and slightly more accurate version of roundToInt(). It works
  418. fine for values above zero, but negative numbers are rounded the wrong way.
  419. */
  420. inline int roundToIntAccurate (double value) noexcept
  421. {
  422. #ifdef __INTEL_COMPILER
  423. #pragma float_control (pop)
  424. #endif
  425. return roundToInt (value + 1.5e-8);
  426. }
  427. //==============================================================================
  428. /** Truncates a positive floating-point number to an unsigned int.
  429. This is generally faster than static_cast<unsigned int> (std::floor (x))
  430. but it only works for positive numbers small enough to be represented as an
  431. unsigned int.
  432. */
  433. template <typename FloatType>
  434. unsigned int truncatePositiveToUnsignedInt (FloatType value) noexcept
  435. {
  436. jassert (value >= static_cast<FloatType> (0));
  437. jassert (static_cast<FloatType> (value)
  438. <= static_cast<FloatType> (std::numeric_limits<unsigned int>::max()));
  439. return static_cast<unsigned int> (value);
  440. }
  441. //==============================================================================
  442. /** Returns true if the specified integer is a power-of-two. */
  443. template <typename IntegerType>
  444. constexpr bool isPowerOfTwo (IntegerType value)
  445. {
  446. return (value & (value - 1)) == 0;
  447. }
  448. /** Returns the smallest power-of-two which is equal to or greater than the given integer. */
  449. inline int nextPowerOfTwo (int n) noexcept
  450. {
  451. --n;
  452. n |= (n >> 1);
  453. n |= (n >> 2);
  454. n |= (n >> 4);
  455. n |= (n >> 8);
  456. n |= (n >> 16);
  457. return n + 1;
  458. }
  459. /** Returns the index of the highest set bit in a (non-zero) number.
  460. So for n=3 this would return 1, for n=7 it returns 2, etc.
  461. An input value of 0 is illegal!
  462. */
  463. int findHighestSetBit (uint32 n) noexcept;
  464. /** Returns the number of bits in a 32-bit integer. */
  465. constexpr int countNumberOfBits (uint32 n) noexcept
  466. {
  467. n -= ((n >> 1) & 0x55555555);
  468. n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
  469. n = (((n >> 4) + n) & 0x0f0f0f0f);
  470. n += (n >> 8);
  471. n += (n >> 16);
  472. return (int) (n & 0x3f);
  473. }
  474. /** Returns the number of bits in a 64-bit integer. */
  475. constexpr int countNumberOfBits (uint64 n) noexcept
  476. {
  477. return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
  478. }
  479. /** Performs a modulo operation, but can cope with the dividend being negative.
  480. The divisor must be greater than zero.
  481. */
  482. template <typename IntegerType>
  483. IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
  484. {
  485. jassert (divisor > 0);
  486. dividend %= divisor;
  487. return (dividend < 0) ? (dividend + divisor) : dividend;
  488. }
  489. /** Returns the square of its argument. */
  490. template <typename NumericType>
  491. inline constexpr NumericType square (NumericType n) noexcept
  492. {
  493. return n * n;
  494. }
  495. //==============================================================================
  496. /** Writes a number of bits into a memory buffer at a given bit index.
  497. The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
  498. so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the value would be written
  499. into bits 2-8 of targetBuffer[1], and the upper 5 bits of value into bits 0-5 of targetBuffer[2].
  500. @see readLittleEndianBitsInBuffer
  501. */
  502. void writeLittleEndianBitsInBuffer (void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
  503. /** Reads a number of bits from a buffer at a given bit index.
  504. The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
  505. so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the result would be read
  506. from bits 2-8 of sourceBuffer[1], and the upper 5 bits of the result from bits 0-5 of sourceBuffer[2].
  507. @see writeLittleEndianBitsInBuffer
  508. */
  509. uint32 readLittleEndianBitsInBuffer (const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
  510. //==============================================================================
  511. #if JUCE_INTEL || DOXYGEN
  512. /** This macro can be applied to a float variable to check whether it contains a denormalised
  513. value, and to normalise it if necessary.
  514. On CPUs that aren't vulnerable to denormalisation problems, this will have no effect.
  515. */
  516. #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
  517. #else
  518. #define JUCE_UNDENORMALISE(x)
  519. #endif
  520. //==============================================================================
  521. /** This namespace contains a few template classes for helping work out class type variations.
  522. */
  523. namespace TypeHelpers
  524. {
  525. /** The ParameterType struct is used to find the best type to use when passing some kind
  526. of object as a parameter.
  527. Of course, this is only likely to be useful in certain esoteric template situations.
  528. E.g. "myFunction (typename TypeHelpers::ParameterType<int>::type, typename TypeHelpers::ParameterType<MyObject>::type)"
  529. would evaluate to "myfunction (int, const MyObject&)", keeping any primitive types as
  530. pass-by-value, but passing objects as a const reference, to avoid copying.
  531. @tags{Core}
  532. */
  533. template <typename Type> struct ParameterType { using type = const Type&; };
  534. #ifndef DOXYGEN
  535. template <typename Type> struct ParameterType <Type&> { using type = Type&; };
  536. template <typename Type> struct ParameterType <Type*> { using type = Type*; };
  537. template <> struct ParameterType <char> { using type = char; };
  538. template <> struct ParameterType <unsigned char> { using type = unsigned char; };
  539. template <> struct ParameterType <short> { using type = short; };
  540. template <> struct ParameterType <unsigned short> { using type = unsigned short; };
  541. template <> struct ParameterType <int> { using type = int; };
  542. template <> struct ParameterType <unsigned int> { using type = unsigned int; };
  543. template <> struct ParameterType <long> { using type = long; };
  544. template <> struct ParameterType <unsigned long> { using type = unsigned long; };
  545. template <> struct ParameterType <int64> { using type = int64; };
  546. template <> struct ParameterType <uint64> { using type = uint64; };
  547. template <> struct ParameterType <bool> { using type = bool; };
  548. template <> struct ParameterType <float> { using type = float; };
  549. template <> struct ParameterType <double> { using type = double; };
  550. #endif
  551. /** These templates are designed to take a type, and if it's a double, they return a double
  552. type; for anything else, they return a float type.
  553. @tags{Core}
  554. */
  555. template <typename Type>
  556. using SmallestFloatType = std::conditional_t<std::is_same_v<Type, double>, double, float>;
  557. /** These templates are designed to take an integer type, and return an unsigned int
  558. version with the same size.
  559. @tags{Core}
  560. */
  561. template <int bytes> struct UnsignedTypeWithSize {};
  562. #ifndef DOXYGEN
  563. template <> struct UnsignedTypeWithSize<1> { using type = uint8; };
  564. template <> struct UnsignedTypeWithSize<2> { using type = uint16; };
  565. template <> struct UnsignedTypeWithSize<4> { using type = uint32; };
  566. template <> struct UnsignedTypeWithSize<8> { using type = uint64; };
  567. #endif
  568. }
  569. //==============================================================================
  570. #ifndef DOXYGEN
  571. [[deprecated ("Use roundToInt instead.")]] inline int roundDoubleToInt (double value) noexcept { return roundToInt (value); }
  572. [[deprecated ("Use roundToInt instead.")]] inline int roundFloatToInt (float value) noexcept { return roundToInt (value); }
  573. [[deprecated ("Use std::abs() instead.")]] inline int64 abs64 (int64 n) noexcept { return std::abs (n); }
  574. #endif
  575. } // namespace juce