… | |
… | |
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; |
… | |
… | |
573 | // FNV-1a is faster on many cpus because the multiplication |
576 | // FNV-1a is faster on many cpus because the multiplication |
574 | // runs concurrently with the looping logic. |
577 | // runs concurrently with the looping logic. |
575 | uint32_t hash = STRHSH_NULL; |
578 | uint32_t hash = STRHSH_NULL; |
576 | |
579 | |
577 | while (*s) |
580 | while (*s) |
578 | hash = (hash ^ *s++) * 16777619; |
581 | hash = (hash ^ *s++) * 16777619U; |
579 | |
582 | |
580 | return hash; |
583 | return hash; |
581 | } |
584 | } |
582 | |
585 | |
583 | static inline uint32_t |
586 | static inline uint32_t |
584 | memhsh (const char *s, size_t len) |
587 | memhsh (const char *s, size_t len) |
585 | { |
588 | { |
586 | uint32_t hash = STRHSH_NULL; |
589 | uint32_t hash = STRHSH_NULL; |
587 | |
590 | |
588 | while (len--) |
591 | while (len--) |
589 | hash = (hash ^ *s++) * 16777619; |
592 | hash = (hash ^ *s++) * 16777619U; |
590 | |
593 | |
591 | return hash; |
594 | return hash; |
592 | } |
595 | } |
593 | |
596 | |
594 | struct str_hash |
597 | struct str_hash |