Path: blob/main/contrib/llvm-project/compiler-rt/lib/orc/endianness.h
39566 views
//===- endian.h - Endianness support ----------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file declares generic and optimized functions to swap the byte order of9// an integral type.10//11//===----------------------------------------------------------------------===//1213#ifndef ORC_RT_ENDIAN_H14#define ORC_RT_ENDIAN_H1516#include <cstddef>17#include <cstdint>18#include <type_traits>19#if defined(_MSC_VER) && !defined(_DEBUG)20#include <stdlib.h>21#endif2223#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \24defined(__Fuchsia__) || defined(__EMSCRIPTEN__)25#include <endian.h>26#elif defined(_AIX)27#include <sys/machine.h>28#elif defined(__sun)29/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */30#include <sys/types.h>31#define BIG_ENDIAN 432132#define LITTLE_ENDIAN 123433#if defined(_BIG_ENDIAN)34#define BYTE_ORDER BIG_ENDIAN35#else36#define BYTE_ORDER LITTLE_ENDIAN37#endif38#elif defined(__MVS__)39#define BIG_ENDIAN 432140#define LITTLE_ENDIAN 123441#define BYTE_ORDER BIG_ENDIAN42#else43#if !defined(BYTE_ORDER) && !defined(_WIN32)44#include <machine/endian.h>45#endif46#endif4748namespace __orc_rt {4950/// ByteSwap_16 - This function returns a byte-swapped representation of51/// the 16-bit argument.52inline uint16_t ByteSwap_16(uint16_t value) {53#if defined(_MSC_VER) && !defined(_DEBUG)54// The DLL version of the runtime lacks these functions (bug!?), but in a55// release build they're replaced with BSWAP instructions anyway.56return _byteswap_ushort(value);57#else58uint16_t Hi = value << 8;59uint16_t Lo = value >> 8;60return Hi | Lo;61#endif62}6364/// This function returns a byte-swapped representation of the 32-bit argument.65inline uint32_t ByteSwap_32(uint32_t value) {66#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))67return __builtin_bswap32(value);68#elif defined(_MSC_VER) && !defined(_DEBUG)69return _byteswap_ulong(value);70#else71uint32_t Byte0 = value & 0x000000FF;72uint32_t Byte1 = value & 0x0000FF00;73uint32_t Byte2 = value & 0x00FF0000;74uint32_t Byte3 = value & 0xFF000000;75return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);76#endif77}7879/// This function returns a byte-swapped representation of the 64-bit argument.80inline uint64_t ByteSwap_64(uint64_t value) {81#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))82return __builtin_bswap64(value);83#elif defined(_MSC_VER) && !defined(_DEBUG)84return _byteswap_uint64(value);85#else86uint64_t Hi = ByteSwap_32(uint32_t(value));87uint32_t Lo = ByteSwap_32(uint32_t(value >> 32));88return (Hi << 32) | Lo;89#endif90}9192#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN93constexpr bool IsBigEndianHost = true;94#else95constexpr bool IsBigEndianHost = false;96#endif9798static const bool IsLittleEndianHost = !IsBigEndianHost;99100inline unsigned char getSwappedBytes(unsigned char C) { return C; }101inline signed char getSwappedBytes(signed char C) { return C; }102inline char getSwappedBytes(char C) { return C; }103104inline unsigned short getSwappedBytes(unsigned short C) {105return ByteSwap_16(C);106}107inline signed short getSwappedBytes(signed short C) { return ByteSwap_16(C); }108109inline unsigned int getSwappedBytes(unsigned int C) { return ByteSwap_32(C); }110inline signed int getSwappedBytes(signed int C) { return ByteSwap_32(C); }111112inline unsigned long getSwappedBytes(unsigned long C) {113// Handle LLP64 and LP64 platforms.114return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C)115: ByteSwap_64((uint64_t)C);116}117inline signed long getSwappedBytes(signed long C) {118// Handle LLP64 and LP64 platforms.119return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C)120: ByteSwap_64((uint64_t)C);121}122123inline unsigned long long getSwappedBytes(unsigned long long C) {124return ByteSwap_64(C);125}126inline signed long long getSwappedBytes(signed long long C) {127return ByteSwap_64(C);128}129130template <typename T>131inline std::enable_if_t<std::is_enum<T>::value, T> getSwappedBytes(T C) {132return static_cast<T>(133getSwappedBytes(static_cast<std::underlying_type_t<T>>(C)));134}135136template <typename T> inline void swapByteOrder(T &Value) {137Value = getSwappedBytes(Value);138}139140} // end namespace __orc_rt141142#endif // ORC_RT_ENDIAN_H143144145