… | |
… | |
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 |
… | |
… | |
116 | |
118 | |
117 | // sign0 returns -1, 0 or +1 |
119 | // sign0 returns -1, 0 or +1 |
118 | template<typename T> |
120 | template<typename T> |
119 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
121 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
120 | |
122 | |
|
|
123 | // div* only work correctly for div > 0 |
121 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
124 | // 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; } |
125 | template<typename T> static inline T div (T val, T div) |
|
|
126 | { |
|
|
127 | return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
|
|
128 | } |
123 | // div, round-up |
129 | // div, round-up |
124 | template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } |
130 | template<typename T> static inline T div_ru (T val, T div) |
|
|
131 | { |
|
|
132 | return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
133 | } |
125 | // div, round-down |
134 | // div, round-down |
126 | template<typename T> static inline T div_rd (T val, T div) { return (val ) / div; } |
135 | template<typename T> static inline T div_rd (T val, T div) |
|
|
136 | { |
|
|
137 | return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
|
|
138 | } |
127 | |
139 | |
|
|
140 | // lerp* only work correctly for min_in < max_in |
|
|
141 | // Linear intERPolate, scales val from min_in..max_in to min_out..max_out |
128 | template<typename T> |
142 | template<typename T> |
129 | static inline T |
143 | static inline T |
130 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
144 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
131 | { |
145 | { |
132 | return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in); |
146 | return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in); |
… | |
… | |
407 | |
421 | |
408 | // Xorshift RNGs, George Marsaglia |
422 | // Xorshift RNGs, George Marsaglia |
409 | // http://www.jstatsoft.org/v08/i14/paper |
423 | // http://www.jstatsoft.org/v08/i14/paper |
410 | // this one is about 40% faster than the tausworthe one above (i.e. not much), |
424 | // 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. |
425 | // despite the inlining, and has the issue of only creating 2**32-1 numbers. |
|
|
426 | // see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf |
412 | struct xorshift_random_generator |
427 | struct xorshift_random_generator |
413 | { |
428 | { |
414 | uint32_t x, y; |
429 | uint32_t x, y; |
415 | |
430 | |
416 | void operator =(const xorshift_random_generator &src) |
431 | void operator =(const xorshift_random_generator &src) |
… | |
… | |
673 | erase (&obj); |
688 | erase (&obj); |
674 | } |
689 | } |
675 | }; |
690 | }; |
676 | |
691 | |
677 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
692 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
693 | // returns the number of bytes actually used (including \0) |
678 | void assign (char *dst, const char *src, int maxlen); |
694 | int assign (char *dst, const char *src, int maxsize); |
679 | |
695 | |
680 | // type-safe version of assign |
696 | // type-safe version of assign |
681 | template<int N> |
697 | template<int N> |
682 | inline void assign (char (&dst)[N], const char *src) |
698 | inline int assign (char (&dst)[N], const char *src) |
683 | { |
699 | { |
684 | assign ((char *)&dst, src, N); |
700 | return assign ((char *)&dst, src, N); |
685 | } |
701 | } |
686 | |
702 | |
687 | typedef double tstamp; |
703 | typedef double tstamp; |
688 | |
704 | |
689 | // return current time as timestamp |
705 | // return current time as timestamp |