--- deliantra/server/include/util.h 2009/11/07 18:30:05 1.93 +++ deliantra/server/include/util.h 2010/03/26 01:04:44 1.97 @@ -1,7 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2005,2006,2007,2008,2009,2010 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 Affero GNU General Public License as published by the @@ -188,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) @@ -242,6 +242,20 @@ return ((d - 1) & 7) + 1; } +// avoid ctz name because netbsd or freebsd spams it's namespace with it +#if GCC_VERSION(3,4) +static inline int least_significant_bit (uint32_t x) +{ + return __builtin_ctz (x); +} +#else +int least_significant_bit (uint32_t x); +#endif + +#define for_all_bits_sparse_32(mask, idxvar) \ + for (uint32_t idxvar, mask_ = mask; \ + mask_ && ((idxvar = least_significant_bit (mask_)), mask_ &= ~(1 << idxvar), 1);) + extern ssize_t slice_alloc; // statistics void *salloc_ (int n) throw (std::bad_alloc); @@ -548,41 +562,45 @@ typedef refptr client_ptr; typedef refptr player_ptr; +#define STRHSH_NULL 2166136261 + +static inline uint32_t +strhsh (const char *s) +{ + // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) + // it is about twice as fast as the one-at-a-time one, + // with good distribution. + // FNV-1a is faster on many cpus because the multiplication + // runs concurrently with the looping logic. + uint32_t hash = STRHSH_NULL; + + while (*s) + hash = (hash ^ *s++) * 16777619; + + return hash; +} + +static inline uint32_t +memhsh (const char *s, size_t len) +{ + uint32_t hash = STRHSH_NULL; + + while (len--) + hash = (hash ^ *s++) * 16777619; + + return hash; +} + struct str_hash { std::size_t operator ()(const char *s) const { -#if 0 - uint32_t hash = 0; - - /* use the one-at-a-time hash function, which supposedly is - * better than the djb2-like one used by perl5.005, but - * certainly is better then the bug used here before. - * see http://burtleburtle.net/bob/hash/doobs.html - */ - while (*s) - { - hash += *s++; - hash += hash << 10; - hash ^= hash >> 6; - } - - hash += hash << 3; - hash ^= hash >> 11; - hash += hash << 15; -#else - // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) - // it is about twice as fast as the one-at-a-time one, - // with good distribution. - // FNV-1a is faster on many cpus because the multiplication - // runs concurrent with the looping logic. - uint32_t hash = 2166136261; - - while (*s) - hash = (hash ^ *s++) * 16777619; -#endif + return strhsh (s); + } - return hash; + std::size_t operator ()(const shstr &s) const + { + return strhsh (s); } };