… | |
… | |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
47 | |
47 | |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
49 | |
|
|
50 | // lots of stuff taken from FXT |
|
|
51 | |
|
|
52 | /* Rotate right. This is used in various places for checksumming */ |
|
|
53 | //TODO: this sucks, use a better checksum algo |
|
|
54 | static inline uint32_t |
|
|
55 | rotate_right (uint32_t c) |
|
|
56 | { |
|
|
57 | return (c << 31) | (c >> 1); |
|
|
58 | } |
|
|
59 | |
|
|
60 | // Return abs(a-b) |
|
|
61 | // Both a and b must not have the most significant bit set |
|
|
62 | static inline uint32_t |
|
|
63 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
64 | { |
|
|
65 | long d1 = b - a; |
|
|
66 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
67 | |
|
|
68 | return d1 - d2; // == (b - d) - (a + d); |
|
|
69 | } |
|
|
70 | |
|
|
71 | // Both a and b must not have the most significant bit set |
|
|
72 | static inline uint32_t |
|
|
73 | upos_min (uint32_t a, uint32_t b) |
|
|
74 | { |
|
|
75 | int32_t d = b - a; |
|
|
76 | d &= d >> 31; |
|
|
77 | return a + d; |
|
|
78 | } |
|
|
79 | |
|
|
80 | // Both a and b must not have the most significant bit set |
|
|
81 | static inline uint32_t |
|
|
82 | upos_max (uint32_t a, uint32_t b) |
|
|
83 | { |
|
|
84 | int32_t d = b - a; |
|
|
85 | d &= d >> 31; |
|
|
86 | return b - d; |
|
|
87 | } |
49 | |
88 | |
50 | // this is much faster than crossfires original algorithm |
89 | // this is much faster than crossfires original algorithm |
51 | // on modern cpus |
90 | // on modern cpus |
52 | inline int |
91 | inline int |
53 | isqrt (int n) |
92 | isqrt (int n) |