RetroArch
retro_endianness.h
Go to the documentation of this file.
1 /* Copyright (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (retro_endianness.h).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __LIBRETRO_SDK_ENDIANNESS_H
24 #define __LIBRETRO_SDK_ENDIANNESS_H
25 
26 #include <retro_inline.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 
30 #if defined(_MSC_VER) && _MSC_VER > 1200
31 #define SWAP16 _byteswap_ushort
32 #define SWAP32 _byteswap_ulong
33 #else
34 #define SWAP16(x) ((uint16_t)( \
35  (((uint16_t)(x) & 0x00ff) << 8) | \
36  (((uint16_t)(x) & 0xff00) >> 8) \
37  ))
38 #define SWAP32(x) ((uint32_t)( \
39  (((uint32_t)(x) & 0x000000ff) << 24) | \
40  (((uint32_t)(x) & 0x0000ff00) << 8) | \
41  (((uint32_t)(x) & 0x00ff0000) >> 8) | \
42  (((uint32_t)(x) & 0xff000000) >> 24) \
43  ))
44 #endif
45 
46 #if defined(_MSC_VER) && _MSC_VER <= 1200
47 #define SWAP64(val) \
48  ((((uint64_t)(val) & 0x00000000000000ff) << 56) \
49  | (((uint64_t)(val) & 0x000000000000ff00) << 40) \
50  | (((uint64_t)(val) & 0x0000000000ff0000) << 24) \
51  | (((uint64_t)(val) & 0x00000000ff000000) << 8) \
52  | (((uint64_t)(val) & 0x000000ff00000000) >> 8) \
53  | (((uint64_t)(val) & 0x0000ff0000000000) >> 24) \
54  | (((uint64_t)(val) & 0x00ff000000000000) >> 40) \
55  | (((uint64_t)(val) & 0xff00000000000000) >> 56))
56 #else
57 #define SWAP64(val) \
58  ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
59  | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
60  | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
61  | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
62  | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
63  | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
64  | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
65  | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
66 #endif
67 
76 #if defined(MSB_FIRST)
77 #define is_little_endian() (0)
78 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
79 #define is_little_endian() (1)
80 #else
82 {
83  union
84  {
85  uint16_t x;
86  uint8_t y[2];
87  } u;
88 
89  u.x = 1;
90  return u.y[0];
91 }
92 #endif
93 
104 #if defined(MSB_FIRST)
105 #define swap_if_big64(val) (SWAP64(val))
106 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
107 #define swap_if_big64(val) (val)
108 #else
110 {
111  if (is_little_endian())
112  return val;
113  return SWAP64(val);
114 }
115 #endif
116 
127 #if defined(MSB_FIRST)
128 #define swap_if_big32(val) (SWAP32(val))
129 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
130 #define swap_if_big32(val) (val)
131 #else
133 {
134  if (is_little_endian())
135  return val;
136  return SWAP32(val);
137 }
138 #endif
139 
150 #if defined(MSB_FIRST)
151 #define swap_if_little64(val) (val)
152 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
153 #define swap_if_little64(val) (SWAP64(val))
154 #else
156 {
157  if (is_little_endian())
158  return SWAP64(val);
159  return val;
160 }
161 #endif
162 
173 #if defined(MSB_FIRST)
174 #define swap_if_little32(val) (val)
175 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
176 #define swap_if_little32(val) (SWAP32(val))
177 #else
179 {
180  if (is_little_endian())
181  return SWAP32(val);
182  return val;
183 }
184 #endif
185 
196 #if defined(MSB_FIRST)
197 #define swap_if_big16(val) (SWAP16(val))
198 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
199 #define swap_if_big16(val) (val)
200 #else
202 {
203  if (is_little_endian())
204  return val;
205  return SWAP16(val);
206 }
207 #endif
208 
219 #if defined(MSB_FIRST)
220 #define swap_if_little16(val) (val)
221 #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
222 #define swap_if_little16(val) (SWAP16(val))
223 #else
225 {
226  if (is_little_endian())
227  return SWAP16(val);
228  return val;
229 }
230 #endif
231 
241 {
243 }
244 
254 {
255  return swap_if_little32(*addr);
256 }
257 
258 #endif
GLuint GLfloat * val
Definition: glext.h:7847
#define INLINE
Definition: retro_inline.h:35
#define SWAP32(x)
Definition: retro_endianness.h:38
#define SWAP16(x)
Definition: retro_endianness.h:34
static INLINE uint32_t swap_if_big32(uint32_t val)
Definition: retro_endianness.h:132
Definition: ibxm.h:9
static INLINE uint32_t swap_if_little32(uint32_t val)
Definition: retro_endianness.h:178
static INLINE uint32_t load32be(const uint32_t *addr)
Definition: retro_endianness.h:253
static INLINE uint64_t swap_if_big64(uint64_t val)
Definition: retro_endianness.h:109
static INLINE uint16_t swap_if_little16(uint16_t val)
Definition: retro_endianness.h:224
GLenum const GLvoid * addr
Definition: glext.h:10528
static INLINE uint64_t swap_if_little64(uint64_t val)
Definition: retro_endianness.h:155
static INLINE uint8_t is_little_endian(void)
Definition: retro_endianness.h:81
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:6295
GLint GLint GLint GLint GLint x
Definition: glext.h:6295
static INLINE void store32be(uint32_t *addr, uint32_t data)
Definition: retro_endianness.h:240
static INLINE uint16_t swap_if_big16(uint16_t val)
Definition: retro_endianness.h:201
#define SWAP64(val)
Definition: retro_endianness.h:57
unsigned short uint16_t
Definition: stdint.h:125
unsigned __int64 uint64_t
Definition: stdint.h:136
unsigned char uint8_t
Definition: stdint.h:124
unsigned int uint32_t
Definition: stdint.h:126