Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares generic and optimized functions to swap the byte order of |
| 10 | // an integral type. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_SUPPORT_SWAPBYTEORDER_H |
| 15 | #define LLVM_SUPPORT_SWAPBYTEORDER_H |
| 16 | |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include <cstddef> |
| 20 | #if defined(_MSC_VER) && !defined(_DEBUG) |
| 21 | #include <stdlib.h> |
| 22 | #endif |
| 23 | |
| 24 | namespace llvm { |
| 25 | namespace sys { |
| 26 | |
| 27 | /// SwapByteOrder_16 - This function returns a byte-swapped representation of |
| 28 | /// the 16-bit argument. |
| 29 | inline uint16_t SwapByteOrder_16(uint16_t value) { |
| 30 | #if defined(_MSC_VER) && !defined(_DEBUG) |
| 31 | // The DLL version of the runtime lacks these functions (bug!?), but in a |
| 32 | // release build they're replaced with BSWAP instructions anyway. |
| 33 | return _byteswap_ushort(value); |
| 34 | #else |
| 35 | uint16_t Hi = value << 8; |
| 36 | uint16_t Lo = value >> 8; |
| 37 | return Hi | Lo; |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | /// SwapByteOrder_32 - This function returns a byte-swapped representation of |
| 42 | /// the 32-bit argument. |
| 43 | inline uint32_t SwapByteOrder_32(uint32_t value) { |
| 44 | #if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC)) |
| 45 | return __builtin_bswap32(value); |
| 46 | #elif defined(_MSC_VER) && !defined(_DEBUG) |
| 47 | return _byteswap_ulong(value); |
| 48 | #else |
| 49 | uint32_t Byte0 = value & 0x000000FF; |
| 50 | uint32_t Byte1 = value & 0x0000FF00; |
| 51 | uint32_t Byte2 = value & 0x00FF0000; |
| 52 | uint32_t Byte3 = value & 0xFF000000; |
| 53 | return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | /// SwapByteOrder_64 - This function returns a byte-swapped representation of |
| 58 | /// the 64-bit argument. |
| 59 | inline uint64_t SwapByteOrder_64(uint64_t value) { |
| 60 | #if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC)) |
| 61 | return __builtin_bswap64(value); |
| 62 | #elif defined(_MSC_VER) && !defined(_DEBUG) |
| 63 | return _byteswap_uint64(value); |
| 64 | #else |
| 65 | uint64_t Hi = SwapByteOrder_32(uint32_t(value)); |
| 66 | uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32)); |
| 67 | return (Hi << 32) | Lo; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | inline unsigned char getSwappedBytes(unsigned char C) { return C; } |
| 72 | inline signed char getSwappedBytes(signed char C) { return C; } |
| 73 | inline char getSwappedBytes(char C) { return C; } |
| 74 | |
| 75 | inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); } |
| 76 | inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); } |
| 77 | |
| 78 | inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); } |
| 79 | inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); } |
| 80 | |
| 81 | #if __LONG_MAX__ == __INT_MAX__ |
| 82 | inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_32(C); } |
| 83 | inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_32(C); } |
| 84 | #elif __LONG_MAX__ == __LONG_LONG_MAX__ |
| 85 | inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_64(C); } |
| 86 | inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_64(C); } |
| 87 | #else |
| 88 | #error "Unknown long size!" |
| 89 | #endif |
| 90 | |
| 91 | inline unsigned long long getSwappedBytes(unsigned long long C) { |
| 92 | return SwapByteOrder_64(C); |
| 93 | } |
| 94 | inline signed long long getSwappedBytes(signed long long C) { |
| 95 | return SwapByteOrder_64(C); |
| 96 | } |
| 97 | |
| 98 | inline float getSwappedBytes(float C) { |
| 99 | union { |
| 100 | uint32_t i; |
| 101 | float f; |
| 102 | } in, out; |
| 103 | in.f = C; |
| 104 | out.i = SwapByteOrder_32(in.i); |
| 105 | return out.f; |
| 106 | } |
| 107 | |
| 108 | inline double getSwappedBytes(double C) { |
| 109 | union { |
| 110 | uint64_t i; |
| 111 | double d; |
| 112 | } in, out; |
| 113 | in.d = C; |
| 114 | out.i = SwapByteOrder_64(in.i); |
| 115 | return out.d; |
| 116 | } |
| 117 | |
| 118 | template<typename T> |
| 119 | inline void swapByteOrder(T &Value) { |
| 120 | Value = getSwappedBytes(Value); |
| 121 | } |
| 122 | |
| 123 | } // end namespace sys |
| 124 | } // end namespace llvm |
| 125 | |
| 126 | #endif |