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.

480 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MATHSFUNCTIONS_JUCEHEADER__
  19. #define __JUCE_MATHSFUNCTIONS_JUCEHEADER__
  20. //==============================================================================
  21. /*
  22. This file sets up some handy mathematical typdefs and functions.
  23. */
  24. //==============================================================================
  25. // Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
  26. /** A platform-independent 8-bit signed integer type. */
  27. typedef signed char int8;
  28. /** A platform-independent 8-bit unsigned integer type. */
  29. typedef unsigned char uint8;
  30. /** A platform-independent 16-bit signed integer type. */
  31. typedef signed short int16;
  32. /** A platform-independent 16-bit unsigned integer type. */
  33. typedef unsigned short uint16;
  34. /** A platform-independent 32-bit signed integer type. */
  35. typedef signed int int32;
  36. /** A platform-independent 32-bit unsigned integer type. */
  37. typedef unsigned int uint32;
  38. #if JUCE_MSVC
  39. /** A platform-independent 64-bit integer type. */
  40. typedef __int64 int64;
  41. /** A platform-independent 64-bit unsigned integer type. */
  42. typedef unsigned __int64 uint64;
  43. /** A platform-independent macro for writing 64-bit literals, needed because
  44. different compilers have different syntaxes for this.
  45. E.g. writing literal64bit (0x1000000000) will translate to 0x1000000000LL for
  46. GCC, or 0x1000000000 for MSVC.
  47. */
  48. #define literal64bit(longLiteral) ((__int64) longLiteral)
  49. #else
  50. /** A platform-independent 64-bit integer type. */
  51. typedef long long int64;
  52. /** A platform-independent 64-bit unsigned integer type. */
  53. typedef unsigned long long uint64;
  54. /** A platform-independent macro for writing 64-bit literals, needed because
  55. different compilers have different syntaxes for this.
  56. E.g. writing literal64bit (0x1000000000) will translate to 0x1000000000LL for
  57. GCC, or 0x1000000000 for MSVC.
  58. */
  59. #define literal64bit(longLiteral) (longLiteral##LL)
  60. #endif
  61. #if JUCE_64BIT
  62. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  63. typedef int64 pointer_sized_int;
  64. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  65. typedef uint64 pointer_sized_uint;
  66. #elif JUCE_MSVC && ! JUCE_VC6
  67. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  68. typedef _W64 int pointer_sized_int;
  69. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  70. typedef _W64 unsigned int pointer_sized_uint;
  71. #else
  72. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  73. typedef int pointer_sized_int;
  74. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  75. typedef unsigned int pointer_sized_uint;
  76. #endif
  77. //==============================================================================
  78. // Some indispensible min/max functions
  79. /** Returns the larger of two values. */
  80. template <typename Type>
  81. inline Type jmax (const Type a, const Type b) { return (a < b) ? b : a; }
  82. /** Returns the larger of three values. */
  83. template <typename Type>
  84. inline Type jmax (const Type a, const Type b, const Type c) { return (a < b) ? ((b < c) ? c : b) : ((a < c) ? c : a); }
  85. /** Returns the larger of four values. */
  86. template <typename Type>
  87. inline Type jmax (const Type a, const Type b, const Type c, const Type d) { return jmax (a, jmax (b, c, d)); }
  88. /** Returns the smaller of two values. */
  89. template <typename Type>
  90. inline Type jmin (const Type a, const Type b) { return (b < a) ? b : a; }
  91. /** Returns the smaller of three values. */
  92. template <typename Type>
  93. inline Type jmin (const Type a, const Type b, const Type c) { return (b < a) ? ((c < b) ? c : b) : ((c < a) ? c : a); }
  94. /** Returns the smaller of four values. */
  95. template <typename Type>
  96. inline Type jmin (const Type a, const Type b, const Type c, const Type d) { return jmin (a, jmin (b, c, d)); }
  97. /** Scans an array of values, returning the minimum value that it contains. */
  98. template <typename Type>
  99. const Type findMinimum (const Type* data, int numValues)
  100. {
  101. if (numValues <= 0)
  102. return Type();
  103. Type result (*data++);
  104. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  105. {
  106. const Type& v = *data++;
  107. if (v < result) result = v;
  108. }
  109. return result;
  110. }
  111. /** Scans an array of values, returning the minimum value that it contains. */
  112. template <typename Type>
  113. const Type findMaximum (const Type* values, int numValues)
  114. {
  115. if (numValues <= 0)
  116. return Type();
  117. Type result (*values++);
  118. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  119. {
  120. const Type& v = *values++;
  121. if (result > v) result = v;
  122. }
  123. return result;
  124. }
  125. /** Scans an array of values, returning the minimum and maximum values that it contains. */
  126. template <typename Type>
  127. void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
  128. {
  129. if (numValues <= 0)
  130. {
  131. lowest = Type();
  132. highest = Type();
  133. }
  134. else
  135. {
  136. Type mn (*values++);
  137. Type mx (mn);
  138. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  139. {
  140. const Type& v = *values++;
  141. if (mx < v) mx = v;
  142. if (v < mn) mn = v;
  143. }
  144. lowest = mn;
  145. highest = mx;
  146. }
  147. }
  148. //==============================================================================
  149. /** Constrains a value to keep it within a given range.
  150. This will check that the specified value lies between the lower and upper bounds
  151. specified, and if not, will return the nearest value that would be in-range. Effectively,
  152. it's like calling jmax (lowerLimit, jmin (upperLimit, value)).
  153. Note that it expects that lowerLimit <= upperLimit. If this isn't true,
  154. the results will be unpredictable.
  155. @param lowerLimit the minimum value to return
  156. @param upperLimit the maximum value to return
  157. @param valueToConstrain the value to try to return
  158. @returns the closest value to valueToConstrain which lies between lowerLimit
  159. and upperLimit (inclusive)
  160. @see jlimit0To, jmin, jmax
  161. */
  162. template <typename Type>
  163. inline Type jlimit (const Type lowerLimit,
  164. const Type upperLimit,
  165. const Type valueToConstrain) noexcept
  166. {
  167. jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
  168. return (valueToConstrain < lowerLimit) ? lowerLimit
  169. : ((upperLimit < valueToConstrain) ? upperLimit
  170. : valueToConstrain);
  171. }
  172. /** Returns true if a value is at least zero, and also below a specified upper limit.
  173. This is basically a quicker way to write:
  174. @code valueToTest >= 0 && valueToTest < upperLimit
  175. @endcode
  176. */
  177. template <typename Type>
  178. inline bool isPositiveAndBelow (Type valueToTest, Type upperLimit) noexcept
  179. {
  180. jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
  181. return Type() <= valueToTest && valueToTest < upperLimit;
  182. }
  183. #if ! JUCE_VC6
  184. template <>
  185. inline bool isPositiveAndBelow (const int valueToTest, const int upperLimit) noexcept
  186. {
  187. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  188. return static_cast <unsigned int> (valueToTest) < static_cast <unsigned int> (upperLimit);
  189. }
  190. #endif
  191. /** Returns true if a value is at least zero, and also less than or equal to a specified upper limit.
  192. This is basically a quicker way to write:
  193. @code valueToTest >= 0 && valueToTest <= upperLimit
  194. @endcode
  195. */
  196. template <typename Type>
  197. inline bool isPositiveAndNotGreaterThan (Type valueToTest, Type upperLimit) noexcept
  198. {
  199. jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
  200. return Type() <= valueToTest && valueToTest <= upperLimit;
  201. }
  202. #if ! JUCE_VC6
  203. template <>
  204. inline bool isPositiveAndNotGreaterThan (const int valueToTest, const int upperLimit) noexcept
  205. {
  206. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  207. return static_cast <unsigned int> (valueToTest) <= static_cast <unsigned int> (upperLimit);
  208. }
  209. #endif
  210. //==============================================================================
  211. /** Handy function to swap two values. */
  212. template <typename Type>
  213. inline void swapVariables (Type& variable1, Type& variable2)
  214. {
  215. std::swap (variable1, variable2);
  216. }
  217. #if JUCE_VC6
  218. #define numElementsInArray(X) (sizeof((X)) / sizeof(0[X]))
  219. #else
  220. /** Handy function for getting the number of elements in a simple const C array.
  221. E.g.
  222. @code
  223. static int myArray[] = { 1, 2, 3 };
  224. int numElements = numElementsInArray (myArray) // returns 3
  225. @endcode
  226. */
  227. template <typename Type, int N>
  228. inline int numElementsInArray (Type (&array)[N])
  229. {
  230. (void) array; // (required to avoid a spurious warning in MS compilers)
  231. (void) sizeof (0[array]); // This line should cause an error if you pass an object with a user-defined subscript operator
  232. return N;
  233. }
  234. #endif
  235. //==============================================================================
  236. // Some useful maths functions that aren't always present with all compilers and build settings.
  237. /** Using juce_hypot is easier than dealing with the different types of hypot function
  238. that are provided by the various platforms and compilers. */
  239. template <typename Type>
  240. inline Type juce_hypot (Type a, Type b) noexcept
  241. {
  242. #if JUCE_WINDOWS
  243. return static_cast <Type> (_hypot (a, b));
  244. #else
  245. return static_cast <Type> (hypot (a, b));
  246. #endif
  247. }
  248. /** 64-bit abs function. */
  249. inline int64 abs64 (const int64 n) noexcept
  250. {
  251. return (n >= 0) ? n : -n;
  252. }
  253. /** This templated negate function will negate pointers as well as integers */
  254. template <typename Type>
  255. inline Type juce_negate (Type n) noexcept
  256. {
  257. return sizeof (Type) == 1 ? (Type) -(signed char) n
  258. : (sizeof (Type) == 2 ? (Type) -(short) n
  259. : (sizeof (Type) == 4 ? (Type) -(int) n
  260. : ((Type) -(int64) n)));
  261. }
  262. /** This templated negate function will negate pointers as well as integers */
  263. template <typename Type>
  264. inline Type* juce_negate (Type* n) noexcept
  265. {
  266. return (Type*) -(pointer_sized_int) n;
  267. }
  268. //==============================================================================
  269. /** A predefined value for Pi, at double-precision.
  270. @see float_Pi
  271. */
  272. const double double_Pi = 3.1415926535897932384626433832795;
  273. /** A predefined value for Pi, at sngle-precision.
  274. @see double_Pi
  275. */
  276. const float float_Pi = 3.14159265358979323846f;
  277. //==============================================================================
  278. /** The isfinite() method seems to vary between platforms, so this is a
  279. platform-independent function for it.
  280. */
  281. template <typename FloatingPointType>
  282. inline bool juce_isfinite (FloatingPointType value)
  283. {
  284. #if JUCE_WINDOWS
  285. return _finite (value);
  286. #elif JUCE_ANDROID
  287. return isfinite (value);
  288. #else
  289. return std::isfinite (value);
  290. #endif
  291. }
  292. //==============================================================================
  293. /** Fast floating-point-to-integer conversion.
  294. This is faster than using the normal c++ cast to convert a float to an int, and
  295. it will round the value to the nearest integer, rather than rounding it down
  296. like the normal cast does.
  297. Note that this routine gets its speed at the expense of some accuracy, and when
  298. rounding values whose floating point component is exactly 0.5, odd numbers and
  299. even numbers will be rounded up or down differently.
  300. */
  301. template <typename FloatType>
  302. inline int roundToInt (const FloatType value) noexcept
  303. {
  304. union { int asInt[2]; double asDouble; } n;
  305. n.asDouble = ((double) value) + 6755399441055744.0;
  306. #if JUCE_BIG_ENDIAN
  307. return n.asInt [1];
  308. #else
  309. return n.asInt [0];
  310. #endif
  311. }
  312. /** Fast floating-point-to-integer conversion.
  313. This is a slightly slower and slightly more accurate version of roundDoubleToInt(). It works
  314. fine for values above zero, but negative numbers are rounded the wrong way.
  315. */
  316. inline int roundToIntAccurate (const double value) noexcept
  317. {
  318. return roundToInt (value + 1.5e-8);
  319. }
  320. /** Fast floating-point-to-integer conversion.
  321. This is faster than using the normal c++ cast to convert a double to an int, and
  322. it will round the value to the nearest integer, rather than rounding it down
  323. like the normal cast does.
  324. Note that this routine gets its speed at the expense of some accuracy, and when
  325. rounding values whose floating point component is exactly 0.5, odd numbers and
  326. even numbers will be rounded up or down differently. For a more accurate conversion,
  327. see roundDoubleToIntAccurate().
  328. */
  329. inline int roundDoubleToInt (const double value) noexcept
  330. {
  331. return roundToInt (value);
  332. }
  333. /** Fast floating-point-to-integer conversion.
  334. This is faster than using the normal c++ cast to convert a float to an int, and
  335. it will round the value to the nearest integer, rather than rounding it down
  336. like the normal cast does.
  337. Note that this routine gets its speed at the expense of some accuracy, and when
  338. rounding values whose floating point component is exactly 0.5, odd numbers and
  339. even numbers will be rounded up or down differently.
  340. */
  341. inline int roundFloatToInt (const float value) noexcept
  342. {
  343. return roundToInt (value);
  344. }
  345. //==============================================================================
  346. #if (JUCE_INTEL && JUCE_32BIT) || defined (DOXYGEN)
  347. /** This macro can be applied to a float variable to check whether it contains a denormalised
  348. value, and to normalise it if necessary.
  349. On CPUs that aren't vulnerable to denormalisation problems, this will have no effect.
  350. */
  351. #define JUCE_UNDENORMALISE(x) x += 1.0f; x -= 1.0f;
  352. #else
  353. #define JUCE_UNDENORMALISE(x)
  354. #endif
  355. //==============================================================================
  356. /** This namespace contains a few template classes for helping work out class type variations.
  357. */
  358. namespace TypeHelpers
  359. {
  360. #if JUCE_VC8_OR_EARLIER
  361. #define PARAMETER_TYPE(type) const type&
  362. #else
  363. /** The ParameterType struct is used to find the best type to use when passing some kind
  364. of object as a parameter.
  365. Of course, this is only likely to be useful in certain esoteric template situations.
  366. Because "typename TypeHelpers::ParameterType<SomeClass>::type" is a bit of a mouthful, there's
  367. a PARAMETER_TYPE(SomeClass) macro that you can use to get the same effect.
  368. E.g. "myFunction (PARAMETER_TYPE (int), PARAMETER_TYPE (MyObject))"
  369. would evaluate to "myfunction (int, const MyObject&)", keeping any primitive types as
  370. pass-by-value, but passing objects as a const reference, to avoid copying.
  371. */
  372. template <typename Type> struct ParameterType { typedef const Type& type; };
  373. #if ! DOXYGEN
  374. template <typename Type> struct ParameterType <Type&> { typedef Type& type; };
  375. template <typename Type> struct ParameterType <Type*> { typedef Type* type; };
  376. template <> struct ParameterType <char> { typedef char type; };
  377. template <> struct ParameterType <unsigned char> { typedef unsigned char type; };
  378. template <> struct ParameterType <short> { typedef short type; };
  379. template <> struct ParameterType <unsigned short> { typedef unsigned short type; };
  380. template <> struct ParameterType <int> { typedef int type; };
  381. template <> struct ParameterType <unsigned int> { typedef unsigned int type; };
  382. template <> struct ParameterType <long> { typedef long type; };
  383. template <> struct ParameterType <unsigned long> { typedef unsigned long type; };
  384. template <> struct ParameterType <int64> { typedef int64 type; };
  385. template <> struct ParameterType <uint64> { typedef uint64 type; };
  386. template <> struct ParameterType <bool> { typedef bool type; };
  387. template <> struct ParameterType <float> { typedef float type; };
  388. template <> struct ParameterType <double> { typedef double type; };
  389. #endif
  390. /** A helpful macro to simplify the use of the ParameterType template.
  391. @see ParameterType
  392. */
  393. #define PARAMETER_TYPE(a) typename TypeHelpers::ParameterType<a>::type
  394. #endif
  395. }
  396. //==============================================================================
  397. #endif // __JUCE_MATHSFUNCTIONS_JUCEHEADER__