… | |
… | |
99 | |
99 | |
100 | // sign0 returns -1, 0 or +1 |
100 | // sign0 returns -1, 0 or +1 |
101 | template<typename T> |
101 | template<typename T> |
102 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
102 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
103 | |
103 | |
|
|
104 | template<typename T, typename U> |
|
|
105 | static inline T copysign (T a, U b) { return a > 0 ? b : -b; } |
|
|
106 | |
104 | // div* only work correctly for div > 0 |
107 | // div* only work correctly for div > 0 |
105 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
108 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
106 | template<typename T> static inline T div (T val, T div) |
109 | template<typename T> static inline T div (T val, T div) |
107 | { |
110 | { |
108 | return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
111 | return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
… | |
… | |
467 | return !is_constant (num) ? get_range (num) // non-constant |
470 | return !is_constant (num) ? get_range (num) // non-constant |
468 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
471 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
469 | : this->next () & (num - 1); // constant, power-of-two |
472 | : this->next () & (num - 1); // constant, power-of-two |
470 | } |
473 | } |
471 | |
474 | |
472 | // return a number within (min .. max) |
475 | // return a number within the closed interval [min .. max] |
473 | int operator () (int r_min, int r_max) |
476 | int operator () (int r_min, int r_max) |
474 | { |
477 | { |
475 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
478 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
476 | ? r_min + operator ()(r_max - r_min + 1) |
479 | ? r_min + operator ()(r_max - r_min + 1) |
477 | : get_range (r_min, r_max); |
480 | : get_range (r_min, r_max); |
478 | } |
481 | } |
479 | |
482 | |
|
|
483 | // return a number within the closed interval [0..1] |
480 | double operator ()() |
484 | double operator ()() |
481 | { |
485 | { |
482 | return this->next () / (double)0xFFFFFFFFU; |
486 | return this->next () / (double)0xFFFFFFFFU; |
483 | } |
487 | } |
484 | |
488 | |
… | |
… | |
573 | // FNV-1a is faster on many cpus because the multiplication |
577 | // FNV-1a is faster on many cpus because the multiplication |
574 | // runs concurrently with the looping logic. |
578 | // runs concurrently with the looping logic. |
575 | uint32_t hash = STRHSH_NULL; |
579 | uint32_t hash = STRHSH_NULL; |
576 | |
580 | |
577 | while (*s) |
581 | while (*s) |
578 | hash = (hash ^ *s++) * 16777619; |
582 | hash = (hash ^ *s++) * 16777619U; |
579 | |
583 | |
580 | return hash; |
584 | return hash; |
581 | } |
585 | } |
582 | |
586 | |
583 | static inline uint32_t |
587 | static inline uint32_t |
584 | memhsh (const char *s, size_t len) |
588 | memhsh (const char *s, size_t len) |
585 | { |
589 | { |
586 | uint32_t hash = STRHSH_NULL; |
590 | uint32_t hash = STRHSH_NULL; |
587 | |
591 | |
588 | while (len--) |
592 | while (len--) |
589 | hash = (hash ^ *s++) * 16777619; |
593 | hash = (hash ^ *s++) * 16777619U; |
590 | |
594 | |
591 | return hash; |
595 | return hash; |
592 | } |
596 | } |
593 | |
597 | |
594 | struct str_hash |
598 | struct str_hash |