ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.178 by root, Mon Jun 24 22:22:59 2019 UTC vs.
Revision 1.183 by root, Mon Jan 20 14:10:06 2020 UTC

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
592ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } 608ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
593ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } 609ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
594ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } 610ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
595ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } 611ecb_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
615inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); }
616inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); }
617inline uint32_t ecb_ctz (uint32_t v) { return ecb_ctz32 (v); }
618inline uint64_t ecb_ctz (uint64_t v) { return ecb_ctz64 (v); }
619
620inline bool ecb_is_pot (uint8_t v) { return ecb_is_pot32 (v); }
621inline bool ecb_is_pot (uint16_t v) { return ecb_is_pot32 (v); }
622inline bool ecb_is_pot (uint32_t v) { return ecb_is_pot32 (v); }
623inline bool ecb_is_pot (uint64_t v) { return ecb_is_pot64 (v); }
624
625inline int ecb_ld (uint8_t v) { return ecb_ld32 (v); }
626inline int ecb_ld (uint16_t v) { return ecb_ld32 (v); }
627inline int ecb_ld (uint32_t v) { return ecb_ld32 (v); }
628inline int ecb_ld (uint64_t v) { return ecb_ld64 (v); }
629
630inline int ecb_popcount (uint8_t v) { return ecb_popcount32 (v); }
631inline int ecb_popcount (uint16_t v) { return ecb_popcount32 (v); }
632inline int ecb_popcount (uint32_t v) { return ecb_popcount32 (v); }
633inline int ecb_popcount (uint64_t v) { return ecb_popcount64 (v); }
634
635inline uint8_t ecb_bitrev (uint8_t v) { return ecb_bitrev8 (v); }
636inline uint16_t ecb_bitrev (uint16_t v) { return ecb_bitrev16 (v); }
637inline uint32_t ecb_bitrev (uint32_t v) { return ecb_bitrev32 (v); }
638
639inline uint8_t ecb_rotl (uint8_t v, unsigned int count) { return ecb_rotl8 (v, count); }
640inline uint16_t ecb_rotl (uint16_t v, unsigned int count) { return ecb_rotl16 (v, count); }
641inline uint32_t ecb_rotl (uint32_t v, unsigned int count) { return ecb_rotl32 (v, count); }
642inline uint64_t ecb_rotl (uint64_t v, unsigned int count) { return ecb_rotl64 (v, count); }
643
644inline uint8_t ecb_rotr (uint8_t v, unsigned int count) { return ecb_rotr8 (v, count); }
645inline uint16_t ecb_rotr (uint16_t v, unsigned int count) { return ecb_rotr16 (v, count); }
646inline uint32_t ecb_rotr (uint32_t v, unsigned int count) { return ecb_rotr32 (v, count); }
647inline 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)
672ecb_inline ecb_const ecb_bool ecb_big_endian (void); 726ecb_inline ecb_const ecb_bool ecb_big_endian (void);
673ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } 727ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
674ecb_inline ecb_const ecb_bool ecb_little_endian (void); 728ecb_inline ecb_const ecb_bool ecb_little_endian (void);
675ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } 729ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
676 730
731/*****************************************************************************/
732/* unaligned load/store */
733
734ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
735ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
736ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
737
738ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
739ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
740ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
741
742ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; }
743ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; }
744ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; }
745
746ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); }
747ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); }
748ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); }
749
750ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); }
751ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); }
752ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); }
753
754ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
755ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
756ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
757
758ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
759ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
760ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
761
762ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); }
763ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); }
764ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); }
765
766ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); }
767ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); }
768ecb_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
770ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); }
771ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); }
772ecb_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
776inline uint8_t ecb_bswap (uint8_t v) { return v; }
777inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); }
778inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); }
779inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); }
780
781template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
782template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
783template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; }
784template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); }
785template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); }
786template<typename T> inline T ecb_peek_u (const void *ptr) { T v; std::memcpy (&v, ptr, sizeof (v)); return v; }
787template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); }
788template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); }
789
790template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
791template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
792template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; }
793template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); }
794template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); }
795template<typename T> inline void ecb_poke_u (void *ptr, T v) { std::memcpy (ptr, &v, sizeof (v)); }
796template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); }
797template<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
710ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); 838ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
711ecb_function_ ecb_const uint32_t 839ecb_function_ ecb_const uint32_t
712ecb_binary16_to_binary32 (uint32_t x) 840ecb_binary16_to_binary32 (uint32_t x)
713{ 841{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines