4260f680a527687ded489c595761155a7dd6e17b
[ardour.git] / libs / backends / wavesaudio / wavesapi / MiscUtils / MinMaxUtilities.h
1 /*
2     Copyright (C) 2014 Waves Audio Ltd.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __MinMaxUtilities_h__
21 #define __MinMaxUtilities_h__
22
23 /* copy to include
24 #include "MiscUtils/MinMaxUtilities.h"
25 */
26
27 #include "BasicTypes/WUDefines.h"
28 #include "BasicTypes/WUMathConsts.h"
29 #include "WavesPublicAPI/wstdint.h"
30
31 #ifdef __GNUC__
32 #undef round
33 #endif
34
35 // New accelerated templates
36 #if defined ( __cplusplus ) && !defined (__WUMinMax)
37 #define __WUMinMax   // Also defined in Nativepr.h
38
39
40 template<class T> inline T WUMin(const T &a, const T &b) {return (a < b) ? a : b;} // requires only < to be defined for T
41 template<class T> inline T WUMax(const T &a,const T &b) {return (a < b) ? b : a;} // requires only < to be defined for T
42 template<class T> inline T WUMinMax(const T &Smallest, const T &Biggest, const T &Val)  // requires only < to be defined for T
43 {
44         return ((Val < Smallest) ? Smallest : ((Biggest < Val) ? Biggest : Val));
45 }
46 /*
47 // Min and Max
48         template<class T> inline T WUMin(T a,T b) {return (a < b) ? a : b;} // requires only < to be defined for T
49         template<class T> inline T WUMax(T a,T b) {return (a < b) ? b : a;} // requires only < to be defined for T
50         template<class T> inline T WUMinMax(T SMALLEST, T BIGGEST, T X)  // requires only < to be defined for T
51         {
52                 return ((X < SMALLEST) ? SMALLEST : ((BIGGEST < X) ? BIGGEST : X));
53         }
54  */
55 // Absolute value
56
57 #ifdef PLATFORM_WINDOWS
58         #include <math.h>
59
60 #ifndef __GNUC__
61 #define __abs(x)        abs(x)
62 #define __labs(x)       labs(x)
63 #define __fabs(x)       fabs(x)
64 #endif
65
66 #endif
67
68 #ifdef __GNUC__
69         #include <iostream> // why don't know makes it work need to check
70         #include <cstdlib>
71         #include <cmath>
72
73 #define __abs(x)        std::abs(x)
74 #define __labs(x)       std::labs(x)
75 #define __fabs(x)       std::fabs(x)
76 #endif
77         #ifdef __APPLE__
78         #ifdef __GNUC__
79             #include <iostream> // why don't know makes it work need to check
80             #include <cmath>
81 #define __abs(x)        std::abs(x)
82 #define __labs(x)       std::labs(x)
83 #define __fabs(x)       std::fabs(x)
84         #endif
85         #endif
86
87 // log2: on Windows there's no proper definition for log2, whereas on other platform there is.
88         #ifndef WUlog2
89     #if defined(PLATFORM_WINDOWS)
90         #define WUlog2(x)  (kdOneOverLog2 * log10((x)))
91     #else
92         #define WUlog2(x) log2(x)
93     #endif
94     #endif
95
96 template <class T> inline T WUAbs(const T &xA)
97 {
98         return (xA > T(0))? xA: -xA;
99 }
100
101 template <> inline int WUAbs(const int &xA)
102 {
103         return __abs(xA);
104 }
105
106 //template <> inline int32_t WUAbs(const int32_t &xA)// 64BitConversion
107 //{
108 //      return __labs(xA);
109 //}
110
111 template <> inline float WUAbs(const float &xA)
112 {
113         return (float) __fabs(xA);
114 }
115
116 template <> inline double WUAbs(const double &xA)
117 {
118         return __fabs(xA);
119 }
120
121 #endif
122
123 int32_t DllExport WURand(intptr_t in_Seed);
124 int32_t DllExport WURand();
125 int32_t DllExport rand_gen_formula(int32_t rndSeed);
126
127 template <class T> inline bool WUIsEqualWithTolerance(const T &xA, const T &xB, const T &xTolerance)
128 {
129         return (WUAbs(xA - xB) < xTolerance) ? true : false;
130 }
131
132
133 #endif