endian.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include <cstdint>
32 
33 #if defined(_MSC_VER)
34 #include <stdlib.h>
35 #elif !defined(__APPLE__)
36 #include <byteswap.h>
37 #define HAS_BYTESWAP
38 #endif
39 
40 namespace uicore
41 {
42 #if defined(_MSC_VER)
43 
44  class Endian
45  {
46  public:
47  static uint8_t swap(uint8_t val)
48  {
49  return val;
50  }
51 
52  static int8_t swap(int8_t val)
53  {
54  return val;
55  }
56 
57  static uint16_t swap(uint16_t val)
58  {
59  return _byteswap_ushort(val);
60  }
61 
62  static int16_t swap(int16_t val)
63  {
64  return (int16_t)swap((uint16_t)val);
65  }
66 
67  static uint32_t swap(uint32_t val)
68  {
69  return _byteswap_ulong(val);
70  }
71 
72  static int32_t swap(int32_t val)
73  {
74  return (int32_t)swap((uint32_t)val);
75  }
76 
77  static uint64_t swap(uint64_t val)
78  {
79  return _byteswap_uint64(val);
80  }
81 
82  static int64_t swap(int64_t val)
83  {
84  return (int64_t)swap((uint64_t)val);
85  }
86 
87  static float swap(float val)
88  {
89  return val;
90  }
91 
92  static double swap(double val)
93  {
94  return val;
95  }
96  };
97 
98 #elif defined(HAS_BYTESWAP)
99 
100  class Endian
101  {
102  public:
103  static uint8_t swap(uint8_t val)
104  {
105  return val;
106  }
107 
108  static int8_t swap(int8_t val)
109  {
110  return val;
111  }
112 
113  static uint16_t swap(uint16_t val)
114  {
115 #ifdef bswap_16
116  return bswap_16(val);
117 #else
118  return __bswap_16(val);
119 #endif
120  }
121 
122  static int16_t swap(int16_t val)
123  {
124  return (int16_t)swap((uint16_t)val);
125  }
126 
127  static uint32_t swap(uint32_t val)
128  {
129 #ifdef bswap_32
130  return bswap_32(val);
131 #else
132  return __bswap_32(val);
133 #endif
134  }
135 
136  static int32_t swap(int32_t val)
137  {
138  return (int32_t)swap((uint32_t)val);
139  }
140 
141  static uint64_t swap(uint64_t val)
142  {
143 #ifdef bswap_64
144  return bswap_64(val);
145 #else
146  return __bswap_64(val);
147 #endif
148  }
149 
150  static int64_t swap(int64_t val)
151  {
152  return (int64_t)swap((uint64_t)val);
153  }
154 
155  static float swap(float val)
156  {
157  return val;
158  }
159 
160  static double swap(double val)
161  {
162  return val;
163  }
164  };
165 
166 #else
167 
168  class Endian
169  {
170  public:
171  static uint8_t swap(uint8_t val)
172  {
173  return val;
174  }
175 
176  static int8_t swap(int8_t val)
177  {
178  return val;
179  }
180 
181  static uint16_t swap(uint16_t val)
182  {
183  return (val << 8) | (val >> 8);
184  }
185 
186  static int16_t swap(int16_t val)
187  {
188  return (val << 8) | ((val >> 8) & 0xFF);
189  }
190 
191  static uint32_t swap(uint32_t val)
192  {
193  val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
194  return (val << 16) | (val >> 16);
195  }
196 
197  static int32_t swap(int32_t val)
198  {
199  val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
200  return (val << 16) | ((val >> 16) & 0xFFFF);
201  }
202 
203  static uint64_t swap(uint64_t val)
204  {
205  val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
206  val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
207  return (val << 32) | (val >> 32);
208  }
209 
210  static int64_t swap(int64_t val)
211  {
212  val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
213  val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
214  return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
215  }
216 
217  static float swap(float val)
218  {
219  return val;
220  }
221 
222  static double swap(double val)
223  {
224  return val;
225  }
226  };
227 
228 #endif
229 }
230 
static uint32_t swap(uint32_t val)
Definition: endian.h:127
static uint8_t swap(uint8_t val)
Definition: endian.h:103
static uint64_t swap(uint64_t val)
Definition: endian.h:141
Definition: endian.h:100
static double swap(double val)
Definition: endian.h:160
static float swap(float val)
Definition: endian.h:155
static int8_t swap(int8_t val)
Definition: endian.h:108
static int16_t swap(int16_t val)
Definition: endian.h:122
Definition: Application/application.h:35
static int64_t swap(int64_t val)
Definition: endian.h:150
static uint16_t swap(uint16_t val)
Definition: endian.h:113
static int32_t swap(int32_t val)
Definition: endian.h:136