--- deliantra/server/include/util.h 2009/01/12 03:40:21 1.87
+++ deliantra/server/include/util.h 2009/11/08 16:13:45 1.94
@@ -3,18 +3,19 @@
*
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
*
- * Deliantra is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * Deliantra is free software: you can redistribute it and/or modify it under
+ * the terms of the Affero GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
+ * You should have received a copy of the Affero GNU General Public License
+ * and the GNU General Public License along with this program. If not, see
+ * .
*
* The authors can be reached via e-mail to
*/
@@ -22,32 +23,12 @@
#ifndef UTIL_H__
#define UTIL_H__
+#include
+
#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0
#define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs
#define PREFER_MALLOC 0 // use malloc and not the slice allocator
-#if __GNUC__ >= 3
-# define is_constant(c) __builtin_constant_p (c)
-# define expect(expr,value) __builtin_expect ((expr),(value))
-# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
-# define noinline __attribute__((__noinline__))
-#else
-# define is_constant(c) 0
-# define expect(expr,value) (expr)
-# define prefetch(addr,rw,locality)
-# define noinline
-#endif
-
-#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4)
-# define decltype(x) typeof(x)
-#endif
-
-// put into ifs if you are very sure that the expression
-// is mostly true or mosty false. note that these return
-// booleans, not the expression.
-#define expect_false(expr) expect ((expr) ? 1 : 0, 0)
-#define expect_true(expr) expect ((expr) ? 1 : 0, 1)
-
#include
#include
@@ -120,13 +101,25 @@
template
static inline T sign0 (T v) { return v ? sign (v) : 0; }
+// div* only work correctly for div > 0
// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
-template static inline T div (T val, T div) { return (val + div / 2) / div; }
+template static inline T div (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div;
+}
// div, round-up
-template static inline T div_ru (T val, T div) { return (val + div - 1) / div; }
+template static inline T div_ru (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div;
+}
// div, round-down
-template static inline T div_rd (T val, T div) { return (val ) / div; }
+template static inline T div_rd (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div;
+}
+// lerp* only work correctly for min_in < max_in
+// Linear intERPolate, scales val from min_in..max_in to min_out..max_out
template
static inline T
lerp (T val, T min_in, T max_in, T min_out, T max_out)
@@ -195,7 +188,7 @@
return b - d;
}
-// this is much faster than crossfires original algorithm
+// this is much faster than crossfire's original algorithm
// on modern cpus
inline int
isqrt (int n)
@@ -203,6 +196,20 @@
return (int)sqrtf ((float)n);
}
+// this is kind of like the ^^ operator, if it would exist, without sequence point.
+// more handy than it looks like, due to the implicit !! done on its arguments
+inline bool
+logical_xor (bool a, bool b)
+{
+ return a != b;
+}
+
+inline bool
+logical_implies (bool a, bool b)
+{
+ return a <= b;
+}
+
// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
#if 0
// and has a max. error of 6 in the range -100..+100.
@@ -695,8 +702,12 @@
int similar_direction (int a, int b);
-// like sprintf, but returns a "static" buffer
-const char *format (const char *format, ...);
+// like v?sprintf, but returns a "static" buffer
+char *vformat (const char *format, va_list ap);
+char *format (const char *format, ...) attribute ((format (printf, 1, 2)));
+
+// safety-check player input which will become object->msg
+bool msg_is_safe (const char *msg);
/////////////////////////////////////////////////////////////////////////////
// threads, very very thin wrappers around pthreads