LMMS
Loading...
Searching...
No Matches
MathsFunctions.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_MATHSFUNCTIONS_H_INCLUDED
27#define WATER_MATHSFUNCTIONS_H_INCLUDED
28
29#include "../water.h"
30
31#include "CarlaUtils.hpp"
32
33#include <algorithm>
34#include <cmath>
35
36namespace water {
37
38//==============================================================================
39/*
40 This file sets up some handy mathematical typdefs and functions.
41*/
42
43//==============================================================================
44// Some indispensable min/max functions
45
47template <typename Type>
48Type jmax (const Type a, const Type b) { return (a < b) ? b : a; }
49
51template <typename Type>
52Type jmax (const Type a, const Type b, const Type c) { return (a < b) ? ((b < c) ? c : b) : ((a < c) ? c : a); }
53
55template <typename Type>
56Type jmax (const Type a, const Type b, const Type c, const Type d) { return jmax (a, jmax (b, c, d)); }
57
59template <typename Type>
60Type jmin (const Type a, const Type b) { return (b < a) ? b : a; }
61
63template <typename Type>
64Type jmin (const Type a, const Type b, const Type c) { return (b < a) ? ((c < b) ? c : b) : ((c < a) ? c : a); }
65
67template <typename Type>
68Type jmin (const Type a, const Type b, const Type c, const Type d) { return jmin (a, jmin (b, c, d)); }
69
73template <typename Type>
74Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
75{
76 return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
77}
78
80template <typename Type>
81Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
82{
83 wassert (sourceRangeMax != sourceRangeMin); // mapping from a range of zero will produce NaN!
84 return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
85}
86
88template <typename Type>
89Type findMinimum (const Type* data, int numValues)
90{
91 if (numValues <= 0)
92 return Type();
93
94 Type result (*data++);
95
96 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
97 {
98 const Type& v = *data++;
99 if (v < result) result = v;
100 }
101
102 return result;
103}
104
106template <typename Type>
107Type findMaximum (const Type* values, int numValues)
108{
109 if (numValues <= 0)
110 return Type();
111
112 Type result (*values++);
113
114 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
115 {
116 const Type& v = *values++;
117 if (result < v) result = v;
118 }
119
120 return result;
121}
122
124template <typename Type>
125void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
126{
127 if (numValues <= 0)
128 {
129 lowest = Type();
130 highest = Type();
131 }
132 else
133 {
134 Type mn (*values++);
135 Type mx (mn);
136
137 while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
138 {
139 const Type& v = *values++;
140
141 if (mx < v) mx = v;
142 if (v < mn) mn = v;
143 }
144
145 lowest = mn;
146 highest = mx;
147 }
148}
149
150
151//==============================================================================
168template <typename Type>
169Type jlimit (const Type lowerLimit,
170 const Type upperLimit,
171 const Type valueToConstrain) noexcept
172{
173 // if these are in the wrong order, results are unpredictable..
174 CARLA_SAFE_ASSERT_RETURN(lowerLimit <= upperLimit, lowerLimit);
175
176 return (valueToConstrain < lowerLimit) ? lowerLimit
177 : ((upperLimit < valueToConstrain) ? upperLimit
178 : valueToConstrain);
179}
180
186template <typename Type>
187bool isPositiveAndBelow (Type valueToTest, Type upperLimit) noexcept
188{
189 // makes no sense to call this if the upper limit is itself below zero..
190 CARLA_SAFE_ASSERT_RETURN(Type() <= upperLimit, false);
191
192 return Type() <= valueToTest && valueToTest < upperLimit;
193}
194
195template <>
196inline bool isPositiveAndBelow (const int valueToTest, const int upperLimit) noexcept
197{
198 // makes no sense to call this if the upper limit is itself below zero..
199 CARLA_SAFE_ASSERT_RETURN(upperLimit >= 0, false);
200
201 return static_cast<unsigned int> (valueToTest) < static_cast<unsigned int> (upperLimit);
202}
203
209template <typename Type>
210bool isPositiveAndNotGreaterThan (Type valueToTest, Type upperLimit) noexcept
211{
212 // makes no sense to call this if the upper limit is itself below zero..
213 CARLA_SAFE_ASSERT_RETURN(Type() <= upperLimit, false);
214
215 return Type() <= valueToTest && valueToTest <= upperLimit;
216}
217
218template <>
219inline bool isPositiveAndNotGreaterThan (const int valueToTest, const int upperLimit) noexcept
220{
221 // makes no sense to call this if the upper limit is itself below zero..
222 CARLA_SAFE_ASSERT_RETURN(upperLimit >= 0, false);
223
224 return static_cast<unsigned int> (valueToTest) <= static_cast<unsigned int> (upperLimit);
225}
226
227//==============================================================================
229template <typename Type>
230void swapVariables (Type& variable1, Type& variable2)
231{
232 std::swap (variable1, variable2);
233}
234
236template <typename Type1>
237void ignoreUnused (const Type1&) noexcept {}
238
239template <typename Type1, typename Type2>
240void ignoreUnused (const Type1&, const Type2&) noexcept {}
241
242template <typename Type1, typename Type2, typename Type3>
243void ignoreUnused (const Type1&, const Type2&, const Type3&) noexcept {}
244
245template <typename Type1, typename Type2, typename Type3, typename Type4>
246void ignoreUnused (const Type1&, const Type2&, const Type3&, const Type4&) noexcept {}
247
256template <typename Type, size_t N>
257size_t numElementsInArray (Type (&array)[N])
258{
259 ignoreUnused (array);
260 (void) sizeof (0[array]); // This line should cause an error if you pass an object with a user-defined subscript operator
261 return N;
262}
263
264//==============================================================================
265// Some useful maths functions that aren't always present with all compilers and build settings.
266
269template <typename Type>
270Type water_hypot (Type a, Type b) noexcept
271{
272 return static_cast<Type> (hypot (a, b));
273}
274
275template <>
276inline float water_hypot (float a, float b) noexcept
277{
278 return hypotf (a, b);
279}
280
282inline int64 abs64 (const int64 n) noexcept
283{
284 return (n >= 0) ? n : -n;
285}
286
287//==============================================================================
291const double double_Pi = 3.1415926535897932384626433832795;
292
296const float float_Pi = 3.14159265358979323846f;
297
298
300inline float degreesToRadians (float degrees) noexcept { return degrees * (float_Pi / 180.0f); }
301
303inline double degreesToRadians (double degrees) noexcept { return degrees * (double_Pi / 180.0); }
304
306inline float radiansToDegrees (float radians) noexcept { return radians * (180.0f / float_Pi); }
307
309inline double radiansToDegrees (double radians) noexcept { return radians * (180.0 / double_Pi); }
310
311
312//==============================================================================
316template <typename NumericType>
317bool water_isfinite (NumericType) noexcept
318{
319 return true; // Integer types are always finite
320}
321
322template <>
323inline bool water_isfinite (float value) noexcept
324{
325 return std::isfinite (value);
326}
327
328template <>
329inline bool water_isfinite (double value) noexcept
330{
331 return std::isfinite (value);
332}
333
334//==============================================================================
345template <typename FloatType>
346int roundToInt (const FloatType value) noexcept
347{
348 union { int asInt[2]; double asDouble; } n;
349 n.asDouble = ((double) value) + 6755399441055744.0;
350
351 #if defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
352 return n.asInt [1];
353 #else
354 return n.asInt [0];
355 #endif
356}
357
358inline int roundToInt (int value) noexcept
359{
360 return value;
361}
362
368inline int roundToIntAccurate (double value) noexcept
369{
370 return roundToInt (value + 1.5e-8);
371}
372
384inline int roundDoubleToInt (double value) noexcept
385{
386 return roundToInt (value);
387}
388
399inline int roundFloatToInt (float value) noexcept
400{
401 return roundToInt (value);
402}
403
404//==============================================================================
406template <typename IntegerType>
407bool isPowerOfTwo (IntegerType value)
408{
409 return (value & (value - 1)) == 0;
410}
411
413inline int nextPowerOfTwo (int n) noexcept
414{
415 --n;
416 n |= (n >> 1);
417 n |= (n >> 2);
418 n |= (n >> 4);
419 n |= (n >> 8);
420 n |= (n >> 16);
421 return n + 1;
422}
423
429
431inline int countNumberOfBits (uint32 n) noexcept
432{
433 n -= ((n >> 1) & 0x55555555);
434 n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
435 n = (((n >> 4) + n) & 0x0f0f0f0f);
436 n += (n >> 8);
437 n += (n >> 16);
438 return (int) (n & 0x3f);
439}
440
442inline int countNumberOfBits (uint64 n) noexcept
443{
444 return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
445}
446
450template <typename IntegerType>
451IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
452{
453 wassert (divisor > 0);
454 dividend %= divisor;
455 return (dividend < 0) ? (dividend + divisor) : dividend;
456}
457
459template <typename NumericType>
460NumericType square (NumericType n) noexcept
461{
462 return n * n;
463}
464
465//==============================================================================
473void writeLittleEndianBitsInBuffer (void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
474
482uint32 readLittleEndianBitsInBuffer (const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
483
484//==============================================================================
485
486}
487
488#endif // WATER_MATHSFUNCTIONS_H_INCLUDED
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
uint8_t a
Definition Spc_Cpu.h:141
unsigned v[N_MAX]
Definition inflate.c:1584
unsigned d
Definition inflate.c:940
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
#define wassert(expression)
Definition AudioSampleBuffer.h:33
unsigned int uint32
Definition water.h:98
IntegerType negativeAwareModulo(IntegerType dividend, const IntegerType divisor) noexcept
Definition MathsFunctions.h:451
bool isPositiveAndNotGreaterThan(Type valueToTest, Type upperLimit) noexcept
Definition MathsFunctions.h:210
int roundToInt(const FloatType value) noexcept
Definition MathsFunctions.h:346
void writeLittleEndianBitsInBuffer(void *targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept
const double double_Pi
Definition MathsFunctions.h:291
bool water_isfinite(NumericType) noexcept
Definition MathsFunctions.h:317
int roundToIntAccurate(double value) noexcept
Definition MathsFunctions.h:368
void findMinAndMax(const Type *values, int numValues, Type &lowest, Type &highest)
Definition MathsFunctions.h:125
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
float radiansToDegrees(float radians) noexcept
Definition MathsFunctions.h:306
unsigned long long uint64
Definition water.h:102
bool isPositiveAndBelow(Type valueToTest, Type upperLimit) noexcept
Definition MathsFunctions.h:187
Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Definition MathsFunctions.h:74
Type findMinimum(const Type *data, int numValues)
Definition MathsFunctions.h:89
int64 abs64(const int64 n) noexcept
Definition MathsFunctions.h:282
void swapVariables(Type &variable1, Type &variable2)
Definition MathsFunctions.h:230
void ignoreUnused(const Type1 &) noexcept
Definition MathsFunctions.h:237
int roundFloatToInt(float value) noexcept
Definition MathsFunctions.h:399
Type findMaximum(const Type *values, int numValues)
Definition MathsFunctions.h:107
float degreesToRadians(float degrees) noexcept
Definition MathsFunctions.h:300
long long int64
Definition water.h:100
uint32 readLittleEndianBitsInBuffer(const void *sourceBuffer, uint32 startBit, uint32 numBits) noexcept
bool isPowerOfTwo(IntegerType value)
Definition MathsFunctions.h:407
int findHighestSetBit(uint32 n) noexcept
int nextPowerOfTwo(int n) noexcept
Definition MathsFunctions.h:413
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
int countNumberOfBits(uint32 n) noexcept
Definition MathsFunctions.h:431
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition MathsFunctions.h:169
Type water_hypot(Type a, Type b) noexcept
Definition MathsFunctions.h:270
size_t numElementsInArray(Type(&array)[N])
Definition MathsFunctions.h:257
const float float_Pi
Definition MathsFunctions.h:296
NumericType square(NumericType n) noexcept
Definition MathsFunctions.h:460
int roundDoubleToInt(double value) noexcept
Definition MathsFunctions.h:384
#define N
Definition nseel-cfunc.c:36
int n
Definition crypt.c:458
return c
Definition crypt.c:175
b
Definition crypt.c:628
int result
Definition process.c:1455
#define void
Definition unzip.h:396