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.83 by root, Tue Dec 30 07:24:16 2008 UTC vs.
Revision 1.87 by root, Mon Jan 12 03:40:21 2009 UTC

28 28
29#if __GNUC__ >= 3 29#if __GNUC__ >= 3
30# define is_constant(c) __builtin_constant_p (c) 30# define is_constant(c) __builtin_constant_p (c)
31# define expect(expr,value) __builtin_expect ((expr),(value)) 31# define expect(expr,value) __builtin_expect ((expr),(value))
32# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 32# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
33# define noinline __attribute__((__noinline__))
33#else 34#else
34# define is_constant(c) 0 35# define is_constant(c) 0
35# define expect(expr,value) (expr) 36# define expect(expr,value) (expr)
36# define prefetch(addr,rw,locality) 37# define prefetch(addr,rw,locality)
38# define noinline
37#endif 39#endif
38 40
39#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) 41#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4)
40# define decltype(x) typeof(x) 42# define decltype(x) typeof(x)
41#endif 43#endif
42 44
43// put into ifs if you are very sure that the expression 45// put into ifs if you are very sure that the expression
44// is mostly true or mosty false. note that these return 46// is mostly true or mosty false. note that these return
45// booleans, not the expression. 47// booleans, not the expression.
46#define expect_false(expr) expect ((expr) != 0, 0) 48#define expect_false(expr) expect ((expr) ? 1 : 0, 0)
47#define expect_true(expr) expect ((expr) != 0, 1) 49#define expect_true(expr) expect ((expr) ? 1 : 0, 1)
48 50
49#include <pthread.h> 51#include <pthread.h>
50 52
51#include <cstddef> 53#include <cstddef>
52#include <cmath> 54#include <cmath>
407 409
408// Xorshift RNGs, George Marsaglia 410// Xorshift RNGs, George Marsaglia
409// http://www.jstatsoft.org/v08/i14/paper 411// http://www.jstatsoft.org/v08/i14/paper
410// this one is about 40% faster than the tausworthe one above (i.e. not much), 412// this one is about 40% faster than the tausworthe one above (i.e. not much),
411// despite the inlining, and has the issue of only creating 2**32-1 numbers. 413// despite the inlining, and has the issue of only creating 2**32-1 numbers.
414// see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
412struct xorshift_random_generator 415struct xorshift_random_generator
413{ 416{
414 uint32_t x, y; 417 uint32_t x, y;
415 418
416 void operator =(const xorshift_random_generator &src) 419 void operator =(const xorshift_random_generator &src)
540 543
541struct str_hash 544struct str_hash
542{ 545{
543 std::size_t operator ()(const char *s) const 546 std::size_t operator ()(const char *s) const
544 { 547 {
545 unsigned long hash = 0; 548#if 0
549 uint32_t hash = 0;
546 550
547 /* use the one-at-a-time hash function, which supposedly is 551 /* use the one-at-a-time hash function, which supposedly is
548 * better than the djb2-like one used by perl5.005, but 552 * better than the djb2-like one used by perl5.005, but
549 * certainly is better then the bug used here before. 553 * certainly is better then the bug used here before.
550 * see http://burtleburtle.net/bob/hash/doobs.html 554 * see http://burtleburtle.net/bob/hash/doobs.html
557 } 561 }
558 562
559 hash += hash << 3; 563 hash += hash << 3;
560 hash ^= hash >> 11; 564 hash ^= hash >> 11;
561 hash += hash << 15; 565 hash += hash << 15;
566#else
567 // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/)
568 // it is about twice as fast as the one-at-a-time one,
569 // with good distribution.
570 // FNV-1a is faster on many cpus because the multiplication
571 // runs concurrent with the looping logic.
572 uint32_t hash = 2166136261;
573
574 while (*s)
575 hash = (hash ^ *s++) * 16777619;
576#endif
562 577
563 return hash; 578 return hash;
564 } 579 }
565}; 580};
566 581
661 erase (&obj); 676 erase (&obj);
662 } 677 }
663}; 678};
664 679
665// basically does what strncpy should do, but appends "..." to strings exceeding length 680// basically does what strncpy should do, but appends "..." to strings exceeding length
681// returns the number of bytes actually used (including \0)
666void assign (char *dst, const char *src, int maxlen); 682int assign (char *dst, const char *src, int maxsize);
667 683
668// type-safe version of assign 684// type-safe version of assign
669template<int N> 685template<int N>
670inline void assign (char (&dst)[N], const char *src) 686inline int assign (char (&dst)[N], const char *src)
671{ 687{
672 assign ((char *)&dst, src, N); 688 return assign ((char *)&dst, src, N);
673} 689}
674 690
675typedef double tstamp; 691typedef double tstamp;
676 692
677// return current time as timestamp 693// return current time as timestamp

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines