… | |
… | |
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 0x00010006 |
45 | #define ECB_VERSION 0x00010008 |
46 | |
46 | |
47 | #ifdef _WIN32 |
47 | #ifdef _WIN32 |
48 | typedef signed char int8_t; |
48 | typedef signed char int8_t; |
49 | typedef unsigned char uint8_t; |
49 | typedef unsigned char uint8_t; |
|
|
50 | typedef signed char int_fast8_t; |
|
|
51 | typedef unsigned char uint_fast8_t; |
50 | typedef signed short int16_t; |
52 | typedef signed short int16_t; |
51 | typedef unsigned short uint16_t; |
53 | typedef unsigned short uint16_t; |
|
|
54 | typedef signed int int_fast16_t; |
|
|
55 | typedef unsigned int uint_fast16_t; |
52 | typedef signed int int32_t; |
56 | typedef signed int int32_t; |
53 | typedef unsigned int uint32_t; |
57 | typedef unsigned int uint32_t; |
|
|
58 | typedef signed int int_fast32_t; |
|
|
59 | typedef unsigned int uint_fast32_t; |
54 | #if __GNUC__ |
60 | #if __GNUC__ |
55 | typedef signed long long int64_t; |
61 | typedef signed long long int64_t; |
56 | typedef unsigned long long uint64_t; |
62 | typedef unsigned long long uint64_t; |
57 | #else /* _MSC_VER || __BORLANDC__ */ |
63 | #else /* _MSC_VER || __BORLANDC__ */ |
58 | typedef signed __int64 int64_t; |
64 | typedef signed __int64 int64_t; |
59 | typedef unsigned __int64 uint64_t; |
65 | typedef unsigned __int64 uint64_t; |
60 | #endif |
66 | #endif |
|
|
67 | typedef int64_t int_fast64_t; |
|
|
68 | typedef uint64_t uint_fast64_t; |
61 | #ifdef _WIN64 |
69 | #ifdef _WIN64 |
62 | #define ECB_PTRSIZE 8 |
70 | #define ECB_PTRSIZE 8 |
63 | typedef uint64_t uintptr_t; |
71 | typedef uint64_t uintptr_t; |
64 | typedef int64_t intptr_t; |
72 | typedef int64_t intptr_t; |
65 | #else |
73 | #else |
… | |
… | |
76 | #endif |
84 | #endif |
77 | #endif |
85 | #endif |
78 | |
86 | |
79 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
87 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
80 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
88 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
|
|
89 | |
|
|
90 | #ifndef ECB_OPTIMIZE_SIZE |
|
|
91 | #if __OPTIMIZE_SIZE__ |
|
|
92 | #define ECB_OPTIMIZE_SIZE 1 |
|
|
93 | #else |
|
|
94 | #define ECB_OPTIMIZE_SIZE 0 |
|
|
95 | #endif |
|
|
96 | #endif |
81 | |
97 | |
82 | /* work around x32 idiocy by defining proper macros */ |
98 | /* work around x32 idiocy by defining proper macros */ |
83 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
99 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
84 | #if _ILP32 |
100 | #if _ILP32 |
85 | #define ECB_AMD64_X32 1 |
101 | #define ECB_AMD64_X32 1 |
… | |
… | |
592 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
608 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
593 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
609 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
594 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
610 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
595 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
611 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
596 | |
612 | |
|
|
613 | #if ECB_CPP |
|
|
614 | |
|
|
615 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
|
|
616 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
|
|
617 | inline uint32_t ecb_ctz (uint32_t v) { return ecb_ctz32 (v); } |
|
|
618 | inline uint64_t ecb_ctz (uint64_t v) { return ecb_ctz64 (v); } |
|
|
619 | |
|
|
620 | inline bool ecb_is_pot (uint8_t v) { return ecb_is_pot32 (v); } |
|
|
621 | inline bool ecb_is_pot (uint16_t v) { return ecb_is_pot32 (v); } |
|
|
622 | inline bool ecb_is_pot (uint32_t v) { return ecb_is_pot32 (v); } |
|
|
623 | inline bool ecb_is_pot (uint64_t v) { return ecb_is_pot64 (v); } |
|
|
624 | |
|
|
625 | inline int ecb_ld (uint8_t v) { return ecb_ld32 (v); } |
|
|
626 | inline int ecb_ld (uint16_t v) { return ecb_ld32 (v); } |
|
|
627 | inline int ecb_ld (uint32_t v) { return ecb_ld32 (v); } |
|
|
628 | inline int ecb_ld (uint64_t v) { return ecb_ld64 (v); } |
|
|
629 | |
|
|
630 | inline int ecb_popcount (uint8_t v) { return ecb_popcount32 (v); } |
|
|
631 | inline int ecb_popcount (uint16_t v) { return ecb_popcount32 (v); } |
|
|
632 | inline int ecb_popcount (uint32_t v) { return ecb_popcount32 (v); } |
|
|
633 | inline int ecb_popcount (uint64_t v) { return ecb_popcount64 (v); } |
|
|
634 | |
|
|
635 | inline uint8_t ecb_bitrev (uint8_t v) { return ecb_bitrev8 (v); } |
|
|
636 | inline uint16_t ecb_bitrev (uint16_t v) { return ecb_bitrev16 (v); } |
|
|
637 | inline uint32_t ecb_bitrev (uint32_t v) { return ecb_bitrev32 (v); } |
|
|
638 | |
|
|
639 | inline uint8_t ecb_rotl (uint8_t v, unsigned int count) { return ecb_rotl8 (v, count); } |
|
|
640 | inline uint16_t ecb_rotl (uint16_t v, unsigned int count) { return ecb_rotl16 (v, count); } |
|
|
641 | inline uint32_t ecb_rotl (uint32_t v, unsigned int count) { return ecb_rotl32 (v, count); } |
|
|
642 | inline uint64_t ecb_rotl (uint64_t v, unsigned int count) { return ecb_rotl64 (v, count); } |
|
|
643 | |
|
|
644 | inline uint8_t ecb_rotr (uint8_t v, unsigned int count) { return ecb_rotr8 (v, count); } |
|
|
645 | inline uint16_t ecb_rotr (uint16_t v, unsigned int count) { return ecb_rotr16 (v, count); } |
|
|
646 | inline uint32_t ecb_rotr (uint32_t v, unsigned int count) { return ecb_rotr32 (v, count); } |
|
|
647 | inline uint64_t ecb_rotr (uint64_t v, unsigned int count) { return ecb_rotr64 (v, count); } |
|
|
648 | |
|
|
649 | #endif |
|
|
650 | |
597 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
651 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
598 | #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16) |
652 | #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16) |
599 | #define ecb_bswap16(x) __builtin_bswap16 (x) |
653 | #define ecb_bswap16(x) __builtin_bswap16 (x) |
600 | #else |
654 | #else |
601 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
655 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
… | |
… | |
672 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
726 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
673 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
727 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
674 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
728 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
675 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
729 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
676 | |
730 | |
|
|
731 | /*****************************************************************************/ |
|
|
732 | /* unaligned load/store */ |
|
|
733 | |
|
|
734 | ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
735 | ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
736 | ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
737 | |
|
|
738 | ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
739 | ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
740 | ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
741 | |
|
|
742 | ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
743 | ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
744 | ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
745 | |
|
|
746 | ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
747 | ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
748 | ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
749 | |
|
|
750 | ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
751 | ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
752 | ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
753 | |
|
|
754 | ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
755 | ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
756 | ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
757 | |
|
|
758 | ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
759 | ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
760 | ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
761 | |
|
|
762 | ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
763 | ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
764 | ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
765 | |
|
|
766 | ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); } |
|
|
767 | ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); } |
|
|
768 | ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); } |
|
|
769 | |
|
|
770 | ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); } |
|
|
771 | ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); } |
|
|
772 | ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); } |
|
|
773 | |
|
|
774 | #ifdef ECB_CPP |
|
|
775 | |
|
|
776 | inline uint8_t ecb_bswap (uint8_t v) { return v; } |
|
|
777 | inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); } |
|
|
778 | inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); } |
|
|
779 | inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); } |
|
|
780 | |
|
|
781 | template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
782 | template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
783 | template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; } |
|
|
784 | template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); } |
|
|
785 | template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); } |
|
|
786 | template<typename T> inline T ecb_peek_u (const void *ptr) { T v; std::memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
787 | template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); } |
|
|
788 | template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); } |
|
|
789 | |
|
|
790 | template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
791 | template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
792 | template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; } |
|
|
793 | template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); } |
|
|
794 | template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); } |
|
|
795 | template<typename T> inline void ecb_poke_u (void *ptr, T v) { std::memcpy (ptr, &v, sizeof (v)); } |
|
|
796 | template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); } |
|
|
797 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
|
|
798 | |
|
|
799 | #endif |
|
|
800 | |
|
|
801 | /*****************************************************************************/ |
|
|
802 | |
677 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
803 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
678 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
804 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
679 | #else |
805 | #else |
680 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
806 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
681 | #endif |
807 | #endif |
… | |
… | |
704 | return N; |
830 | return N; |
705 | } |
831 | } |
706 | #else |
832 | #else |
707 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
833 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
708 | #endif |
834 | #endif |
|
|
835 | |
|
|
836 | /*****************************************************************************/ |
709 | |
837 | |
710 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
838 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
711 | ecb_function_ ecb_const uint32_t |
839 | ecb_function_ ecb_const uint32_t |
712 | ecb_binary16_to_binary32 (uint32_t x) |
840 | ecb_binary16_to_binary32 (uint32_t x) |
713 | { |
841 | { |