Mercurial > hg > orthanc
annotate Core/Endianness.h @ 2140:aa4b8895cd23
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 16:12:47 +0100 |
parents | 325772dadcd6 |
children | a3a65de1840f |
rev | line source |
---|---|
1938 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #pragma once | |
34 | |
35 | |
36 /******************************************************************** | |
37 ** LINUX ARCHITECTURES | |
38 ********************************************************************/ | |
39 | |
1976
325772dadcd6
Macro "__linux" (now obsolete) replaced by macro "__linux__"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1971
diff
changeset
|
40 #if defined(__linux__) |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
41 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1 |
1938 | 42 # include <endian.h> |
43 #endif | |
44 | |
45 | |
46 /******************************************************************** | |
47 ** WINDOWS ARCHITECTURES | |
48 ** | |
1950
533ff46e944b
return a single raw frame from DICOM videos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1939
diff
changeset
|
49 ** On Windows x86, "host" will always be little-endian ("le"). |
1938 | 50 ********************************************************************/ |
51 | |
52 #if defined(_WIN32) | |
53 # if defined(_MSC_VER) | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
54 // Visual Studio - http://msdn.microsoft.com/en-us/library/a3140177.aspx |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
55 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1 |
1938 | 56 # define be16toh(x) _byteswap_ushort(x) |
57 # define be32toh(x) _byteswap_ulong(x) | |
58 # define be64toh(x) _byteswap_uint64(x) | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
59 # elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
60 // MinGW >= 4.3 - Use builtin intrinsic for byte swapping |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
61 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1 |
1938 | 62 # define be16toh(x) __builtin_bswap16(x) |
63 # define be32toh(x) __builtin_bswap32(x) | |
64 # define be64toh(x) __builtin_bswap64(x) | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
65 # else |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
66 // MinGW <= 4.2, we must manually implement the byte swapping |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
67 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 0 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
68 # define be16toh(x) __orthanc_bswap16(x) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
69 # define be32toh(x) __orthanc_bswap32(x) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
70 # define be64toh(x) __orthanc_bswap64(x) |
1938 | 71 # endif |
72 | |
73 # define htobe16(x) be16toh(x) | |
74 # define htobe32(x) be32toh(x) | |
75 # define htobe64(x) be64toh(x) | |
76 | |
77 # define htole16(x) x | |
78 # define htole32(x) x | |
79 # define htole64(x) x | |
80 | |
81 # define le16toh(x) x | |
82 # define le32toh(x) x | |
83 # define le64toh(x) x | |
84 #endif | |
85 | |
86 | |
87 /******************************************************************** | |
88 ** FREEBSD ARCHITECTURES | |
89 ********************************************************************/ | |
90 | |
91 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
92 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1 |
1938 | 93 # include <arpa/inet.h> |
94 #endif | |
95 | |
96 | |
97 /******************************************************************** | |
98 ** APPLE ARCHITECTURES (including OS X) | |
99 ********************************************************************/ | |
100 | |
101 #if defined(__APPLE__) | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
102 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1 |
1938 | 103 # include <libkern/OSByteOrder.h> |
104 # define be16toh(x) OSSwapBigToHostInt16(x) | |
105 # define be32toh(x) OSSwapBigToHostInt32(x) | |
106 # define be64toh(x) OSSwapBigToHostInt64(x) | |
107 | |
108 # define htobe16(x) OSSwapHostToBigInt16(x) | |
109 # define htobe32(x) OSSwapHostToBigInt32(x) | |
110 # define htobe64(x) OSSwapHostToBigInt64(x) | |
111 | |
112 # define htole16(x) OSSwapHostToLittleInt16(x) | |
113 # define htole32(x) OSSwapHostToLittleInt32(x) | |
114 # define htole64(x) OSSwapHostToLittleInt64(x) | |
115 | |
116 # define le16toh(x) OSSwapLittleToHostInt16(x) | |
117 # define le32toh(x) OSSwapLittleToHostInt32(x) | |
118 # define le64toh(x) OSSwapLittleToHostInt64(x) | |
119 #endif | |
1971
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
120 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
121 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
122 /******************************************************************** |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
123 ** PORTABLE (BUT SLOW) IMPLEMENTATION OF BYTE-SWAPPING |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
124 ********************************************************************/ |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
125 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
126 #if ORTHANC_HAS_BUILTIN_BYTE_SWAP != 1 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
127 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
128 #include <stdint.h> |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
129 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
130 static inline uint16_t __orthanc_bswap16(uint16_t a) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
131 { |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
132 return (a << 8) | (a >> 8); |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
133 } |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
134 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
135 static inline uint32_t __orthanc_bswap32(uint32_t a) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
136 { |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
137 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a); |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
138 return (static_cast<uint32_t>(p[0]) << 24 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
139 static_cast<uint32_t>(p[1]) << 16 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
140 static_cast<uint32_t>(p[2]) << 8 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
141 static_cast<uint32_t>(p[3])); |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
142 } |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
143 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
144 static inline uint64_t __orthanc_bswap64(uint64_t a) |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
145 { |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
146 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a); |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
147 return (static_cast<uint64_t>(p[0]) << 56 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
148 static_cast<uint64_t>(p[1]) << 48 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
149 static_cast<uint64_t>(p[2]) << 40 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
150 static_cast<uint64_t>(p[3]) << 32 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
151 static_cast<uint64_t>(p[4]) << 24 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
152 static_cast<uint64_t>(p[5]) << 16 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
153 static_cast<uint64_t>(p[6]) << 8 | |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
154 static_cast<uint64_t>(p[7])); |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
155 } |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
156 |
869a87c08673
fix for mingw <= 4.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1950
diff
changeset
|
157 #endif |