18 #ifndef FOEDUS_ASSORTED_ENDIANNESS_HPP_
19 #define FOEDUS_ASSORTED_ENDIANNESS_HPP_
53 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
55 #elif __BYTE_ORDER == __BIG_ENDIAN
56 const bool kIsLittleEndian =
true;
57 #else // __BYTE_ORDER == __BIG_ENDIAN
58 #error "__BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN."
59 #endif // __BYTE_ORDER
64 template <
typename T> T
betoh(T be_value);
65 template <>
inline uint64_t
betoh<uint64_t>(uint64_t be_value) {
return be64toh(be_value); }
66 template <>
inline uint32_t
betoh<uint32_t>(uint32_t be_value) {
return be32toh(be_value); }
67 template <>
inline uint16_t
betoh<uint16_t>(uint16_t be_value) {
return be16toh(be_value); }
68 template <>
inline uint8_t
betoh<uint8_t>(uint8_t be_value) {
return be_value; }
74 return be64toh(static_cast<uint64_t>(be_value)) - (1ULL << 63);
77 return be32toh(static_cast<uint32_t>(be_value)) - (1U << 31);
80 return be16toh(static_cast<uint16_t>(be_value)) - (1U << 15);
83 return be_value - (1U << 7);
86 template <
typename T> T
htobe(T host_value);
87 template <>
inline uint64_t
htobe<uint64_t>(uint64_t host_value) {
return htobe64(host_value); }
88 template <>
inline uint32_t
htobe<uint32_t>(uint32_t host_value) {
return htobe32(host_value); }
89 template <>
inline uint16_t
htobe<uint16_t>(uint16_t host_value) {
return htobe16(host_value); }
90 template <>
inline uint8_t
htobe<uint8_t>(uint8_t host_value) {
return host_value; }
93 return htobe64(static_cast<uint64_t>(host_value) - (1ULL << 63));
96 return htobe32(static_cast<uint32_t>(host_value) - (1U << 31));
99 return htobe16(static_cast<uint16_t>(host_value) - (1U << 15));
102 return host_value - (1U << 7);
115 template <
typename T>
118 const T* be_address =
reinterpret_cast<const T*
>(
ASSUME_ALIGNED(be_bytes,
sizeof(T)));
119 T be_value = *be_address;
120 return betoh(be_value);
130 template <
typename T>
132 T* be_address =
reinterpret_cast<T*
>(
ASSUME_ALIGNED(be_bytes,
sizeof(T)));
133 *be_address = htobe<T>(host_value);
139 #endif // FOEDUS_ASSORTED_ENDIANNESS_HPP_
uint16_t betoh< uint16_t >(uint16_t be_value)
int64_t betoh< int64_t >(int64_t be_value)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
int64_t htobe< int64_t >(int64_t host_value)
#define ASSUME_ALIGNED(x, y)
Wraps GCC's __builtin_assume_aligned.
int16_t betoh< int16_t >(int16_t be_value)
int8_t betoh< int8_t >(int8_t be_value)
int32_t htobe< int32_t >(int32_t host_value)
uint64_t htobe< uint64_t >(uint64_t host_value)
void write_bigendian(T host_value, void *be_bytes)
Convert a native integer to big-endian bytes and write them to the given address. ...
uint8_t htobe< uint8_t >(uint8_t host_value)
uint16_t htobe< uint16_t >(uint16_t host_value)
uint8_t betoh< uint8_t >(uint8_t be_value)
uint32_t betoh< uint32_t >(uint32_t be_value)
int8_t htobe< int8_t >(int8_t host_value)
uint64_t betoh< uint64_t >(uint64_t be_value)
T read_bigendian(const void *be_bytes)
Convert a big-endian byte array to a native integer.
uint32_t htobe< uint32_t >(uint32_t host_value)
int32_t betoh< int32_t >(int32_t be_value)
int16_t htobe< int16_t >(int16_t host_value)
const bool kIsLittleEndian
A handy const boolean to tell if it's little endina.