gwnavruntime/base/endianness.h Source File

endianness.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef Navigation_Endianness_H
10 #define Navigation_Endianness_H
11 
12 
14 
15 
16 namespace Kaim
17 {
18 
19 
22 class Endianness
23 {
24 public:
26  enum Type
27  {
29  BigEndian = 1,
30  };
31 
33  enum Target
34  {
37  };
38 
40  static Type GetSystemEndianness()
41  {
42 #if defined(KY_OS_XBOX360) || defined(KY_OS_PS3) || defined(KY_OS_WII) || defined(KY_OS_WIIU)
43  return BigEndian;
44 #else
45  return LittleEndian;
46 #endif
47  }
48 
51  {
53  }
54 
56  static Type GetInverseEndianness(Type endianness)
57  {
58  return (endianness == BigEndian) ? LittleEndian : BigEndian;
59  }
60 
62  static Type GetEndiannessFromMem32(const KyUInt32* mem)
63  {
64  return (*mem == 0) ? LittleEndian : BigEndian;
65  }
66 
68  static void SetEndiannessToMem32(Type endianness, KyUInt32* mem)
69  {
70  *mem = 0;
71  if (endianness == BigEndian)
72  *((char*)mem) = 1; // set first byte to 1 regardless of the endianness
73  }
74 
76  static void SwapEndiannessInMem32(KyUInt32* mem)
77  {
78  Type endianness = GetEndiannessFromMem32(mem);
79  Type inverseEndianness = GetInverseEndianness(endianness);
80  SetEndiannessToMem32(inverseEndianness, mem);
81  }
82 
84  static inline void Swap16(void* x)
85  {
86  (*(KyUInt16*)x) =
87  ( (*(KyUInt16*)x) >> 8 ) |
88  ( (*(KyUInt16*)x) << 8 );
89  }
90 
92  static inline void Swap32(void* x)
93  {
94  (*(KyUInt32*)x) =
95  ( ( (*(KyUInt32*)x) >> 24 ) ) |
96  ( ( (*(KyUInt32*)x) << 8 ) & 0x00FF0000 ) |
97  ( ( (*(KyUInt32*)x) >> 8 ) & 0x0000FF00 ) |
98  ( ( (*(KyUInt32*)x) << 24 ) );
99  }
100 
102  static inline void Swap64(void* x)
103  {
104  (*(KyUInt64*)x) =
105  ( ( (*(KyUInt64*)x) << 56 ) ) |
106  ( ( (*(KyUInt64*)x) << 40 ) & 0x00FF000000000000ULL ) |
107  ( ( (*(KyUInt64*)x) << 24 ) & 0x0000FF0000000000ULL ) |
108  ( ( (*(KyUInt64*)x) << 8 ) & 0x000000FF00000000ULL ) |
109  ( ( (*(KyUInt64*)x) >> 8 ) & 0x00000000FF000000ULL ) |
110  ( ( (*(KyUInt64*)x) >> 24 ) & 0x0000000000FF0000ULL ) |
111  ( ( (*(KyUInt64*)x) >> 40 ) & 0x000000000000FF00ULL ) |
112  ( ( (*(KyUInt64*)x) >> 56 ) );
113  }
114 };
115 
118 inline void SwapEndianness(Endianness::Target, KyInt32& x ) { Endianness::Swap32(&x); }
119 
122 inline void SwapEndianness(Endianness::Target, KyUInt32& x ) { Endianness::Swap32(&x); }
123 
126 inline void SwapEndianness(Endianness::Target, KyInt16& x ) { Endianness::Swap16(&x); }
127 
130 inline void SwapEndianness(Endianness::Target, KyUInt16& x ) { Endianness::Swap16(&x); }
134 inline void SwapEndianness(Endianness::Target, KyInt8& ) {}
135 
138 inline void SwapEndianness(Endianness::Target, KyUInt8& ) {}
139 
142 inline void SwapEndianness(Endianness::Target, char& ) {}
143 
146 inline void SwapEndianness(Endianness::Target, KyFloat32& x) { Endianness::Swap32(&x); }
147 
150 inline void SwapEndianness(Endianness::Target, KyInt64& x ) { Endianness::Swap64(&x); }
154 inline void SwapEndianness(Endianness::Target, KyUInt64& x ) { Endianness::Swap64(&x); }
155 
157 template <class OSTREAM>
158 inline OSTREAM& operator<<(OSTREAM& os, Endianness::Type& endianess)
159 {
160  switch (endianess)
161  {
162  case Endianness::LittleEndian: os << "LittleEndian"; break;
163  case Endianness::BigEndian: os << "BigEndian"; break;
164  }
165  return os;
166 }
167 
168 
169 }
170 
171 
172 #endif
The opposite endianness type from the current platform.
Definition: endianness.h:38
static void Swap64(void *x)
Swaps the endianness of the data in a 64-bit buffer.
Definition: endianness.h:104
The same endianness type as the current platform.
Definition: endianness.h:37
static void Swap32(void *x)
Swaps the endianness of the data in a 32-bit buffer.
Definition: endianness.h:94
static Type GetSystemEndianness()
Retrieves the endianness of the current platform.
Definition: endianness.h:42
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
static Type GetEndiannessFromMem32(const KyUInt32 *mem)
Retrieves the endianness from a 4-byte memory location.
Definition: endianness.h:64
Big-endian format (used, for example, for PlayStation 3, Xbox 360).
Definition: endianness.h:31
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:35
unsigned __int64 KyUInt64
Type used internally to represent an unsigned 64-bit integer.
Definition: types.h:38
Little-endian format (used, for example, for Windows, Linux).
Definition: endianness.h:30
unsigned char KyUInt8
Type used internally to represent an unsigned 8-bit integer.
Definition: types.h:41
static Type GetInverseEndianness(Type endianness)
Retrieves the endianness opposite to the specified type.
Definition: endianness.h:58
Definition: gamekitcrowddispersion.h:20
static void SetEndiannessToMem32(Type endianness, KyUInt32 *mem)
Sets a 4-byte memory location to the specified endianness.
Definition: endianness.h:70
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
static void SwapEndiannessInMem32(KyUInt32 *mem)
Swaps the endianness in a 4-byte memory location.
Definition: endianness.h:78
signed char KyInt8
Type used internally to represent an 8-bit integer.
Definition: types.h:42
Type
Enumerates possible endianness types.
Definition: endianness.h:28
static Type GetInverseSystemEndianness()
Retrieves the endianness opposite to that of the current platform.
Definition: endianness.h:52
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
static void Swap16(void *x)
Swaps the endianness of the data in a 16-bit buffer.
Definition: endianness.h:86
__int64 KyInt64
Type used internally to represent a 64-bit integer.
Definition: types.h:37
short KyInt16
Type used internally to represent a 16-bit integer.
Definition: types.h:39
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43