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.

475 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. /** A platform-independent unicode character type. */
  78. typedef wchar_t juce_wchar;
  79. //==============================================================================
  80. // Some indispensible min/max functions
  81. /** Returns the larger of two values. */
  82. template <typename Type>
  83. inline Type jmax (const Type a, const Type b) { return (a < b) ? b : a; }
  84. /** Returns the larger of three values. */
  85. template <typename Type>
  86. inline Type jmax (const Type a, const Type b, const Type c) { return (a < b) ? ((b < c) ? c : b) : ((a < c) ? c : a); }
  87. /** Returns the larger of four values. */
  88. template <typename Type>
  89. inline Type jmax (const Type a, const Type b, const Type c, const Type d) { return jmax (a, jmax (b, c, d)); }
  90. /** Returns the smaller of two values. */
  91. template <typename Type>
  92. inline Type jmin (const Type a, const Type b) { return (b < a) ? b : a; }
  93. /** Returns the smaller of three values. */
  94. template <typename Type>
  95. inline Type jmin (const Type a, const Type b, const Type c) { return (b < a) ? ((c < b) ? c : b) : ((c < a) ? c : a); }
  96. /** Returns the smaller of four values. */
  97. template <typename Type>
  98. inline Type jmin (const Type a, const Type b, const Type c, const Type d) { return jmin (a, jmin (b, c, d)); }
  99. /** Scans an array of values, returning the minimum value that it contains. */
  100. template <typename Type>
  101. const Type findMinimum (const Type* data, int numValues)
  102. {
  103. if (numValues <= 0)
  104. return Type();
  105. Type result (*data++);
  106. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  107. {
  108. const Type& v = *data++;
  109. if (v < result) result = v;
  110. }
  111. return result;
  112. }
  113. /** Scans an array of values, returning the minimum value that it contains. */
  114. template <typename Type>
  115. const Type findMaximum (const Type* values, int numValues)
  116. {
  117. if (numValues <= 0)
  118. return Type();
  119. Type result (*values++);
  120. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  121. {
  122. const Type& v = *values++;
  123. if (result > v) result = v;
  124. }
  125. return result;
  126. }
  127. /** Scans an array of values, returning the minimum and maximum values that it contains. */
  128. template <typename Type>
  129. void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
  130. {
  131. if (numValues <= 0)
  132. {
  133. lowest = Type();
  134. highest = Type();
  135. }
  136. else
  137. {
  138. Type mn (*values++);
  139. Type mx (mn);
  140. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  141. {
  142. const Type& v = *values++;
  143. if (mx < v) mx = v;
  144. if (v < mn) mn = v;
  145. }
  146. lowest = mn;
  147. highest = mx;
  148. }
  149. }
  150. //==============================================================================
  151. /** Constrains a value to keep it within a given range.
  152. This will check that the specified value lies between the lower and upper bounds
  153. specified, and if not, will return the nearest value that would be in-range. Effectively,
  154. it's like calling jmax (lowerLimit, jmin (upperLimit, value)).
  155. Note that it expects that lowerLimit <= upperLimit. If this isn't true,
  156. the results will be unpredictable.
  157. @param lowerLimit the minimum value to return
  158. @param upperLimit the maximum value to return
  159. @param valueToConstrain the value to try to return
  160. @returns the closest value to valueToConstrain which lies between lowerLimit
  161. and upperLimit (inclusive)
  162. @see jlimit0To, jmin, jmax
  163. */
  164. template <typename Type>
  165. inline Type jlimit (const Type lowerLimit,
  166. const Type upperLimit,
  167. const Type valueToConstrain) throw()
  168. {
  169. jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
  170. return (valueToConstrain < lowerLimit) ? lowerLimit
  171. : ((upperLimit < valueToConstrain) ? upperLimit
  172. : valueToConstrain);
  173. }
  174. /** Returns true if a value is at least zero, and also below a specified upper limit.
  175. This is basically a quicker way to write:
  176. @code valueToTest >= 0 && valueToTest < upperLimit
  177. @endcode
  178. */
  179. template <typename Type>
  180. inline bool isPositiveAndBelow (Type valueToTest, Type upperLimit) throw()
  181. {
  182. jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
  183. return Type() <= valueToTest && valueToTest < upperLimit;
  184. }
  185. #if ! JUCE_VC6
  186. template <>
  187. inline bool isPositiveAndBelow (const int valueToTest, const int upperLimit) throw()
  188. {
  189. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  190. return static_cast <unsigned int> (valueToTest) < static_cast <unsigned int> (upperLimit);
  191. }
  192. #endif
  193. /** Returns true if a value is at least zero, and also less than or equal to a specified upper limit.
  194. This is basically a quicker way to write:
  195. @code valueToTest >= 0 && valueToTest <= upperLimit
  196. @endcode
  197. */
  198. template <typename Type>
  199. inline bool isPositiveAndNotGreaterThan (Type valueToTest, Type upperLimit) throw()
  200. {
  201. jassert (Type() <= upperLimit); // makes no sense to call this if the upper limit is itself below zero..
  202. return Type() <= valueToTest && valueToTest <= upperLimit;
  203. }
  204. #if ! JUCE_VC6
  205. template <>
  206. inline bool isPositiveAndNotGreaterThan (const int valueToTest, const int upperLimit) throw()
  207. {
  208. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  209. return static_cast <unsigned int> (valueToTest) <= static_cast <unsigned int> (upperLimit);
  210. }
  211. #endif
  212. //==============================================================================
  213. /** Handy function to swap two values over.
  214. */
  215. template <typename Type>
  216. inline void swapVariables (Type& variable1, Type& variable2)
  217. {
  218. const Type tempVal = variable1;
  219. variable1 = variable2;
  220. variable2 = tempVal;
  221. }
  222. #if JUCE_VC6
  223. #define numElementsInArray(X) (sizeof((X)) / sizeof(0[X]))
  224. #else
  225. /** Handy function for getting the number of elements in a simple const C array.
  226. E.g.
  227. @code
  228. static int myArray[] = { 1, 2, 3 };
  229. int numElements = numElementsInArray (myArray) // returns 3
  230. @endcode
  231. */
  232. template <typename Type, int N>
  233. inline int numElementsInArray (Type (&array)[N])
  234. {
  235. (void) array; // (required to avoid a spurious warning in MS compilers)
  236. (void) sizeof (0[array]); // This line should cause an error if you pass an object with a user-defined subscript operator
  237. return N;
  238. }
  239. #endif
  240. //==============================================================================
  241. // Some useful maths functions that aren't always present with all compilers and build settings.
  242. /** Using juce_hypot is easier than dealing with the different types of hypot function
  243. that are provided by the various platforms and compilers. */
  244. template <typename Type>
  245. inline Type juce_hypot (Type a, Type b) throw()
  246. {
  247. #if JUCE_WINDOWS
  248. return static_cast <Type> (_hypot (a, b));
  249. #else
  250. return static_cast <Type> (hypot (a, b));
  251. #endif
  252. }
  253. /** 64-bit abs function. */
  254. inline int64 abs64 (const int64 n) throw()
  255. {
  256. return (n >= 0) ? n : -n;
  257. }
  258. /** This templated negate function will negate pointers as well as integers */
  259. template <typename Type>
  260. inline Type juce_negate (Type n) throw()
  261. {
  262. return sizeof (Type) == 1 ? (Type) -(char) n
  263. : (sizeof (Type) == 2 ? (Type) -(short) n
  264. : (sizeof (Type) == 4 ? (Type) -(int) n
  265. : ((Type) -(int64) n)));
  266. }
  267. /** This templated negate function will negate pointers as well as integers */
  268. template <typename Type>
  269. inline Type* juce_negate (Type* n) throw()
  270. {
  271. return (Type*) -(pointer_sized_int) n;
  272. }
  273. //==============================================================================
  274. /** A predefined value for Pi, at double-precision.
  275. @see float_Pi
  276. */
  277. const double double_Pi = 3.1415926535897932384626433832795;
  278. /** A predefined value for Pi, at sngle-precision.
  279. @see double_Pi
  280. */
  281. const float float_Pi = 3.14159265358979323846f;
  282. //==============================================================================
  283. /** The isfinite() method seems to vary between platforms, so this is a
  284. platform-independent function for it.
  285. */
  286. template <typename FloatingPointType>
  287. inline bool juce_isfinite (FloatingPointType value)
  288. {
  289. #if JUCE_WINDOWS
  290. return _finite (value);
  291. #else
  292. return std::isfinite (value);
  293. #endif
  294. }
  295. //==============================================================================
  296. /** Fast floating-point-to-integer conversion.
  297. This is faster than using the normal c++ cast to convert a float to an int, and
  298. it will round the value to the nearest integer, rather than rounding it down
  299. like the normal cast does.
  300. Note that this routine gets its speed at the expense of some accuracy, and when
  301. rounding values whose floating point component is exactly 0.5, odd numbers and
  302. even numbers will be rounded up or down differently.
  303. */
  304. template <typename FloatType>
  305. inline int roundToInt (const FloatType value) throw()
  306. {
  307. union { int asInt[2]; double asDouble; } n;
  308. n.asDouble = ((double) value) + 6755399441055744.0;
  309. #if JUCE_BIG_ENDIAN
  310. return n.asInt [1];
  311. #else
  312. return n.asInt [0];
  313. #endif
  314. }
  315. /** Fast floating-point-to-integer conversion.
  316. This is a slightly slower and slightly more accurate version of roundDoubleToInt(). It works
  317. fine for values above zero, but negative numbers are rounded the wrong way.
  318. */
  319. inline int roundToIntAccurate (const double value) throw()
  320. {
  321. return roundToInt (value + 1.5e-8);
  322. }
  323. /** Fast floating-point-to-integer conversion.
  324. This is faster than using the normal c++ cast to convert a double to an int, and
  325. it will round the value to the nearest integer, rather than rounding it down
  326. like the normal cast does.
  327. Note that this routine gets its speed at the expense of some accuracy, and when
  328. rounding values whose floating point component is exactly 0.5, odd numbers and
  329. even numbers will be rounded up or down differently. For a more accurate conversion,
  330. see roundDoubleToIntAccurate().
  331. */
  332. inline int roundDoubleToInt (const double value) throw()
  333. {
  334. return roundToInt (value);
  335. }
  336. /** Fast floating-point-to-integer conversion.
  337. This is faster than using the normal c++ cast to convert a float to an int, and
  338. it will round the value to the nearest integer, rather than rounding it down
  339. like the normal cast does.
  340. Note that this routine gets its speed at the expense of some accuracy, and when
  341. rounding values whose floating point component is exactly 0.5, odd numbers and
  342. even numbers will be rounded up or down differently.
  343. */
  344. inline int roundFloatToInt (const float value) throw()
  345. {
  346. return roundToInt (value);
  347. }
  348. //==============================================================================
  349. /** This namespace contains a few template classes for helping work out class type variations.
  350. */
  351. namespace TypeHelpers
  352. {
  353. #if JUCE_VC8_OR_EARLIER
  354. #define PARAMETER_TYPE(a) a
  355. #else
  356. /** The ParameterType struct is used to find the best type to use when passing some kind
  357. of object as a parameter.
  358. Of course, this is only likely to be useful in certain esoteric template situations.
  359. Because "typename TypeHelpers::ParameterType<SomeClass>::type" is a bit of a mouthful, there's
  360. a PARAMETER_TYPE(SomeClass) macro that you can use to get the same effect.
  361. E.g. "myFunction (PARAMETER_TYPE (int), PARAMETER_TYPE (MyObject))"
  362. would evaluate to "myfunction (int, const MyObject&)", keeping any primitive types as
  363. pass-by-value, but passing objects as a const reference, to avoid copying.
  364. */
  365. template <typename Type> struct ParameterType { typedef const Type& type; };
  366. #if ! DOXYGEN
  367. template <typename Type> struct ParameterType <Type&> { typedef Type& type; };
  368. template <typename Type> struct ParameterType <Type*> { typedef Type* type; };
  369. template <> struct ParameterType <char> { typedef char type; };
  370. template <> struct ParameterType <unsigned char> { typedef unsigned char type; };
  371. template <> struct ParameterType <short> { typedef short type; };
  372. template <> struct ParameterType <unsigned short> { typedef unsigned short type; };
  373. template <> struct ParameterType <int> { typedef int type; };
  374. template <> struct ParameterType <unsigned int> { typedef unsigned int type; };
  375. template <> struct ParameterType <long> { typedef long type; };
  376. template <> struct ParameterType <unsigned long> { typedef unsigned long type; };
  377. template <> struct ParameterType <int64> { typedef int64 type; };
  378. template <> struct ParameterType <uint64> { typedef uint64 type; };
  379. template <> struct ParameterType <bool> { typedef bool type; };
  380. template <> struct ParameterType <float> { typedef float type; };
  381. template <> struct ParameterType <double> { typedef double type; };
  382. #endif
  383. /** A helpful macro to simplify the use of the ParameterType template.
  384. @see ParameterType
  385. */
  386. #define PARAMETER_TYPE(a) typename TypeHelpers::ParameterType<a>::type
  387. #endif
  388. }
  389. //==============================================================================
  390. #endif // __JUCE_MATHSFUNCTIONS_JUCEHEADER__