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