ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.36 by root, Thu Jan 25 03:54:45 2007 UTC vs.
Revision 1.44 by root, Fri May 11 08:00:00 2007 UTC

18 18
19#include <shstr.h> 19#include <shstr.h>
20#include <traits.h> 20#include <traits.h>
21 21
22// use a gcc extension for auto declarations until ISO C++ sanctifies them 22// use a gcc extension for auto declarations until ISO C++ sanctifies them
23#define AUTODECL(var,expr) typeof(expr) var = (expr) 23#define auto(var,expr) typeof(expr) var = (expr)
24 24
25// very ugly macro that basicaly declares and initialises a variable 25// very ugly macro that basicaly declares and initialises a variable
26// that is in scope for the next statement only 26// that is in scope for the next statement only
27// works only for stuff that can be assigned 0 and converts to false 27// works only for stuff that can be assigned 0 and converts to false
28// (note: works great for pointers) 28// (note: works great for pointers)
44template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } 44template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } 45template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
46template<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; } 46template<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
48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49
50template<typename T>
51static inline T
52lerp (T val, T min_in, T max_in, T min_out, T max_out)
53{
54 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
55}
56
57// lots of stuff taken from FXT
58
59/* Rotate right. This is used in various places for checksumming */
60//TODO: that sucks, use a better checksum algo
61static inline uint32_t
62rotate_right (uint32_t c, uint32_t count = 1)
63{
64 return (c << (32 - count)) | (c >> count);
65}
66
67static inline uint32_t
68rotate_left (uint32_t c, uint32_t count = 1)
69{
70 return (c >> (32 - count)) | (c << count);
71}
72
73// Return abs(a-b)
74// Both a and b must not have the most significant bit set
75static inline uint32_t
76upos_abs_diff (uint32_t a, uint32_t b)
77{
78 long d1 = b - a;
79 long d2 = (d1 & (d1 >> 31)) << 1;
80
81 return d1 - d2; // == (b - d) - (a + d);
82}
83
84// Both a and b must not have the most significant bit set
85static inline uint32_t
86upos_min (uint32_t a, uint32_t b)
87{
88 int32_t d = b - a;
89 d &= d >> 31;
90 return a + d;
91}
92
93// Both a and b must not have the most significant bit set
94static inline uint32_t
95upos_max (uint32_t a, uint32_t b)
96{
97 int32_t d = b - a;
98 d &= d >> 31;
99 return b - d;
100}
49 101
50// this is much faster than crossfires original algorithm 102// this is much faster than crossfires original algorithm
51// on modern cpus 103// on modern cpus
52inline int 104inline int
53isqrt (int n) 105isqrt (int n)
217 269
218 void seed (uint32_t seed); 270 void seed (uint32_t seed);
219 uint32_t next (); 271 uint32_t next ();
220 272
221 // uniform distribution 273 // uniform distribution
222 uint32_t operator ()(uint32_t r_max) 274 uint32_t operator ()(uint32_t num)
223 { 275 {
224 return is_constant (r_max) 276 return is_constant (num)
225 ? this->next () % r_max 277 ? (next () * (uint64_t)num) >> 32U
226 : get_range (r_max); 278 : get_range (num);
227 } 279 }
228 280
229 // return a number within (min .. max) 281 // return a number within (min .. max)
230 int operator () (int r_min, int r_max) 282 int operator () (int r_min, int r_max)
231 { 283 {
232 return is_constant (r_min) && is_constant (r_max) 284 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
233 ? r_min + (*this) (max (r_max - r_min + 1, 1)) 285 ? r_min + operator ()(r_max - r_min + 1)
234 : get_range (r_min, r_max); 286 : get_range (r_min, r_max);
235 } 287 }
236 288
237 double operator ()() 289 double operator ()()
238 { 290 {
354 } 406 }
355 407
356 void erase (T *obj) 408 void erase (T *obj)
357 { 409 {
358 assert (obj->*index); 410 assert (obj->*index);
359 int pos = obj->*index; 411 unsigned int pos = obj->*index;
360 obj->*index = 0; 412 obj->*index = 0;
361 413
362 if (pos < this->size ()) 414 if (pos < this->size ())
363 { 415 {
364 (*this)[pos - 1] = (*this)[this->size () - 1]; 416 (*this)[pos - 1] = (*this)[this->size () - 1];
389// return current time as timestampe 441// return current time as timestampe
390tstamp now (); 442tstamp now ();
391 443
392int similar_direction (int a, int b); 444int similar_direction (int a, int b);
393 445
446// like printf, but returns a std::string
447const std::string format (const char *format, ...);
448
394#endif 449#endif
395 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines