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.26 by root, Sun Jan 7 02:39:14 2007 UTC vs.
Revision 1.46 by root, Mon May 28 21:15:56 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de>
21 */
22
1#ifndef UTIL_H__ 23#ifndef UTIL_H__
2#define UTIL_H__ 24#define UTIL_H__
3 25
26//#define PREFER_MALLOC
27
4#if __GNUC__ >= 3 28#if __GNUC__ >= 3
5# define is_constant(c) __builtin_constant_p (c) 29# define is_constant(c) __builtin_constant_p (c)
30# define expect(expr,value) __builtin_expect ((expr),(value))
31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
6#else 32#else
7# define is_constant(c) 0 33# define is_constant(c) 0
34# define expect(expr,value) (expr)
35# define prefetch(addr,rw,locality)
8#endif 36#endif
9 37
38// put into ifs if you are very sure that the expression
39// is mostly true or mosty false. note that these return
40// booleans, not the expression.
41#define expect_false(expr) expect ((expr) != 0, 0)
42#define expect_true(expr) expect ((expr) != 0, 1)
43
10#include <cstddef> 44#include <cstddef>
45#include <cmath>
11#include <new> 46#include <new>
12#include <vector> 47#include <vector>
13 48
14#include <glib.h> 49#include <glib.h>
15 50
16#include <shstr.h> 51#include <shstr.h>
17#include <traits.h> 52#include <traits.h>
18 53
19// use a gcc extension for auto declarations until ISO C++ sanctifies them 54// use a gcc extension for auto declarations until ISO C++ sanctifies them
20#define AUTODECL(var,expr) typeof(expr) var = (expr) 55#define auto(var,expr) typeof(expr) var = (expr)
21 56
22// very ugly macro that basicaly declares and initialises a variable 57// very ugly macro that basicaly declares and initialises a variable
23// that is in scope for the next statement only 58// that is in scope for the next statement only
24// works only for stuff that can be assigned 0 and converts to false 59// works only for stuff that can be assigned 0 and converts to false
25// (note: works great for pointers) 60// (note: works great for pointers)
26// most ugly macro I ever wrote 61// most ugly macro I ever wrote
27#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 62#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
28 63
64// in range including end
65#define IN_RANGE_INC(val,beg,end) \
66 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
67
68// in range excluding end
69#define IN_RANGE_EXC(val,beg,end) \
70 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
71
72void fork_abort (const char *msg);
73
74// rationale for using (U) not (T) is to reduce signed/unsigned issues,
75// as a is often a constant while b is the variable. it is still a bug, though.
76template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
77template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
78template<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; }
79
80template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
81
82template<typename T>
83static inline T
84lerp (T val, T min_in, T max_in, T min_out, T max_out)
85{
86 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
87}
88
89// lots of stuff taken from FXT
90
91/* Rotate right. This is used in various places for checksumming */
92//TODO: that sucks, use a better checksum algo
93static inline uint32_t
94rotate_right (uint32_t c, uint32_t count = 1)
95{
96 return (c << (32 - count)) | (c >> count);
97}
98
99static inline uint32_t
100rotate_left (uint32_t c, uint32_t count = 1)
101{
102 return (c >> (32 - count)) | (c << count);
103}
104
105// Return abs(a-b)
106// Both a and b must not have the most significant bit set
107static inline uint32_t
108upos_abs_diff (uint32_t a, uint32_t b)
109{
110 long d1 = b - a;
111 long d2 = (d1 & (d1 >> 31)) << 1;
112
113 return d1 - d2; // == (b - d) - (a + d);
114}
115
116// Both a and b must not have the most significant bit set
117static inline uint32_t
118upos_min (uint32_t a, uint32_t b)
119{
120 int32_t d = b - a;
121 d &= d >> 31;
122 return a + d;
123}
124
125// Both a and b must not have the most significant bit set
126static inline uint32_t
127upos_max (uint32_t a, uint32_t b)
128{
129 int32_t d = b - a;
130 d &= d >> 31;
131 return b - d;
132}
133
134// this is much faster than crossfires original algorithm
135// on modern cpus
136inline int
137isqrt (int n)
138{
139 return (int)sqrtf ((float)n);
140}
141
142// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
143#if 0
144// and has a max. error of 6 in the range -100..+100.
145#else
146// and has a max. error of 9 in the range -100..+100.
147#endif
148inline int
149idistance (int dx, int dy)
150{
151 unsigned int dx_ = abs (dx);
152 unsigned int dy_ = abs (dy);
153
154#if 0
155 return dx_ > dy_
156 ? (dx_ * 61685 + dy_ * 26870) >> 16
157 : (dy_ * 61685 + dx_ * 26870) >> 16;
158#else
159 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
160#endif
161}
162
163/*
164 * absdir(int): Returns a number between 1 and 8, which represent
165 * the "absolute" direction of a number (it actually takes care of
166 * "overflow" in previous calculations of a direction).
167 */
168inline int
169absdir (int d)
170{
171 return ((d - 1) & 7) + 1;
172}
173
29// makes dynamically allocated objects zero-initialised 174// makes dynamically allocated objects zero-initialised
30struct zero_initialised 175struct zero_initialised
31{ 176{
32 void *operator new (size_t s, void *p) 177 void *operator new (size_t s, void *p)
33 { 178 {
74 219
75// for symmetry 220// for symmetry
76template<typename T> 221template<typename T>
77inline void sfree (T *ptr, int n = 1) throw () 222inline void sfree (T *ptr, int n = 1) throw ()
78{ 223{
224#ifdef PREFER_MALLOC
225 free (ptr);
226#else
79 g_slice_free1 (n * sizeof (T), (void *)ptr); 227 g_slice_free1 (n * sizeof (T), (void *)ptr);
228#endif
80} 229}
81 230
82// a STL-compatible allocator that uses g_slice 231// a STL-compatible allocator that uses g_slice
83// boy, this is verbose 232// boy, this is verbose
84template<typename Tp> 233template<typename Tp>
131 void destroy (pointer p) 280 void destroy (pointer p)
132 { 281 {
133 p->~Tp (); 282 p->~Tp ();
134 } 283 }
135}; 284};
285
286// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
287// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
288// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
289struct tausworthe_random_generator
290{
291 // generator
292 uint32_t state [4];
293
294 void operator =(const tausworthe_random_generator &src)
295 {
296 state [0] = src.state [0];
297 state [1] = src.state [1];
298 state [2] = src.state [2];
299 state [3] = src.state [3];
300 }
301
302 void seed (uint32_t seed);
303 uint32_t next ();
304
305 // uniform distribution
306 uint32_t operator ()(uint32_t num)
307 {
308 return is_constant (num)
309 ? (next () * (uint64_t)num) >> 32U
310 : get_range (num);
311 }
312
313 // return a number within (min .. max)
314 int operator () (int r_min, int r_max)
315 {
316 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
317 ? r_min + operator ()(r_max - r_min + 1)
318 : get_range (r_min, r_max);
319 }
320
321 double operator ()()
322 {
323 return this->next () / (double)0xFFFFFFFFU;
324 }
325
326protected:
327 uint32_t get_range (uint32_t r_max);
328 int get_range (int r_min, int r_max);
329};
330
331typedef tausworthe_random_generator rand_gen;
332
333extern rand_gen rndm;
136 334
137template<class T> 335template<class T>
138struct refptr 336struct refptr
139{ 337{
140 T *p; 338 T *p;
240 } 438 }
241 439
242 void erase (T *obj) 440 void erase (T *obj)
243 { 441 {
244 assert (obj->*index); 442 assert (obj->*index);
245 int pos = obj->*index; 443 unsigned int pos = obj->*index;
246 obj->*index = 0; 444 obj->*index = 0;
247 445
248 if (pos < this->size ()) 446 if (pos < this->size ())
249 { 447 {
250 (*this)[pos - 1] = (*this)[this->size () - 1]; 448 (*this)[pos - 1] = (*this)[this->size () - 1];
258 { 456 {
259 errase (&obj); 457 errase (&obj);
260 } 458 }
261}; 459};
262 460
263template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
264template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
265template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; }
266
267template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
268
269// basically does what strncpy should do, but appends "..." to strings exceeding length 461// basically does what strncpy should do, but appends "..." to strings exceeding length
270void assign (char *dst, const char *src, int maxlen); 462void assign (char *dst, const char *src, int maxlen);
271 463
272// type-safe version of assign 464// type-safe version of assign
273template<int N> 465template<int N>
281// return current time as timestampe 473// return current time as timestampe
282tstamp now (); 474tstamp now ();
283 475
284int similar_direction (int a, int b); 476int similar_direction (int a, int b);
285 477
478// like printf, but returns a std::string
479const std::string format (const char *format, ...);
480
286#endif 481#endif
287 482

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines