1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the Free Software Foundation, either version 3 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * (at your option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * and the GNU General Public License along with this program. If not, see |
|
|
18 | * <http://www.gnu.org/licenses/>. |
18 | * |
19 | * |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
21 | */ |
21 | |
22 | |
22 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
… | |
… | |
28 | |
29 | |
29 | #if __GNUC__ >= 3 |
30 | #if __GNUC__ >= 3 |
30 | # define is_constant(c) __builtin_constant_p (c) |
31 | # define is_constant(c) __builtin_constant_p (c) |
31 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
32 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
32 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
33 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
34 | # define noinline __attribute__((__noinline__)) |
33 | #else |
35 | #else |
34 | # define is_constant(c) 0 |
36 | # define is_constant(c) 0 |
35 | # define expect(expr,value) (expr) |
37 | # define expect(expr,value) (expr) |
36 | # define prefetch(addr,rw,locality) |
38 | # define prefetch(addr,rw,locality) |
|
|
39 | # define noinline |
37 | #endif |
40 | #endif |
38 | |
41 | |
39 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
42 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
40 | # define decltype(x) typeof(x) |
43 | # define decltype(x) typeof(x) |
41 | #endif |
44 | #endif |
42 | |
45 | |
43 | // put into ifs if you are very sure that the expression |
46 | // put into ifs if you are very sure that the expression |
44 | // is mostly true or mosty false. note that these return |
47 | // is mostly true or mosty false. note that these return |
45 | // booleans, not the expression. |
48 | // booleans, not the expression. |
46 | #define expect_false(expr) expect ((expr) != 0, 0) |
49 | #define expect_false(expr) expect ((expr) ? 1 : 0, 0) |
47 | #define expect_true(expr) expect ((expr) != 0, 1) |
50 | #define expect_true(expr) expect ((expr) ? 1 : 0, 1) |
48 | |
51 | |
49 | #include <pthread.h> |
52 | #include <pthread.h> |
50 | |
53 | |
51 | #include <cstddef> |
54 | #include <cstddef> |
52 | #include <cmath> |
55 | #include <cmath> |
… | |
… | |
116 | |
119 | |
117 | // sign0 returns -1, 0 or +1 |
120 | // sign0 returns -1, 0 or +1 |
118 | template<typename T> |
121 | template<typename T> |
119 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
122 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
120 | |
123 | |
|
|
124 | // div* only work correctly for div > 0 |
121 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
125 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
122 | template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; } |
126 | template<typename T> static inline T div (T val, T div) |
|
|
127 | { |
|
|
128 | return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
|
|
129 | } |
123 | // div, round-up |
130 | // div, round-up |
124 | template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } |
131 | template<typename T> static inline T div_ru (T val, T div) |
|
|
132 | { |
|
|
133 | return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
134 | } |
125 | // div, round-down |
135 | // div, round-down |
126 | template<typename T> static inline T div_rd (T val, T div) { return (val ) / div; } |
136 | template<typename T> static inline T div_rd (T val, T div) |
|
|
137 | { |
|
|
138 | return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
|
|
139 | } |
127 | |
140 | |
|
|
141 | // lerp* only work correctly for min_in < max_in |
|
|
142 | // Linear intERPolate, scales val from min_in..max_in to min_out..max_out |
128 | template<typename T> |
143 | template<typename T> |
129 | static inline T |
144 | static inline T |
130 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
145 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
131 | { |
146 | { |
132 | return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in); |
147 | return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in); |
… | |
… | |
389 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
404 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
390 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
405 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
391 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
406 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
392 | struct tausworthe_random_generator |
407 | struct tausworthe_random_generator |
393 | { |
408 | { |
394 | // generator |
|
|
395 | uint32_t state [4]; |
409 | uint32_t state [4]; |
396 | |
410 | |
397 | void operator =(const tausworthe_random_generator &src) |
411 | void operator =(const tausworthe_random_generator &src) |
398 | { |
412 | { |
399 | state [0] = src.state [0]; |
413 | state [0] = src.state [0]; |
… | |
… | |
402 | state [3] = src.state [3]; |
416 | state [3] = src.state [3]; |
403 | } |
417 | } |
404 | |
418 | |
405 | void seed (uint32_t seed); |
419 | void seed (uint32_t seed); |
406 | uint32_t next (); |
420 | uint32_t next (); |
|
|
421 | }; |
407 | |
422 | |
|
|
423 | // Xorshift RNGs, George Marsaglia |
|
|
424 | // http://www.jstatsoft.org/v08/i14/paper |
|
|
425 | // this one is about 40% faster than the tausworthe one above (i.e. not much), |
|
|
426 | // despite the inlining, and has the issue of only creating 2**32-1 numbers. |
|
|
427 | // see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf |
|
|
428 | struct xorshift_random_generator |
|
|
429 | { |
|
|
430 | uint32_t x, y; |
|
|
431 | |
|
|
432 | void operator =(const xorshift_random_generator &src) |
|
|
433 | { |
|
|
434 | x = src.x; |
|
|
435 | y = src.y; |
|
|
436 | } |
|
|
437 | |
|
|
438 | void seed (uint32_t seed) |
|
|
439 | { |
|
|
440 | x = seed; |
|
|
441 | y = seed * 69069U; |
|
|
442 | } |
|
|
443 | |
|
|
444 | uint32_t next () |
|
|
445 | { |
|
|
446 | uint32_t t = x ^ (x << 10); |
|
|
447 | x = y; |
|
|
448 | y = y ^ (y >> 13) ^ t ^ (t >> 10); |
|
|
449 | return y; |
|
|
450 | } |
|
|
451 | }; |
|
|
452 | |
|
|
453 | template<class generator> |
|
|
454 | struct random_number_generator : generator |
|
|
455 | { |
408 | // uniform distribution, 0 .. max (0, num - 1) |
456 | // uniform distribution, 0 .. max (0, num - 1) |
409 | uint32_t operator ()(uint32_t num) |
457 | uint32_t operator ()(uint32_t num) |
410 | { |
458 | { |
411 | return !is_constant (num) ? get_range (num) // non-constant |
459 | return !is_constant (num) ? get_range (num) // non-constant |
412 | : num & (num - 1) ? (next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
460 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
413 | : next () & (num - 1); // constant, power-of-two |
461 | : this->next () & (num - 1); // constant, power-of-two |
414 | } |
462 | } |
415 | |
463 | |
416 | // return a number within (min .. max) |
464 | // return a number within (min .. max) |
417 | int operator () (int r_min, int r_max) |
465 | int operator () (int r_min, int r_max) |
418 | { |
466 | { |
… | |
… | |
429 | protected: |
477 | protected: |
430 | uint32_t get_range (uint32_t r_max); |
478 | uint32_t get_range (uint32_t r_max); |
431 | int get_range (int r_min, int r_max); |
479 | int get_range (int r_min, int r_max); |
432 | }; |
480 | }; |
433 | |
481 | |
434 | typedef tausworthe_random_generator rand_gen; |
482 | typedef random_number_generator<tausworthe_random_generator> rand_gen; |
435 | |
483 | |
436 | extern rand_gen rndm, rmg_rndm; |
484 | extern rand_gen rndm, rmg_rndm; |
437 | |
485 | |
438 | INTERFACE_CLASS (attachable) |
486 | INTERFACE_CLASS (attachable) |
439 | struct refcnt_base |
487 | struct refcnt_base |
… | |
… | |
508 | |
556 | |
509 | struct str_hash |
557 | struct str_hash |
510 | { |
558 | { |
511 | std::size_t operator ()(const char *s) const |
559 | std::size_t operator ()(const char *s) const |
512 | { |
560 | { |
513 | unsigned long hash = 0; |
561 | #if 0 |
|
|
562 | uint32_t hash = 0; |
514 | |
563 | |
515 | /* use the one-at-a-time hash function, which supposedly is |
564 | /* use the one-at-a-time hash function, which supposedly is |
516 | * better than the djb2-like one used by perl5.005, but |
565 | * better than the djb2-like one used by perl5.005, but |
517 | * certainly is better then the bug used here before. |
566 | * certainly is better then the bug used here before. |
518 | * see http://burtleburtle.net/bob/hash/doobs.html |
567 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
525 | } |
574 | } |
526 | |
575 | |
527 | hash += hash << 3; |
576 | hash += hash << 3; |
528 | hash ^= hash >> 11; |
577 | hash ^= hash >> 11; |
529 | hash += hash << 15; |
578 | hash += hash << 15; |
|
|
579 | #else |
|
|
580 | // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) |
|
|
581 | // it is about twice as fast as the one-at-a-time one, |
|
|
582 | // with good distribution. |
|
|
583 | // FNV-1a is faster on many cpus because the multiplication |
|
|
584 | // runs concurrent with the looping logic. |
|
|
585 | uint32_t hash = 2166136261; |
|
|
586 | |
|
|
587 | while (*s) |
|
|
588 | hash = (hash ^ *s++) * 16777619; |
|
|
589 | #endif |
530 | |
590 | |
531 | return hash; |
591 | return hash; |
532 | } |
592 | } |
533 | }; |
593 | }; |
534 | |
594 | |
… | |
… | |
629 | erase (&obj); |
689 | erase (&obj); |
630 | } |
690 | } |
631 | }; |
691 | }; |
632 | |
692 | |
633 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
693 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
694 | // returns the number of bytes actually used (including \0) |
634 | void assign (char *dst, const char *src, int maxlen); |
695 | int assign (char *dst, const char *src, int maxsize); |
635 | |
696 | |
636 | // type-safe version of assign |
697 | // type-safe version of assign |
637 | template<int N> |
698 | template<int N> |
638 | inline void assign (char (&dst)[N], const char *src) |
699 | inline int assign (char (&dst)[N], const char *src) |
639 | { |
700 | { |
640 | assign ((char *)&dst, src, N); |
701 | return assign ((char *)&dst, src, N); |
641 | } |
702 | } |
642 | |
703 | |
643 | typedef double tstamp; |
704 | typedef double tstamp; |
644 | |
705 | |
645 | // return current time as timestamp |
706 | // return current time as timestamp |
646 | tstamp now (); |
707 | tstamp now (); |
647 | |
708 | |
648 | int similar_direction (int a, int b); |
709 | int similar_direction (int a, int b); |
649 | |
710 | |
650 | // like sprintf, but returns a "static" buffer |
711 | // like v?sprintf, but returns a "static" buffer |
|
|
712 | char *vformat (const char *format, va_list ap); |
651 | const char *format (const char *format, ...); |
713 | char *format (const char *format, ...); |
|
|
714 | |
|
|
715 | // safety-check player input which will become object->msg |
|
|
716 | bool msg_is_safe (const char *msg); |
652 | |
717 | |
653 | ///////////////////////////////////////////////////////////////////////////// |
718 | ///////////////////////////////////////////////////////////////////////////// |
654 | // threads, very very thin wrappers around pthreads |
719 | // threads, very very thin wrappers around pthreads |
655 | |
720 | |
656 | struct thread |
721 | struct thread |