comparison Core/Endianness.h @ 1971:869a87c08673

fix for mingw <= 4.2
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 11 Apr 2016 13:50:44 +0200
parents 533ff46e944b
children 325772dadcd6
comparison
equal deleted inserted replaced
1970:a1c1c606ce98 1971:869a87c08673
36 /******************************************************************** 36 /********************************************************************
37 ** LINUX ARCHITECTURES 37 ** LINUX ARCHITECTURES
38 ********************************************************************/ 38 ********************************************************************/
39 39
40 #if defined(__linux) 40 #if defined(__linux)
41 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
41 # include <endian.h> 42 # include <endian.h>
42 #endif 43 #endif
43 44
44 45
45 /******************************************************************** 46 /********************************************************************
48 ** On Windows x86, "host" will always be little-endian ("le"). 49 ** On Windows x86, "host" will always be little-endian ("le").
49 ********************************************************************/ 50 ********************************************************************/
50 51
51 #if defined(_WIN32) 52 #if defined(_WIN32)
52 # if defined(_MSC_VER) 53 # if defined(_MSC_VER)
53 // http://msdn.microsoft.com/en-us/library/a3140177.aspx 54 // Visual Studio - http://msdn.microsoft.com/en-us/library/a3140177.aspx
55 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
54 # define be16toh(x) _byteswap_ushort(x) 56 # define be16toh(x) _byteswap_ushort(x)
55 # define be32toh(x) _byteswap_ulong(x) 57 # define be32toh(x) _byteswap_ulong(x)
56 # define be64toh(x) _byteswap_uint64(x) 58 # define be64toh(x) _byteswap_uint64(x)
57 # else // MinGW 59 # elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
60 // MinGW >= 4.3 - Use builtin intrinsic for byte swapping
61 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
58 # define be16toh(x) __builtin_bswap16(x) 62 # define be16toh(x) __builtin_bswap16(x)
59 # define be32toh(x) __builtin_bswap32(x) 63 # define be32toh(x) __builtin_bswap32(x)
60 # define be64toh(x) __builtin_bswap64(x) 64 # define be64toh(x) __builtin_bswap64(x)
65 # else
66 // MinGW <= 4.2, we must manually implement the byte swapping
67 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 0
68 # define be16toh(x) __orthanc_bswap16(x)
69 # define be32toh(x) __orthanc_bswap32(x)
70 # define be64toh(x) __orthanc_bswap64(x)
61 # endif 71 # endif
62 72
63 # define htobe16(x) be16toh(x) 73 # define htobe16(x) be16toh(x)
64 # define htobe32(x) be32toh(x) 74 # define htobe32(x) be32toh(x)
65 # define htobe64(x) be64toh(x) 75 # define htobe64(x) be64toh(x)
77 /******************************************************************** 87 /********************************************************************
78 ** FREEBSD ARCHITECTURES 88 ** FREEBSD ARCHITECTURES
79 ********************************************************************/ 89 ********************************************************************/
80 90
81 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 91 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
92 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
82 # include <arpa/inet.h> 93 # include <arpa/inet.h>
83 #endif 94 #endif
84 95
85 96
86 /******************************************************************** 97 /********************************************************************
87 ** APPLE ARCHITECTURES (including OS X) 98 ** APPLE ARCHITECTURES (including OS X)
88 ********************************************************************/ 99 ********************************************************************/
89 100
90 #if defined(__APPLE__) 101 #if defined(__APPLE__)
102 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
91 # include <libkern/OSByteOrder.h> 103 # include <libkern/OSByteOrder.h>
92 # define be16toh(x) OSSwapBigToHostInt16(x) 104 # define be16toh(x) OSSwapBigToHostInt16(x)
93 # define be32toh(x) OSSwapBigToHostInt32(x) 105 # define be32toh(x) OSSwapBigToHostInt32(x)
94 # define be64toh(x) OSSwapBigToHostInt64(x) 106 # define be64toh(x) OSSwapBigToHostInt64(x)
95 107
103 115
104 # define le16toh(x) OSSwapLittleToHostInt16(x) 116 # define le16toh(x) OSSwapLittleToHostInt16(x)
105 # define le32toh(x) OSSwapLittleToHostInt32(x) 117 # define le32toh(x) OSSwapLittleToHostInt32(x)
106 # define le64toh(x) OSSwapLittleToHostInt64(x) 118 # define le64toh(x) OSSwapLittleToHostInt64(x)
107 #endif 119 #endif
120
121
122 /********************************************************************
123 ** PORTABLE (BUT SLOW) IMPLEMENTATION OF BYTE-SWAPPING
124 ********************************************************************/
125
126 #if ORTHANC_HAS_BUILTIN_BYTE_SWAP != 1
127
128 #include <stdint.h>
129
130 static inline uint16_t __orthanc_bswap16(uint16_t a)
131 {
132 return (a << 8) | (a >> 8);
133 }
134
135 static inline uint32_t __orthanc_bswap32(uint32_t a)
136 {
137 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a);
138 return (static_cast<uint32_t>(p[0]) << 24 |
139 static_cast<uint32_t>(p[1]) << 16 |
140 static_cast<uint32_t>(p[2]) << 8 |
141 static_cast<uint32_t>(p[3]));
142 }
143
144 static inline uint64_t __orthanc_bswap64(uint64_t a)
145 {
146 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a);
147 return (static_cast<uint64_t>(p[0]) << 56 |
148 static_cast<uint64_t>(p[1]) << 48 |
149 static_cast<uint64_t>(p[2]) << 40 |
150 static_cast<uint64_t>(p[3]) << 32 |
151 static_cast<uint64_t>(p[4]) << 24 |
152 static_cast<uint64_t>(p[5]) << 16 |
153 static_cast<uint64_t>(p[6]) << 8 |
154 static_cast<uint64_t>(p[7]));
155 }
156
157 #endif