gwnavruntime/base/endianness.h Source File

endianness.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
8 
10 
11 namespace Kaim
12 {
13 
17 {
18 public:
20  enum Type
21  {
23  BigEndian = 1,
24  };
25 
27  enum Target
28  {
31  };
32 
34  static Type GetSystemEndianness() { return LittleEndian; }
35 
38 
40  static Type GetInverseEndianness(Type endianness) { return (endianness == BigEndian) ? LittleEndian : BigEndian; }
41 
43  static Type GetEndiannessFromMem32(const KyUInt32* mem) { return (*mem == 0) ? LittleEndian : BigEndian; }
44 
46  static void SetEndiannessToMem32(Type endianness, KyUInt32* mem)
47  {
48  *mem = 0;
49  if (endianness == BigEndian)
50  *((char*)mem) = 1; // set first byte to 1 regardless of the endianness
51  }
52 
54  static void SwapEndiannessInMem32(KyUInt32* mem)
55  {
56  Type endianness = GetEndiannessFromMem32(mem);
57  Type inverseEndianness = GetInverseEndianness(endianness);
58  SetEndiannessToMem32(inverseEndianness, mem);
59  }
60 
62  static inline void Swap16(void* x)
63  {
64  (*(KyUInt16*)x) =
65  ( (*(KyUInt16*)x) >> 8 ) |
66  ( (*(KyUInt16*)x) << 8 );
67  }
68 
70  static inline void Swap32(void* x)
71  {
72  (*(KyUInt32*)x) =
73  ( ( (*(KyUInt32*)x) >> 24 ) ) |
74  ( ( (*(KyUInt32*)x) << 8 ) & 0x00FF0000 ) |
75  ( ( (*(KyUInt32*)x) >> 8 ) & 0x0000FF00 ) |
76  ( ( (*(KyUInt32*)x) << 24 ) );
77  }
78 
80  static inline void Swap64(void* x)
81  {
82  (*(KyUInt64*)x) =
83  ( ( (*(KyUInt64*)x) << 56 ) ) |
84  ( ( (*(KyUInt64*)x) << 40 ) & 0x00FF000000000000ULL ) |
85  ( ( (*(KyUInt64*)x) << 24 ) & 0x0000FF0000000000ULL ) |
86  ( ( (*(KyUInt64*)x) << 8 ) & 0x000000FF00000000ULL ) |
87  ( ( (*(KyUInt64*)x) >> 8 ) & 0x00000000FF000000ULL ) |
88  ( ( (*(KyUInt64*)x) >> 24 ) & 0x0000000000FF0000ULL ) |
89  ( ( (*(KyUInt64*)x) >> 40 ) & 0x000000000000FF00ULL ) |
90  ( ( (*(KyUInt64*)x) >> 56 ) );
91  }
92 };
93 
94 inline void SwapEndianness(Endianness::Target, KyInt32& x ) { Endianness::Swap32(&x); }
95 inline void SwapEndianness(Endianness::Target, KyUInt32& x ) { Endianness::Swap32(&x); }
96 inline void SwapEndianness(Endianness::Target, KyInt16& x ) { Endianness::Swap16(&x); }
97 inline void SwapEndianness(Endianness::Target, KyUInt16& x ) { Endianness::Swap16(&x); }
98 inline void SwapEndianness(Endianness::Target, KyInt8& ) {}
99 inline void SwapEndianness(Endianness::Target, KyUInt8& ) {}
100 inline void SwapEndianness(Endianness::Target, char& ) {}
101 inline void SwapEndianness(Endianness::Target, KyFloat32& x) { Endianness::Swap32(&x); }
102 inline void SwapEndianness(Endianness::Target, KyInt64& x ) { Endianness::Swap64(&x); }
103 inline void SwapEndianness(Endianness::Target, KyUInt64& x ) { Endianness::Swap64(&x); }
104 
105 
106 template <class OSTREAM>
107 inline OSTREAM& operator<<(OSTREAM& os, Endianness::Type& endianess)
108 {
109  switch (endianess)
110  {
111  case Endianness::LittleEndian: os << "LittleEndian"; break;
112  case Endianness::BigEndian: os << "BigEndian"; break;
113  }
114  return os;
115 }
116 
117 
118 }
119 
120 
The opposite endianness type from the current platform.
Definition: endianness.h:30
static void Swap64(void *x)
Swaps the endianness of the data in a 64-bit buffer.
Definition: endianness.h:80
std::uint64_t KyUInt64
uint64_t
Definition: types.h:30
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The same endianness type as the current platform.
Definition: endianness.h:29
static void Swap32(void *x)
Swaps the endianness of the data in a 32-bit buffer.
Definition: endianness.h:70
static Type GetSystemEndianness()
Retrieves the endianness of the current platform.
Definition: endianness.h:34
static Type GetEndiannessFromMem32(const KyUInt32 *mem)
Retrieves the endianness from a 4-byte memory location.
Definition: endianness.h:43
Collects endianness types and utilities used in the blob serialization framework. ...
Definition: endianness.h:16
std::uint16_t KyUInt16
uint16_t
Definition: types.h:28
Big-endian format (used, for example, for PlayStation 3, Xbox 360).
Definition: endianness.h:23
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
Little-endian format (used, for example, for Windows, Linux).
Definition: endianness.h:22
std::int64_t KyInt64
int64_t
Definition: types.h:25
static Type GetInverseEndianness(Type endianness)
Retrieves the endianness opposite to the specified type.
Definition: endianness.h:40
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
static void SetEndiannessToMem32(Type endianness, KyUInt32 *mem)
Sets a 4-byte memory location to the specified endianness.
Definition: endianness.h:46
std::int32_t KyInt32
int32_t
Definition: types.h:24
static void SwapEndiannessInMem32(KyUInt32 *mem)
Swaps the endianness in a 4-byte memory location.
Definition: endianness.h:54
std::int8_t KyInt8
int8_t
Definition: types.h:22
Type
Enumerates possible endianness types.
Definition: endianness.h:20
static Type GetInverseSystemEndianness()
Retrieves the endianness opposite to that of the current platform.
Definition: endianness.h:37
static void Swap16(void *x)
Swaps the endianness of the data in a 16-bit buffer.
Definition: endianness.h:62
std::uint8_t KyUInt8
uint8_t
Definition: types.h:27
std::int16_t KyInt16
int16_t
Definition: types.h:23
float KyFloat32
float
Definition: types.h:32