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

Comparing libeio/ecb.h (file contents):
Revision 1.29 by root, Fri Aug 23 07:20:21 2019 UTC vs.
Revision 1.30 by root, Wed Feb 26 15:33:00 2020 UTC

1/* 1/*
2 * libecb - http://software.schmorp.de/pkg/libecb 2 * libecb - http://software.schmorp.de/pkg/libecb
3 * 3 *
4 * Copyright (©) 2009-2015 Marc Alexander Lehmann <libecb@schmorp.de> 4 * Copyright (©) 2009-2015,2018-2020 Marc Alexander Lehmann <libecb@schmorp.de>
5 * Copyright (©) 2011 Emanuele Giaquinta 5 * Copyright (©) 2011 Emanuele Giaquinta
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without modifica- 8 * Redistribution and use in source and binary forms, with or without modifica-
9 * tion, are permitted provided that the following conditions are met: 9 * tion, are permitted provided that the following conditions are met:
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#include <string.h> /* for memcpy */
48
49#if defined (_WIN32) && !defined (__MINGW32__)
48 typedef signed char int8_t; 50 typedef signed char int8_t;
49 typedef unsigned char uint8_t; 51 typedef unsigned char uint8_t;
52 typedef signed char int_fast8_t;
53 typedef unsigned char uint_fast8_t;
50 typedef signed short int16_t; 54 typedef signed short int16_t;
51 typedef unsigned short uint16_t; 55 typedef unsigned short uint16_t;
56 typedef signed int int_fast16_t;
57 typedef unsigned int uint_fast16_t;
52 typedef signed int int32_t; 58 typedef signed int int32_t;
53 typedef unsigned int uint32_t; 59 typedef unsigned int uint32_t;
60 typedef signed int int_fast32_t;
61 typedef unsigned int uint_fast32_t;
54 #if __GNUC__ 62 #if __GNUC__
55 typedef signed long long int64_t; 63 typedef signed long long int64_t;
56 typedef unsigned long long uint64_t; 64 typedef unsigned long long uint64_t;
57 #else /* _MSC_VER || __BORLANDC__ */ 65 #else /* _MSC_VER || __BORLANDC__ */
58 typedef signed __int64 int64_t; 66 typedef signed __int64 int64_t;
59 typedef unsigned __int64 uint64_t; 67 typedef unsigned __int64 uint64_t;
60 #endif 68 #endif
69 typedef int64_t int_fast64_t;
70 typedef uint64_t uint_fast64_t;
61 #ifdef _WIN64 71 #ifdef _WIN64
62 #define ECB_PTRSIZE 8 72 #define ECB_PTRSIZE 8
63 typedef uint64_t uintptr_t; 73 typedef uint64_t uintptr_t;
64 typedef int64_t intptr_t; 74 typedef int64_t intptr_t;
65 #else 75 #else
76 #endif 86 #endif
77#endif 87#endif
78 88
79#define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) 89#define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__)
80#define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) 90#define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64)
91
92#ifndef ECB_OPTIMIZE_SIZE
93 #if __OPTIMIZE_SIZE__
94 #define ECB_OPTIMIZE_SIZE 1
95 #else
96 #define ECB_OPTIMIZE_SIZE 0
97 #endif
98#endif
81 99
82/* work around x32 idiocy by defining proper macros */ 100/* work around x32 idiocy by defining proper macros */
83#if ECB_GCC_AMD64 || ECB_MSVC_AMD64 101#if ECB_GCC_AMD64 || ECB_MSVC_AMD64
84 #if _ILP32 102 #if _ILP32
85 #define ECB_AMD64_X32 1 103 #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); } 610ecb_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); } 611ecb_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); } 612ecb_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); } 613ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
596 614
615#if ECB_CPP
616
617inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); }
618inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); }
619inline uint32_t ecb_ctz (uint32_t v) { return ecb_ctz32 (v); }
620inline uint64_t ecb_ctz (uint64_t v) { return ecb_ctz64 (v); }
621
622inline bool ecb_is_pot (uint8_t v) { return ecb_is_pot32 (v); }
623inline bool ecb_is_pot (uint16_t v) { return ecb_is_pot32 (v); }
624inline bool ecb_is_pot (uint32_t v) { return ecb_is_pot32 (v); }
625inline bool ecb_is_pot (uint64_t v) { return ecb_is_pot64 (v); }
626
627inline int ecb_ld (uint8_t v) { return ecb_ld32 (v); }
628inline int ecb_ld (uint16_t v) { return ecb_ld32 (v); }
629inline int ecb_ld (uint32_t v) { return ecb_ld32 (v); }
630inline int ecb_ld (uint64_t v) { return ecb_ld64 (v); }
631
632inline int ecb_popcount (uint8_t v) { return ecb_popcount32 (v); }
633inline int ecb_popcount (uint16_t v) { return ecb_popcount32 (v); }
634inline int ecb_popcount (uint32_t v) { return ecb_popcount32 (v); }
635inline int ecb_popcount (uint64_t v) { return ecb_popcount64 (v); }
636
637inline uint8_t ecb_bitrev (uint8_t v) { return ecb_bitrev8 (v); }
638inline uint16_t ecb_bitrev (uint16_t v) { return ecb_bitrev16 (v); }
639inline uint32_t ecb_bitrev (uint32_t v) { return ecb_bitrev32 (v); }
640
641inline uint8_t ecb_rotl (uint8_t v, unsigned int count) { return ecb_rotl8 (v, count); }
642inline uint16_t ecb_rotl (uint16_t v, unsigned int count) { return ecb_rotl16 (v, count); }
643inline uint32_t ecb_rotl (uint32_t v, unsigned int count) { return ecb_rotl32 (v, count); }
644inline uint64_t ecb_rotl (uint64_t v, unsigned int count) { return ecb_rotl64 (v, count); }
645
646inline uint8_t ecb_rotr (uint8_t v, unsigned int count) { return ecb_rotr8 (v, count); }
647inline uint16_t ecb_rotr (uint16_t v, unsigned int count) { return ecb_rotr16 (v, count); }
648inline uint32_t ecb_rotr (uint32_t v, unsigned int count) { return ecb_rotr32 (v, count); }
649inline uint64_t ecb_rotr (uint64_t v, unsigned int count) { return ecb_rotr64 (v, count); }
650
651#endif
652
597#if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) 653#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) 654 #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16)
599 #define ecb_bswap16(x) __builtin_bswap16 (x) 655 #define ecb_bswap16(x) __builtin_bswap16 (x)
600 #else 656 #else
601 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) 657 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
672ecb_inline ecb_const ecb_bool ecb_big_endian (void); 728ecb_inline ecb_const ecb_bool ecb_big_endian (void);
673ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } 729ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
674ecb_inline ecb_const ecb_bool ecb_little_endian (void); 730ecb_inline ecb_const ecb_bool ecb_little_endian (void);
675ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } 731ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
676 732
733/*****************************************************************************/
734/* unaligned load/store */
735
736ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
737ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
738ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
739
740ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
741ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
742ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
743
744ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; }
745ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; }
746ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; }
747
748ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); }
749ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); }
750ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); }
751
752ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); }
753ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); }
754ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); }
755
756ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
757ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
758ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
759
760ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
761ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
762ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
763
764ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); }
765ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); }
766ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); }
767
768ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); }
769ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); }
770ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); }
771
772ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); }
773ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); }
774ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); }
775
776#if ECB_CPP
777
778inline uint8_t ecb_bswap (uint8_t v) { return v; }
779inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); }
780inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); }
781inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); }
782
783template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
784template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
785template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; }
786template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); }
787template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); }
788template<typename T> inline T ecb_peek_u (const void *ptr) { T v; memcpy (&v, ptr, sizeof (v)); return v; }
789template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); }
790template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); }
791
792template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
793template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
794template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; }
795template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); }
796template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); }
797template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); }
798template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); }
799template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); }
800
801#endif
802
803/*****************************************************************************/
804
677#if ECB_GCC_VERSION(3,0) || ECB_C99 805#if ECB_GCC_VERSION(3,0) || ECB_C99
678 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 806 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
679#else 807#else
680 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 808 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
681#endif 809#endif
704 return N; 832 return N;
705 } 833 }
706#else 834#else
707 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 835 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
708#endif 836#endif
837
838/*****************************************************************************/
709 839
710ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); 840ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
711ecb_function_ ecb_const uint32_t 841ecb_function_ ecb_const uint32_t
712ecb_binary16_to_binary32 (uint32_t x) 842ecb_binary16_to_binary32 (uint32_t x)
713{ 843{
822 || defined __sh__ \ 952 || defined __sh__ \
823 || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \ 953 || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \
824 || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ 954 || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \
825 || defined __aarch64__ 955 || defined __aarch64__
826 #define ECB_STDFP 1 956 #define ECB_STDFP 1
827 #include <string.h> /* for memcpy */
828#else 957#else
829 #define ECB_STDFP 0 958 #define ECB_STDFP 0
830#endif 959#endif
831 960
832#ifndef ECB_NO_LIBM 961#ifndef ECB_NO_LIBM

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines