summaryrefslogtreecommitdiff
path: root/libs/backends/wavesaudio/wavesapi/miscutils/MinMaxUtilities.h
blob: cd48169bf2420bc13e55dd6d969e3413f9642965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
    Copyright (C) 2013 Waves Audio Ltd.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
#ifndef __MinMaxUtilities_h__
#define __MinMaxUtilities_h__

/* copy to include
#include "MiscUtils/MinMaxUtilities.h"
*/

#include "BasicTypes/WUDefines.h"
#include "BasicTypes/WUMathConsts.h"
#include "WavesPublicAPI/wstdint.h"

// New accelerated templates
#if defined ( __cplusplus ) && !defined (__WUMinMax)
#define __WUMinMax   // Also defined in Nativepr.h


template<class T> inline T WUMin(const T &a, const T &b) {return (a < b) ? a : b;} // requires only < to be defined for T
template<class T> inline T WUMax(const T &a,const T &b) {return (a < b) ? b : a;} // requires only < to be defined for T
template<class T> inline T WUMinMax(const T &Smallest, const T &Biggest, const T &Val)  // requires only < to be defined for T
{	
	return ((Val < Smallest) ? Smallest : ((Biggest < Val) ? Biggest : Val));
}
/*	
// Min and Max
	template<class T> inline T WUMin(T a,T b) {return (a < b) ? a : b;} // requires only < to be defined for T
	template<class T> inline T WUMax(T a,T b) {return (a < b) ? b : a;} // requires only < to be defined for T
	template<class T> inline T WUMinMax(T SMALLEST, T BIGGEST, T X)  // requires only < to be defined for T
	{
		return ((X < SMALLEST) ? SMALLEST : ((BIGGEST < X) ? BIGGEST : X));
	}
 */	
// Absolute value
#ifdef _WINDOWS
	#include <math.h>
#define __abs(x)	abs(x) 
#define __labs(x)	labs(x)
#define __fabs(x)	fabs(x)
#endif
#ifdef __GNUC__
	#include <iostream> // why don't know makes it work need to check
	#include <cstdlib>
	#include <cmath>

#define __abs(x)	std::abs(x)
#define __labs(x)	std::labs(x)
#define __fabs(x)	std::fabs(x)
#endif
	#ifdef __MACOS__
        #ifdef __GNUC__
            #include <iostream> // why don't know makes it work need to check
            #include <cmath>
#define __abs(x)	std::abs(x)
#define __labs(x)	std::labs(x)
#define __fabs(x)	std::fabs(x)
        #endif
	#endif

// log2: on Windows there's no proper definition for log2, whereas on other platform there is.
	#ifndef WUlog2
    #if defined(_WINDOWS)
        #define WUlog2(x)  (kdOneOverLog2 * log10((x))) 
    #else    
        #define WUlog2(x) log2(x)
    #endif
    #endif

template <class T> inline T WUAbs(const T &xA)
{
	return (xA > T(0))? xA: -xA;
}

template <> inline int WUAbs(const int &xA)
{
	return __abs(xA);
}

//template <> inline int32_t WUAbs(const int32_t &xA)// 64BitConversion
//{
//	return __labs(xA);
//}

template <> inline float WUAbs(const float &xA)
{
	return (float) __fabs(xA);
}

template <> inline double WUAbs(const double &xA)
{
	return __fabs(xA);
}

#endif

int32_t DllExport WURand(intptr_t in_Seed);
int32_t DllExport WURand();
int32_t DllExport rand_gen_formula(int32_t rndSeed);

template <class T> inline bool WUIsEqualWithTolerance(const T &xA, const T &xB, const T &xTolerance)
{
	return (WUAbs(xA - xB) < xTolerance) ? true : false;
}


#endif