author | rubidium |
Sun, 06 Apr 2008 23:07:42 +0000 | |
branch | noai |
changeset 9869 | 6404afe43575 |
parent 9826 | 9707ad4c9b60 |
child 11126 | 72d4c9314c72 |
permissions | -rw-r--r-- |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
1 |
/* $Id$ */ |
9722 | 2 |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
3 |
/** @file math_func.hpp Integer math functions */ |
9722 | 4 |
|
5 |
#ifndef MATH_FUNC_HPP |
|
6 |
#define MATH_FUNC_HPP |
|
7 |
||
8 |
#ifdef min |
|
9 |
#undef min |
|
10 |
#endif |
|
11 |
||
12 |
#ifdef max |
|
13 |
#undef max |
|
14 |
#endif |
|
15 |
||
16 |
#ifdef abs |
|
17 |
#undef abs |
|
18 |
#endif |
|
19 |
||
20 |
/** |
|
21 |
* Returns the maximum of two values. |
|
22 |
* |
|
23 |
* This function returns the greater value of two given values. |
|
24 |
* If they are equal the value of a is returned. |
|
25 |
* |
|
26 |
* @param a The first value |
|
27 |
* @param b The second value |
|
28 |
* @return The greater value or a if equals |
|
29 |
*/ |
|
30 |
template<typename T> static inline T max(const T a, const T b) |
|
31 |
{ |
|
32 |
return (a >= b) ? a : b; |
|
33 |
} |
|
34 |
||
35 |
/** |
|
36 |
* Returns the minimum of two values. |
|
37 |
* |
|
38 |
* This function returns the smaller value of two given values. |
|
39 |
* If they are equal the value of b is returned. |
|
40 |
* |
|
41 |
* @param a The first value |
|
42 |
* @param b The second value |
|
43 |
* @return The smaller value or b if equals |
|
44 |
*/ |
|
45 |
template<typename T> static inline T min(const T a, const T b) |
|
46 |
{ |
|
47 |
return (a < b) ? a : b; |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Returns the minimum of two integer. |
|
52 |
* |
|
53 |
* This function returns the smaller value of two given integers. |
|
54 |
* |
|
55 |
* @param a The first integer |
|
56 |
* @param b The second integer |
|
57 |
* @return The smaller value |
|
58 |
*/ |
|
59 |
static inline int min(const int a, const int b) |
|
60 |
{ |
|
61 |
return (a < b) ? a : b; |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Returns the minimum of two unsigned integers. |
|
66 |
* |
|
67 |
* This function returns the smaller value of two given unsigned integers. |
|
68 |
* |
|
69 |
* @param a The first unsigned integer |
|
70 |
* @param b The second unsigned integer |
|
71 |
* @return The smaller value |
|
72 |
*/ |
|
73 |
static inline uint minu(const uint a, const uint b) |
|
74 |
{ |
|
75 |
return (a < b) ? a : b; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Returns the absolute value of (scalar) variable. |
|
80 |
* |
|
81 |
* @note assumes variable to be signed |
|
82 |
* @param a The value we want to unsign |
|
83 |
* @return The unsigned value |
|
84 |
*/ |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
85 |
template <typename T> static inline T abs(const T a) |
9722 | 86 |
{ |
87 |
return (a < (T)0) ? -a : a; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Return the smallest multiple of n equal or greater than x |
|
92 |
* |
|
93 |
* @note n must be a power of 2 |
|
94 |
* @param x The min value |
|
95 |
* @param n The base of the number we are searching |
|
96 |
* @return The smallest multiple of n equal or greater than x |
|
97 |
*/ |
|
98 |
template<typename T> static inline T Align(const T x, uint n) |
|
99 |
{ |
|
100 |
n--; |
|
101 |
return (T)((x + n) & ~(n)); |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Clamp an integer between an interval. |
|
106 |
* |
|
107 |
* This function returns a value which is between the given interval of |
|
108 |
* min and max. If the given value is in this interval the value itself |
|
109 |
* is returned otherwise the border of the interval is returned, according |
|
110 |
* which side of the interval was 'left'. |
|
111 |
* |
|
112 |
* @note The min value must be less or equal of max or you get some |
|
113 |
* unexpected results. |
|
114 |
* @param a The value to clamp/truncate. |
|
115 |
* @param min The minimum of the interval. |
|
116 |
* @param max the maximum of the interval. |
|
117 |
* @returns A value between min and max which is closest to a. |
|
118 |
* @see ClampU(uint, uint, uint) |
|
119 |
*/ |
|
120 |
static inline int Clamp(const int a, const int min, const int max) |
|
121 |
{ |
|
122 |
if (a <= min) return min; |
|
123 |
if (a >= max) return max; |
|
124 |
return a; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Clamp an unsigned integer between an interval. |
|
129 |
* |
|
130 |
* This function returns a value which is between the given interval of |
|
131 |
* min and max. If the given value is in this interval the value itself |
|
132 |
* is returned otherwise the border of the interval is returned, according |
|
133 |
* which side of the interval was 'left'. |
|
134 |
* |
|
135 |
* @note The min value must be less or equal of max or you get some |
|
136 |
* unexpected results. |
|
137 |
* @param a The value to clamp/truncate. |
|
138 |
* @param min The minimum of the interval. |
|
139 |
* @param max the maximum of the interval. |
|
140 |
* @returns A value between min and max which is closest to a. |
|
141 |
* @see Clamp(int, int, int) |
|
142 |
*/ |
|
143 |
static inline uint ClampU(const uint a, const uint min, const uint max) |
|
144 |
{ |
|
145 |
if (a <= min) return min; |
|
146 |
if (a >= max) return max; |
|
147 |
return a; |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Reduce a signed 64-bit int to a signed 32-bit one |
|
152 |
* |
|
153 |
* This function clamps a 64-bit integer to a 32-bit integer. |
|
154 |
* If the 64-bit value is smaller than the smallest 32-bit integer |
|
155 |
* value 0x80000000 this value is returned (the left one bit is the sign bit). |
|
156 |
* If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF |
|
157 |
* this value is returned. In all other cases the 64-bit value 'fits' in a |
|
158 |
* 32-bits integer field and so the value is casted to int32 and returned. |
|
159 |
* |
|
160 |
* @param a The 64-bit value to clamps |
|
161 |
* @return The 64-bit value reduced to a 32-bit value |
|
162 |
* @see Clamp(int, int, int) |
|
163 |
*/ |
|
164 |
static inline int32 ClampToI32(const int64 a) |
|
165 |
{ |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
166 |
if (a <= INT32_MIN) return INT32_MIN; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
167 |
if (a >= INT32_MAX) return INT32_MAX; |
9722 | 168 |
return (int32)a; |
169 |
} |
|
170 |
||
171 |
/** |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
172 |
* Reduce an unsigned 64-bit int to an unsigned 16-bit one |
9732 | 173 |
* |
174 |
* @param a The 64-bit value to clamp |
|
175 |
* @return The 64-bit value reduced to a 16-bit value |
|
176 |
* @see ClampU(uint, uint, uint) |
|
177 |
*/ |
|
178 |
static inline uint16 ClampToU16(const uint64 a) |
|
179 |
{ |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
180 |
return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX); |
9732 | 181 |
} |
182 |
||
183 |
/** |
|
9722 | 184 |
* Returns the (absolute) difference between two (scalar) variables |
185 |
* |
|
186 |
* @param a The first scalar |
|
187 |
* @param b The second scalar |
|
188 |
* @return The absolute difference between the given scalars |
|
189 |
*/ |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
190 |
template <typename T> static inline T Delta(const T a, const T b) { |
9722 | 191 |
return (a < b) ? b - a : a - b; |
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Checks if a value is between a window started at some base point. |
|
196 |
* |
|
197 |
* This function checks if the value x is between the value of base |
|
198 |
* and base+size. If x equals base this returns true. If x equals |
|
199 |
* base+size this returns false. |
|
200 |
* |
|
201 |
* @param x The value to check |
|
202 |
* @param base The base value of the interval |
|
203 |
* @param size The size of the interval |
|
204 |
* @return True if the value is in the interval, false else. |
|
205 |
*/ |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
206 |
template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size) |
9722 | 207 |
{ |
208 |
return (uint)(x - base) < size; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
212 |
* Checks if a value is in an interval. |
9722 | 213 |
* |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
214 |
* Returns true if a value is in the interval of [min, max). |
9722 | 215 |
* |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
216 |
* @param a The value to check |
9722 | 217 |
* @param min The minimum of the interval |
218 |
* @param max The maximum of the interval |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
219 |
* @see IsInsideBS() |
9722 | 220 |
*/ |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
221 |
template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max) |
9722 | 222 |
{ |
223 |
return (uint)(x - min) < (max - min); |
|
224 |
} |
|
225 |
||
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
226 |
/** |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
227 |
* Type safe swap operation |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
228 |
* @param a variable to swap with b |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
229 |
* @param b variable to swap with a |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
230 |
*/ |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
231 |
template<typename T> void Swap(T& a, T& b) |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
232 |
{ |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
233 |
T t = a; |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
234 |
a = b; |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
235 |
b = t; |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
236 |
} |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
237 |
|
9722 | 238 |
#endif /* MATH_FUNC_HPP */ |