--- deliantra/server/include/util.h 2007/03/01 12:28:16 1.39 +++ deliantra/server/include/util.h 2007/06/02 03:48:29 1.47 @@ -1,14 +1,50 @@ +/* + * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. + * + * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team + * + * Crossfire TRT 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 2 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * The authors can be reached via e-mail to + */ + #ifndef UTIL_H__ #define UTIL_H__ //#define PREFER_MALLOC #if __GNUC__ >= 3 -# define is_constant(c) __builtin_constant_p (c) +# 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) #else -# define is_constant(c) 0 +# define is_constant(c) 0 +# define expect(expr,value) (expr) +# define prefetch(addr,rw,locality) #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) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + #include #include #include @@ -20,7 +56,7 @@ #include // use a gcc extension for auto declarations until ISO C++ sanctifies them -#define AUTODECL(var,expr) typeof(expr) var = (expr) +#define auto(var,expr) decltype(expr) var = (expr) // very ugly macro that basicaly declares and initialises a variable // that is in scope for the next statement only @@ -47,6 +83,13 @@ template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } +template +static inline T +lerp (T val, T min_in, T max_in, T min_out, T max_out) +{ + return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; +} + // lots of stuff taken from FXT /* Rotate right. This is used in various places for checksumming */ @@ -264,18 +307,18 @@ uint32_t next (); // uniform distribution - uint32_t operator ()(uint32_t r_max) + uint32_t operator ()(uint32_t num) { - return is_constant (r_max) - ? this->next () % r_max - : get_range (r_max); + return is_constant (num) + ? (next () * (uint64_t)num) >> 32U + : get_range (num); } // return a number within (min .. max) int operator () (int r_min, int r_max) { - return is_constant (r_min) && is_constant (r_max) - ? r_min + (*this) (max (r_max - r_min + 1, 1)) + return is_constant (r_min) && is_constant (r_max) && r_min <= r_max + ? r_min + operator ()(r_max - r_min + 1) : get_range (r_min, r_max); } @@ -436,5 +479,8 @@ int similar_direction (int a, int b); +// like printf, but returns a std::string +const std::string format (const char *format, ...); + #endif