25 #ifndef XAPIAN_INCLUDED_WORDACCESS_H 26 #define XAPIAN_INCLUDED_WORDACCESS_H 29 # error config.h must be included first in each C++ source file 33 #include <type_traits> 39 #ifndef WORDS_BIGENDIAN 41 #if HAVE_DECL__BYTESWAP_USHORT || HAVE_DECL__BYTESWAP_ULONG 46 # if HAVE_DECL___BUILTIN_BSWAP16 47 return __builtin_bswap16(value);
48 # elif HAVE_DECL__BYTESWAP_USHORT 49 return _byteswap_ushort(value);
51 return (value << 8) | (value >> 8);
56 # if HAVE_DECL___BUILTIN_BSWAP32 57 return __builtin_bswap32(value);
58 # elif HAVE_DECL__BYTESWAP_ULONG 59 return _byteswap_ulong(value);
61 return (value << 24) |
62 ((value & 0xff00) << 8) |
63 ((value >> 8) & 0xff00) |
69 # if HAVE_DECL___BUILTIN_BSWAP64 70 return __builtin_bswap64(value);
71 # elif HAVE_DECL__BYTESWAP_UINT64 72 return _byteswap_uint64(value);
74 return (value << 56) |
75 ((value & 0xff00) << 40) |
76 ((value & 0xff0000) << 24) |
77 ((value & 0xff000000) << 8) |
78 ((value >> 8) & 0xff000000) |
79 ((value >> 24) & 0xff0000) |
80 ((value >> 40) & 0xff00) |
87 template<
typename UINT>
92 #ifndef WORDS_BIGENDIAN 98 template<
typename T,
typename UINT>
102 if (std::is_signed<T>::value) {
105 if (
sizeof(T) >
sizeof(UINT)) {
108 UINT v = UINT(value);
109 #ifndef WORDS_BIGENDIAN 115 template<
typename UINT>
120 memcpy(&value, ptr,
sizeof(UINT));
121 #ifndef WORDS_BIGENDIAN 127 template<
typename T,
typename UINT>
131 if (std::is_signed<T>::value) {
134 if (
sizeof(T) >
sizeof(UINT)) {
137 UINT v = UINT(value);
138 #ifndef WORDS_BIGENDIAN 141 memcpy(ptr, &v,
sizeof(UINT));
147 return do_aligned_read<uint32_t>(ptr);
153 return do_unaligned_read<uint32_t>(ptr);
159 return do_aligned_read<uint16_t>(ptr);
165 return do_unaligned_read<uint16_t>(ptr);
172 do_aligned_write<T, uint32_t>(ptr, value);
179 do_unaligned_write<T, uint32_t>(ptr, value);
186 do_aligned_write<T, uint16_t>(ptr, value);
193 do_unaligned_write<T, uint16_t>(ptr, value);
196 #endif // XAPIAN_INCLUDED_WORDACCESS_H
#define AssertRel(A, REL, B)
void unaligned_write4(unsigned char *ptr, T value)
uint16_t unaligned_read2(const unsigned char *ptr)
Cast a pointer we know is suitably aligned.
std::enable_if< std::is_const< typename std::remove_pointer< U >::type >::value, T >::type alignment_cast(U ptr)
Cast a pointer we know is suitably aligned.
void aligned_write4(unsigned char *ptr, T value)
UINT do_unaligned_read(const unsigned char *ptr)
void do_aligned_write(unsigned char *ptr, T value)
UINT do_aligned_read(const unsigned char *ptr)
uint32_t aligned_read4(const unsigned char *ptr)
void aligned_write2(unsigned char *ptr, T value)
uint16_t aligned_read2(const unsigned char *ptr)
void unaligned_write2(unsigned char *ptr, T value)
void do_unaligned_write(unsigned char *ptr, T value)
uint16_t do_bswap(uint16_t value)
Various assertion macros.
uint32_t unaligned_read4(const unsigned char *ptr)