author | smatz |
Thu, 26 Jun 2008 15:46:19 +0000 | |
changeset 11082 | 45ab75d184a0 |
parent 11050 | 091271fcfbb9 |
permissions | -rw-r--r-- |
8587
6db234b2b897
(svn r11652) -Codechange: add the svn $ header for several files
smatz
parents:
8466
diff
changeset
|
1 |
/* $Id$ */ |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
2 |
|
8587
6db234b2b897
(svn r11652) -Codechange: add the svn $ header for several files
smatz
parents:
8466
diff
changeset
|
3 |
/** @file math_func.hpp Integer math functions */ |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
4 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
5 |
#ifndef MATH_FUNC_HPP |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
6 |
#define MATH_FUNC_HPP |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
7 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
8 |
#ifdef min |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
9 |
#undef min |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
10 |
#endif |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
11 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
12 |
#ifdef max |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
13 |
#undef max |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
14 |
#endif |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
15 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
16 |
#ifdef abs |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
17 |
#undef abs |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
18 |
#endif |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
19 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
20 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
21 |
* Returns the maximum of two values. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
22 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
23 |
* This function returns the greater value of two given values. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
24 |
* If they are equal the value of a is returned. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
25 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
26 |
* @param a The first value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
27 |
* @param b The second value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
28 |
* @return The greater value or a if equals |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
29 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
30 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
31 |
static FORCEINLINE T max(const T a, const T b) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
32 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
33 |
return (a >= b) ? a : b; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
34 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
35 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
36 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
37 |
* Returns the minimum of two values. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
38 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
39 |
* This function returns the smaller value of two given values. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
40 |
* If they are equal the value of b is returned. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
41 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
42 |
* @param a The first value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
43 |
* @param b The second value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
44 |
* @return The smaller value or b if equals |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
45 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
46 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
47 |
static FORCEINLINE T min(const T a, const T b) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
48 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
49 |
return (a < b) ? a : b; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
50 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
51 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
52 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
53 |
* Returns the minimum of two integer. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
54 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
55 |
* This function returns the smaller value of two given integers. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
56 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
57 |
* @param a The first integer |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
58 |
* @param b The second integer |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
59 |
* @return The smaller value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
60 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
61 |
static FORCEINLINE int min(const int a, const int b) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
62 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
63 |
return (a < b) ? a : b; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
64 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
65 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
66 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
67 |
* Returns the minimum of two unsigned integers. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
68 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
69 |
* This function returns the smaller value of two given unsigned integers. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
70 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
71 |
* @param a The first unsigned integer |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
72 |
* @param b The second unsigned integer |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
73 |
* @return The smaller value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
74 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
75 |
static FORCEINLINE uint minu(const uint a, const uint b) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
76 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
77 |
return (a < b) ? a : b; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
78 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
79 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
80 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
81 |
* Returns the absolute value of (scalar) variable. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
82 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
83 |
* @note assumes variable to be signed |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
84 |
* @param a The value we want to unsign |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
85 |
* @return The unsigned value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
86 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
87 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
88 |
static FORCEINLINE T abs(const T a) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
89 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
90 |
return (a < (T)0) ? -a : a; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
91 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
92 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
93 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
94 |
* Return the smallest multiple of n equal or greater than x |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
95 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
96 |
* @note n must be a power of 2 |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
97 |
* @param x The min value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
98 |
* @param n The base of the number we are searching |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
99 |
* @return The smallest multiple of n equal or greater than x |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
100 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
101 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
102 |
static FORCEINLINE T Align(const T x, uint n) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
103 |
{ |
11082
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
104 |
assert((n & (n - 1)) == 0 && n != 0); |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
105 |
n--; |
11082
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
106 |
return (T)((x + n) & ~((T)n)); |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
107 |
} |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
108 |
|
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
109 |
/** |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
110 |
* Return the smallest multiple of n equal or greater than x |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
111 |
* Applies to pointers only |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
112 |
* |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
113 |
* @note n must be a power of 2 |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
114 |
* @param x The min value |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
115 |
* @param n The base of the number we are searching |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
116 |
* @return The smallest multiple of n equal or greater than x |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
117 |
* @see Align() |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
118 |
*/ |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
119 |
|
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
120 |
assert_compile(sizeof(size_t) == sizeof(void *)); |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
121 |
|
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
122 |
template <typename T> |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
123 |
static FORCEINLINE T *AlignPtr(T *x, uint n) |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
124 |
{ |
45ab75d184a0
(svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents:
11050
diff
changeset
|
125 |
return (T *)Align((size_t)x, n); |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
126 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
127 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
128 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
129 |
* Clamp an integer between an interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
130 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
131 |
* This function returns a value which is between the given interval of |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
132 |
* min and max. If the given value is in this interval the value itself |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
133 |
* is returned otherwise the border of the interval is returned, according |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
134 |
* which side of the interval was 'left'. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
135 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
136 |
* @note The min value must be less or equal of max or you get some |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
137 |
* unexpected results. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
138 |
* @param a The value to clamp/truncate. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
139 |
* @param min The minimum of the interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
140 |
* @param max the maximum of the interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
141 |
* @returns A value between min and max which is closest to a. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
142 |
* @see ClampU(uint, uint, uint) |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
143 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
144 |
static FORCEINLINE int Clamp(const int a, const int min, const int max) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
145 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
146 |
if (a <= min) return min; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
147 |
if (a >= max) return max; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
148 |
return a; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
149 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
150 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
151 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
152 |
* Clamp an unsigned integer between an interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
153 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
154 |
* This function returns a value which is between the given interval of |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
155 |
* min and max. If the given value is in this interval the value itself |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
156 |
* is returned otherwise the border of the interval is returned, according |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
157 |
* which side of the interval was 'left'. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
158 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
159 |
* @note The min value must be less or equal of max or you get some |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
160 |
* unexpected results. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
161 |
* @param a The value to clamp/truncate. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
162 |
* @param min The minimum of the interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
163 |
* @param max the maximum of the interval. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
164 |
* @returns A value between min and max which is closest to a. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
165 |
* @see Clamp(int, int, int) |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
166 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
167 |
static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
168 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
169 |
if (a <= min) return min; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
170 |
if (a >= max) return max; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
171 |
return a; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
172 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
173 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
174 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
175 |
* Reduce a signed 64-bit int to a signed 32-bit one |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
176 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
177 |
* This function clamps a 64-bit integer to a 32-bit integer. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
178 |
* If the 64-bit value is smaller than the smallest 32-bit integer |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
179 |
* value 0x80000000 this value is returned (the left one bit is the sign bit). |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
180 |
* If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
181 |
* this value is returned. In all other cases the 64-bit value 'fits' in a |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
182 |
* 32-bits integer field and so the value is casted to int32 and returned. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
183 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
184 |
* @param a The 64-bit value to clamps |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
185 |
* @return The 64-bit value reduced to a 32-bit value |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
186 |
* @see Clamp(int, int, int) |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
187 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
188 |
static FORCEINLINE int32 ClampToI32(const int64 a) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
189 |
{ |
9321
3bc631f0ef34
(svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents:
9320
diff
changeset
|
190 |
if (a <= INT32_MIN) return INT32_MIN; |
3bc631f0ef34
(svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents:
9320
diff
changeset
|
191 |
if (a >= INT32_MAX) return INT32_MAX; |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
192 |
return (int32)a; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
193 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
194 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
195 |
/** |
9238 | 196 |
* Reduce an unsigned 64-bit int to an unsigned 16-bit one |
9106
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
197 |
* |
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
198 |
* @param a The 64-bit value to clamp |
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
199 |
* @return The 64-bit value reduced to a 16-bit value |
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
200 |
* @see ClampU(uint, uint, uint) |
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
201 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
202 |
static FORCEINLINE uint16 ClampToU16(const uint64 a) |
9106
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
203 |
{ |
9321
3bc631f0ef34
(svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents:
9320
diff
changeset
|
204 |
return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX); |
9106
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
205 |
} |
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
206 |
|
604b69ee3879
(svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents:
8626
diff
changeset
|
207 |
/** |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
208 |
* Returns the (absolute) difference between two (scalar) variables |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
209 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
210 |
* @param a The first scalar |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
211 |
* @param b The second scalar |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
212 |
* @return The absolute difference between the given scalars |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
213 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
214 |
template <typename T> |
11050
091271fcfbb9
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
11049
diff
changeset
|
215 |
static FORCEINLINE T Delta(const T a, const T b) |
091271fcfbb9
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
11049
diff
changeset
|
216 |
{ |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
217 |
return (a < b) ? b - a : a - b; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
218 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
219 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
220 |
/** |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
221 |
* Checks if a value is between a window started at some base point. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
222 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
223 |
* This function checks if the value x is between the value of base |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
224 |
* and base+size. If x equals base this returns true. If x equals |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
225 |
* base+size this returns false. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
226 |
* |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
227 |
* @param x The value to check |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
228 |
* @param base The base value of the interval |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
229 |
* @param size The size of the interval |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
230 |
* @return True if the value is in the interval, false else. |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
231 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
232 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
233 |
static FORCEINLINE bool IsInsideBS(const T x, const uint base, const uint size) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
234 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
235 |
return (uint)(x - base) < size; |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
236 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
237 |
|
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
238 |
/** |
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8433
diff
changeset
|
239 |
* Checks if a value is in an interval. |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
240 |
* |
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8433
diff
changeset
|
241 |
* Returns true if a value is in the interval of [min, max). |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
242 |
* |
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8433
diff
changeset
|
243 |
* @param a The value to check |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
244 |
* @param min The minimum of the interval |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
245 |
* @param max The maximum of the interval |
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8433
diff
changeset
|
246 |
* @see IsInsideBS() |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
247 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
248 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
249 |
static FORCEINLINE bool IsInsideMM(const T x, const uint min, const uint max) |
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
250 |
{ |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
251 |
return (uint)(x - min) < (max - min); |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
252 |
} |
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
253 |
|
8626
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
254 |
/** |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
255 |
* Type safe swap operation |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
256 |
* @param a variable to swap with b |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
257 |
* @param b variable to swap with a |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
258 |
*/ |
11049
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
259 |
template <typename T> |
f8bbc9635251
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9321
diff
changeset
|
260 |
static FORCEINLINE void Swap(T &a, T &b) |
8626
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
261 |
{ |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
262 |
T t = a; |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
263 |
a = b; |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
264 |
b = t; |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
265 |
} |
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8619
diff
changeset
|
266 |
|
8433
9bbc38806eeb
(svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff
changeset
|
267 |
#endif /* MATH_FUNC_HPP */ |