|
1 /* $Id */ |
|
2 |
|
3 /** @file math_func.hpp */ |
|
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 */ |
|
85 template <typename T> static inline T abs(T a) |
|
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 { |
|
166 if (a <= (int32)0x80000000) return 0x80000000; |
|
167 if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF; |
|
168 return (int32)a; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Returns the (absolute) difference between two (scalar) variables |
|
173 * |
|
174 * @param a The first scalar |
|
175 * @param b The second scalar |
|
176 * @return The absolute difference between the given scalars |
|
177 */ |
|
178 template <typename T> static inline T delta(const T a, const T b) { |
|
179 return (a < b) ? b - a : a - b; |
|
180 } |
|
181 |
|
182 /** |
|
183 * Checks if a value is between a window started at some base point. |
|
184 * |
|
185 * This function checks if the value x is between the value of base |
|
186 * and base+size. If x equals base this returns true. If x equals |
|
187 * base+size this returns false. |
|
188 * |
|
189 * @param x The value to check |
|
190 * @param base The base value of the interval |
|
191 * @param size The size of the interval |
|
192 * @return True if the value is in the interval, false else. |
|
193 */ |
|
194 template<typename T> static inline bool IS_INSIDE_1D(const T x, const int base, const uint size) |
|
195 { |
|
196 return (uint)(x - base) < size; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Checks if a byte is in an interval. |
|
201 * |
|
202 * Returns true if a byte value is in the interval of [min, max). |
|
203 * |
|
204 * @param a The byte value to check |
|
205 * @param min The minimum of the interval |
|
206 * @param max The maximum of the interval |
|
207 * @see IS_INSIDE_1D |
|
208 */ |
|
209 template<typename T> static inline bool IS_BYTE_INSIDE(const T x, const byte min, const byte max) |
|
210 { |
|
211 return (byte)(x - min) < (max - min); |
|
212 } |
|
213 |
|
214 /** |
|
215 * Checks if an int is in an interval. |
|
216 * |
|
217 * Returns true if a integer value is in the interval of [min, max). |
|
218 * |
|
219 * @param a The integer value to check |
|
220 * @param min The minimum of the interval |
|
221 * @param max The maximum of the interval |
|
222 * @see IS_INSIDE_1D |
|
223 */ |
|
224 template<typename T> static inline bool IS_INT_INSIDE(const T x, const int min, const uint max) |
|
225 { |
|
226 return (uint)(x - min) < (max - min); |
|
227 } |
|
228 |
|
229 #endif /* MATH_FUNC_HPP */ |