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.45 by root, Sat May 26 15:44:05 2007 UTC

2#define UTIL_H__ 2#define UTIL_H__
3 3
4//#define PREFER_MALLOC 4//#define PREFER_MALLOC
5 5
6#if __GNUC__ >= 3 6#if __GNUC__ >= 3
7# define is_constant(c) __builtin_constant_p (c) 7# define is_constant(c) __builtin_constant_p (c)
8# define expect(expr,value) __builtin_expect ((expr),(value))
9# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
8#else 10#else
9# define is_constant(c) 0 11# define is_constant(c) 0
12# define expect(expr,value) (expr)
13# define prefetch(addr,rw,locality)
10#endif 14#endif
15
16// put into ifs if you are very sure that the expression
17// is mostly true or mosty false. note that these return
18// booleans, not the expression.
19#define expect_false(expr) expect ((expr) != 0, 0)
20#define expect_true(expr) expect ((expr) != 0, 1)
11 21
12#include <cstddef> 22#include <cstddef>
13#include <cmath> 23#include <cmath>
14#include <new> 24#include <new>
15#include <vector> 25#include <vector>
18 28
19#include <shstr.h> 29#include <shstr.h>
20#include <traits.h> 30#include <traits.h>
21 31
22// use a gcc extension for auto declarations until ISO C++ sanctifies them 32// use a gcc extension for auto declarations until ISO C++ sanctifies them
23#define AUTODECL(var,expr) typeof(expr) var = (expr) 33#define auto(var,expr) typeof(expr) var = (expr)
24 34
25// very ugly macro that basicaly declares and initialises a variable 35// very ugly macro that basicaly declares and initialises a variable
26// that is in scope for the next statement only 36// that is in scope for the next statement only
27// works only for stuff that can be assigned 0 and converts to false 37// works only for stuff that can be assigned 0 and converts to false
28// (note: works great for pointers) 38// (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; } 54template<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; } 55template<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; } 56template<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 57
48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 58template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
59
60template<typename T>
61static inline T
62lerp (T val, T min_in, T max_in, T min_out, T max_out)
63{
64 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
65}
66
67// lots of stuff taken from FXT
68
69/* Rotate right. This is used in various places for checksumming */
70//TODO: that sucks, use a better checksum algo
71static inline uint32_t
72rotate_right (uint32_t c, uint32_t count = 1)
73{
74 return (c << (32 - count)) | (c >> count);
75}
76
77static inline uint32_t
78rotate_left (uint32_t c, uint32_t count = 1)
79{
80 return (c >> (32 - count)) | (c << count);
81}
82
83// Return abs(a-b)
84// Both a and b must not have the most significant bit set
85static inline uint32_t
86upos_abs_diff (uint32_t a, uint32_t b)
87{
88 long d1 = b - a;
89 long d2 = (d1 & (d1 >> 31)) << 1;
90
91 return d1 - d2; // == (b - d) - (a + d);
92}
93
94// Both a and b must not have the most significant bit set
95static inline uint32_t
96upos_min (uint32_t a, uint32_t b)
97{
98 int32_t d = b - a;
99 d &= d >> 31;
100 return a + d;
101}
102
103// Both a and b must not have the most significant bit set
104static inline uint32_t
105upos_max (uint32_t a, uint32_t b)
106{
107 int32_t d = b - a;
108 d &= d >> 31;
109 return b - d;
110}
49 111
50// this is much faster than crossfires original algorithm 112// this is much faster than crossfires original algorithm
51// on modern cpus 113// on modern cpus
52inline int 114inline int
53isqrt (int n) 115isqrt (int n)
217 279
218 void seed (uint32_t seed); 280 void seed (uint32_t seed);
219 uint32_t next (); 281 uint32_t next ();
220 282
221 // uniform distribution 283 // uniform distribution
222 uint32_t operator ()(uint32_t r_max) 284 uint32_t operator ()(uint32_t num)
223 { 285 {
224 return is_constant (r_max) 286 return is_constant (num)
225 ? this->next () % r_max 287 ? (next () * (uint64_t)num) >> 32U
226 : get_range (r_max); 288 : get_range (num);
227 } 289 }
228 290
229 // return a number within (min .. max) 291 // return a number within (min .. max)
230 int operator () (int r_min, int r_max) 292 int operator () (int r_min, int r_max)
231 { 293 {
232 return is_constant (r_min) && is_constant (r_max) 294 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
233 ? r_min + (*this) (max (r_max - r_min + 1, 1)) 295 ? r_min + operator ()(r_max - r_min + 1)
234 : get_range (r_min, r_max); 296 : get_range (r_min, r_max);
235 } 297 }
236 298
237 double operator ()() 299 double operator ()()
238 { 300 {
354 } 416 }
355 417
356 void erase (T *obj) 418 void erase (T *obj)
357 { 419 {
358 assert (obj->*index); 420 assert (obj->*index);
359 int pos = obj->*index; 421 unsigned int pos = obj->*index;
360 obj->*index = 0; 422 obj->*index = 0;
361 423
362 if (pos < this->size ()) 424 if (pos < this->size ())
363 { 425 {
364 (*this)[pos - 1] = (*this)[this->size () - 1]; 426 (*this)[pos - 1] = (*this)[this->size () - 1];
389// return current time as timestampe 451// return current time as timestampe
390tstamp now (); 452tstamp now ();
391 453
392int similar_direction (int a, int b); 454int similar_direction (int a, int b);
393 455
456// like printf, but returns a std::string
457const std::string format (const char *format, ...);
458
394#endif 459#endif
395 460

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines