… | |
… | |
118 | |
118 | |
119 | // sign0 returns -1, 0 or +1 |
119 | // sign0 returns -1, 0 or +1 |
120 | template<typename T> |
120 | template<typename T> |
121 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
121 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
122 | |
122 | |
|
|
123 | // div* only work correctly for div > 0 |
123 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
124 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
124 | 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 | } |
125 | // div, round-up |
129 | // div, round-up |
126 | 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 | } |
127 | // div, round-down |
134 | // div, round-down |
128 | 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 | } |
129 | |
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 |
130 | template<typename T> |
142 | template<typename T> |
131 | static inline T |
143 | static inline T |
132 | 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) |
133 | { |
145 | { |
134 | 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); |
… | |
… | |
409 | |
421 | |
410 | // Xorshift RNGs, George Marsaglia |
422 | // Xorshift RNGs, George Marsaglia |
411 | // http://www.jstatsoft.org/v08/i14/paper |
423 | // http://www.jstatsoft.org/v08/i14/paper |
412 | // 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), |
413 | // 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 |
414 | struct xorshift_random_generator |
427 | struct xorshift_random_generator |
415 | { |
428 | { |
416 | uint32_t x, y; |
429 | uint32_t x, y; |
417 | |
430 | |
418 | void operator =(const xorshift_random_generator &src) |
431 | void operator =(const xorshift_random_generator &src) |
… | |
… | |
675 | erase (&obj); |
688 | erase (&obj); |
676 | } |
689 | } |
677 | }; |
690 | }; |
678 | |
691 | |
679 | // 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) |
680 | void assign (char *dst, const char *src, int maxlen); |
694 | int assign (char *dst, const char *src, int maxsize); |
681 | |
695 | |
682 | // type-safe version of assign |
696 | // type-safe version of assign |
683 | template<int N> |
697 | template<int N> |
684 | inline void assign (char (&dst)[N], const char *src) |
698 | inline int assign (char (&dst)[N], const char *src) |
685 | { |
699 | { |
686 | assign ((char *)&dst, src, N); |
700 | return assign ((char *)&dst, src, N); |
687 | } |
701 | } |
688 | |
702 | |
689 | typedef double tstamp; |
703 | typedef double tstamp; |
690 | |
704 | |
691 | // return current time as timestamp |
705 | // return current time as timestamp |