#pragma once #include "base/base.hpp" #include #include // #define ENDIAN_IS_BIG // @TODO(bykoianko) This method returns false since 05.12.2010. That means only little-endian // architectures are supported. When it's necessary to support a big-endian system: // * method IsBigEndianMacroBased() should be implemented based on IsLittleEndian() function // * method SwapIfBigEndianMacroBased() should be implemented based on IsLittleEndian() function // * all serialization and deserialization of rs_bit_vector and the other rank-select structures // should be implemented taking endianness into account inline bool IsBigEndianMacroBased() { #ifdef ENDIAN_IS_BIG return true; #else return false; #endif } template T ReverseByteOrder(T t) { static_assert(std::is_integral::value, "Only integral types are supported."); T res; char const * a = reinterpret_cast(&t); char * b = reinterpret_cast(&res); for (size_t i = 0; i < sizeof(T); ++i) b[i] = a[sizeof(T) - 1 - i]; return res; } template T SwapIfBigEndianMacroBased(T t) { #ifdef ENDIAN_IS_BIG return ReverseByteOrder(t); #else return t; #endif } inline bool IsLittleEndian() { uint16_t const word = 0x0001; uint8_t const * const b = reinterpret_cast(&word); return b[0] != 0x0; }