… | |
… | |
40 | |
40 | |
41 | #ifndef ECB_H |
41 | #ifndef ECB_H |
42 | #define ECB_H |
42 | #define ECB_H |
43 | |
43 | |
44 | /* 16 bits major, 16 bits minor */ |
44 | /* 16 bits major, 16 bits minor */ |
45 | #define ECB_VERSION 0x00010009 |
45 | #define ECB_VERSION 0x0001000a |
46 | |
46 | |
47 | #include <string.h> /* for memcpy */ |
47 | #include <string.h> /* for memcpy */ |
48 | |
48 | |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
50 | typedef signed char int8_t; |
50 | typedef signed char int8_t; |
… | |
… | |
807 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
807 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
808 | |
808 | |
809 | #endif |
809 | #endif |
810 | |
810 | |
811 | /*****************************************************************************/ |
811 | /*****************************************************************************/ |
|
|
812 | /* pointer/integer hashing */ |
|
|
813 | |
|
|
814 | /* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */ |
|
|
815 | ecb_function_ uint32_t ecb_mix32 (uint32_t v); |
|
|
816 | ecb_function_ uint32_t ecb_mix32 (uint32_t v) |
|
|
817 | { |
|
|
818 | v ^= v >> 16; v *= 0x7feb352dU; |
|
|
819 | v ^= v >> 15; v *= 0x846ca68bU; |
|
|
820 | v ^= v >> 16; |
|
|
821 | return v; |
|
|
822 | } |
|
|
823 | |
|
|
824 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v); |
|
|
825 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v) |
|
|
826 | { |
|
|
827 | v ^= v >> 16 ; v *= 0x43021123U; |
|
|
828 | v ^= v >> 15 ^ v >> 30; v *= 0x1d69e2a5U; |
|
|
829 | v ^= v >> 16 ; |
|
|
830 | return v; |
|
|
831 | } |
|
|
832 | |
|
|
833 | /* based on splitmix64, by Sebastiona Vigna, https://prng.di.unimi.it/splitmix64.c */ |
|
|
834 | ecb_function_ uint64_t ecb_mix64 (uint64_t v); |
|
|
835 | ecb_function_ uint64_t ecb_mix64 (uint64_t v) |
|
|
836 | { |
|
|
837 | v ^= v >> 30; v *= 0xbf58476d1ce4e5b9U; |
|
|
838 | v ^= v >> 27; v *= 0x94d049bb133111ebU; |
|
|
839 | v ^= v >> 31; |
|
|
840 | return v; |
|
|
841 | } |
|
|
842 | |
|
|
843 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v); |
|
|
844 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v) |
|
|
845 | { |
|
|
846 | v ^= v >> 31 ^ v >> 62; v *= 0x319642b2d24d8ec3U; |
|
|
847 | v ^= v >> 27 ^ v >> 54; v *= 0x96de1b173f119089U; |
|
|
848 | v ^= v >> 30 ^ v >> 60; |
|
|
849 | return v; |
|
|
850 | } |
|
|
851 | |
|
|
852 | ecb_function_ uintptr_t ecb_ptrmix (void *p); |
|
|
853 | ecb_function_ uintptr_t ecb_ptrmix (void *p) |
|
|
854 | { |
|
|
855 | #if ECB_PTRSIZE <= 4 |
|
|
856 | return ecb_mix32 ((uint32_t)p); |
|
|
857 | #else |
|
|
858 | return ecb_mix64 ((uint64_t)p); |
|
|
859 | #endif |
|
|
860 | } |
|
|
861 | |
|
|
862 | ecb_function_ void *ecb_ptrunmix (uintptr_t v); |
|
|
863 | ecb_function_ void *ecb_ptrunmix (uintptr_t v) |
|
|
864 | { |
|
|
865 | #if ECB_PTRSIZE <= 4 |
|
|
866 | return (void *)ecb_unmix32 (v); |
|
|
867 | #else |
|
|
868 | return (void *)ecb_unmix64 (v); |
|
|
869 | #endif |
|
|
870 | } |
|
|
871 | |
|
|
872 | #if ECB_CPP |
|
|
873 | |
|
|
874 | template<typename T> |
|
|
875 | inline uintptr_t ecb_ptrmix (T *p) |
|
|
876 | { |
|
|
877 | return ecb_ptrmix (static_cast<void *>(p)); |
|
|
878 | } |
|
|
879 | |
|
|
880 | template<typename T> |
|
|
881 | inline T *ecb_ptrunmix (uintptr_t v) |
|
|
882 | { |
|
|
883 | return static_cast<T *>(ecb_ptrunmix (v)); |
|
|
884 | } |
|
|
885 | |
|
|
886 | #endif |
|
|
887 | |
|
|
888 | /*****************************************************************************/ |
812 | /* division */ |
889 | /* division */ |
813 | |
890 | |
814 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
891 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
815 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
892 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
816 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
893 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |