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.35 by root, Fri Jan 19 22:47:57 2007 UTC vs.
Revision 1.40 by root, Mon Apr 16 15:41:27 2007 UTC

1#ifndef UTIL_H__ 1#ifndef UTIL_H__
2#define UTIL_H__ 2#define UTIL_H__
3
4//#define PREFER_MALLOC
3 5
4#if __GNUC__ >= 3 6#if __GNUC__ >= 3
5# define is_constant(c) __builtin_constant_p (c) 7# define is_constant(c) __builtin_constant_p (c)
6#else 8#else
7# define is_constant(c) 0 9# define is_constant(c) 0
16 18
17#include <shstr.h> 19#include <shstr.h>
18#include <traits.h> 20#include <traits.h>
19 21
20// 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
21#define AUTODECL(var,expr) typeof(expr) var = (expr) 23#define auto(var,expr) typeof(expr) var = (expr)
22 24
23// very ugly macro that basicaly declares and initialises a variable 25// very ugly macro that basicaly declares and initialises a variable
24// that is in scope for the next statement only 26// that is in scope for the next statement only
25// 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
26// (note: works great for pointers) 28// (note: works great for pointers)
42template<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; }
43template<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; }
44template<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; }
45 47
46template<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
50// lots of stuff taken from FXT
51
52/* Rotate right. This is used in various places for checksumming */
53//TODO: that sucks, use a better checksum algo
54static inline uint32_t
55rotate_right (uint32_t c, uint32_t count = 1)
56{
57 return (c << (32 - count)) | (c >> count);
58}
59
60static inline uint32_t
61rotate_left (uint32_t c, uint32_t count = 1)
62{
63 return (c >> (32 - count)) | (c << count);
64}
65
66// Return abs(a-b)
67// Both a and b must not have the most significant bit set
68static inline uint32_t
69upos_abs_diff (uint32_t a, uint32_t b)
70{
71 long d1 = b - a;
72 long d2 = (d1 & (d1 >> 31)) << 1;
73
74 return d1 - d2; // == (b - d) - (a + d);
75}
76
77// Both a and b must not have the most significant bit set
78static inline uint32_t
79upos_min (uint32_t a, uint32_t b)
80{
81 int32_t d = b - a;
82 d &= d >> 31;
83 return a + d;
84}
85
86// Both a and b must not have the most significant bit set
87static inline uint32_t
88upos_max (uint32_t a, uint32_t b)
89{
90 int32_t d = b - a;
91 d &= d >> 31;
92 return b - d;
93}
47 94
48// this is much faster than crossfires original algorithm 95// this is much faster than crossfires original algorithm
49// on modern cpus 96// on modern cpus
50inline int 97inline int
51isqrt (int n) 98isqrt (int n)
133 180
134// for symmetry 181// for symmetry
135template<typename T> 182template<typename T>
136inline void sfree (T *ptr, int n = 1) throw () 183inline void sfree (T *ptr, int n = 1) throw ()
137{ 184{
185#ifdef PREFER_MALLOC
186 free (ptr);
187#else
138 g_slice_free1 (n * sizeof (T), (void *)ptr); 188 g_slice_free1 (n * sizeof (T), (void *)ptr);
189#endif
139} 190}
140 191
141// a STL-compatible allocator that uses g_slice 192// a STL-compatible allocator that uses g_slice
142// boy, this is verbose 193// boy, this is verbose
143template<typename Tp> 194template<typename Tp>
348 } 399 }
349 400
350 void erase (T *obj) 401 void erase (T *obj)
351 { 402 {
352 assert (obj->*index); 403 assert (obj->*index);
353 int pos = obj->*index; 404 unsigned int pos = obj->*index;
354 obj->*index = 0; 405 obj->*index = 0;
355 406
356 if (pos < this->size ()) 407 if (pos < this->size ())
357 { 408 {
358 (*this)[pos - 1] = (*this)[this->size () - 1]; 409 (*this)[pos - 1] = (*this)[this->size () - 1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines