… | |
… | |
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 0x00010007 |
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 |
… | |
… | |
680 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
688 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
681 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
689 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
682 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
690 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
683 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
691 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
684 | |
692 | |
|
|
693 | /*****************************************************************************/ |
|
|
694 | /* unaligned load/store */ |
|
|
695 | |
|
|
696 | ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
697 | ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
698 | ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
699 | |
|
|
700 | ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
701 | ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
702 | ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
703 | |
|
|
704 | ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
705 | ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
706 | ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
707 | |
|
|
708 | ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
709 | ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
710 | ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
711 | |
|
|
712 | ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
713 | ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
714 | ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
715 | |
|
|
716 | ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
717 | ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
718 | ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
719 | |
|
|
720 | ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
721 | ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
722 | ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
723 | |
|
|
724 | ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
725 | ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
726 | ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
727 | |
|
|
728 | 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)); } |
|
|
729 | 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)); } |
|
|
730 | 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)); } |
|
|
731 | |
|
|
732 | 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)); } |
|
|
733 | 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)); } |
|
|
734 | 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)); } |
|
|
735 | |
|
|
736 | #ifdef __cplusplus |
|
|
737 | |
|
|
738 | inline uint8_t ecb_bswap (uint8_t v) { return v; } |
|
|
739 | inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); } |
|
|
740 | inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); } |
|
|
741 | inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); } |
|
|
742 | |
|
|
743 | template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
744 | template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
745 | template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; } |
|
|
746 | template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); } |
|
|
747 | template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); } |
|
|
748 | template<typename T> inline T ecb_peek_u (const void *ptr) { T v; std::memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
749 | template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); } |
|
|
750 | template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); } |
|
|
751 | |
|
|
752 | template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
753 | template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
754 | template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; } |
|
|
755 | template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); } |
|
|
756 | template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); } |
|
|
757 | template<typename T> inline void ecb_poke_u (void *ptr, T v) { std::memcpy (ptr, &v, sizeof (v)); } |
|
|
758 | template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); } |
|
|
759 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
|
|
760 | |
|
|
761 | #endif |
|
|
762 | |
|
|
763 | /*****************************************************************************/ |
|
|
764 | |
685 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
765 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
686 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
766 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
687 | #else |
767 | #else |
688 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
768 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
689 | #endif |
769 | #endif |
… | |
… | |
712 | return N; |
792 | return N; |
713 | } |
793 | } |
714 | #else |
794 | #else |
715 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
795 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
716 | #endif |
796 | #endif |
|
|
797 | |
|
|
798 | /*****************************************************************************/ |
717 | |
799 | |
718 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
800 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
719 | ecb_function_ ecb_const uint32_t |
801 | ecb_function_ ecb_const uint32_t |
720 | ecb_binary16_to_binary32 (uint32_t x) |
802 | ecb_binary16_to_binary32 (uint32_t x) |
721 | { |
803 | { |