-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.h
executable file
·76 lines (66 loc) · 1.48 KB
/
Util.h
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
#if !defined(__Util_hdr__)
#define __Util_hdr__
#include <cassert>
#include <cstring>
#include <limits>
/*! \brief class with static utility functions
*/
class CUtil
{
public:
/*! converts a float to an int
\param fInput float value
\return T
*/
template<typename T>
static T float2int (float fInput)
{
if (fInput >= 0.F)
return static_cast<T>(fInput + .5F);
else
return static_cast<T>(fInput - .5F);
}
/*! converts a double to an int
\param fInput double value
\return T
*/
template<typename T>
static T double2int (double fInput)
{
if (fInput >= 0)
return static_cast<T>(fInput + .5);
else
return static_cast<T>(fInput - .5);
}
/*! checks if the input is a power of 2
\param n integer value
\return bool
*/
static bool isPowOf2 (int n)
{
return !(n & (n-1));
}
/*! converts an arbitrary integer (positive) to the next larger power of two
\param n integer value
\return int
*/
static int nextPowOf2(int n)
{
int iOrder = 0;
if (n == 0)
return 0;
while (n>>iOrder)
iOrder++;
if (!(n%(1<<(iOrder-1))))
iOrder--;
return (1<<(iOrder));
}
template<typename T>
static void swap (T &tValue1, T &tValue2)
{
T tTmp = tValue1;
tValue1 = tValue2;
tValue2 = tTmp;
}
};
#endif // __Util_hdr__