Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cube_endian.hpp
Go to the documentation of this file.
1 #pragma once
6 
7 #include <SDL2/SDL.h>
9 
10 const int islittleendian = 1;
11 
12 #ifdef SDL_BYTEORDER
13 #define endianswap16 SDL_Swap16
14 #define endianswap32 SDL_Swap32
15 #define endianswap64 SDL_Swap64
16 #else
17 inline ushort endianswap16(ushort n) { return (n<<8) | (n>>8); }
18 inline uint endianswap32(uint n) { return (n<<24) | (n>>24) | ((n>>8)&0xFF00) | ((n<<8)&0xFF0000); }
19 inline ullong endianswap64(ullong n) { return endianswap32(uint(n >> 32)) | ((ullong)endianswap32(uint(n)) << 32); }
20 #endif
21 template<class T> inline T endianswap(T n) { union { T t; uint i; } conv; conv.t = n; conv.i = endianswap32(conv.i); return conv.t; }
22 template<> inline ushort endianswap<ushort>(ushort n) { return endianswap16(n); }
23 template<> inline short endianswap<short>(short n) { return endianswap16(n); }
24 template<> inline uint endianswap<uint>(uint n) { return endianswap32(n); }
25 template<> inline int endianswap<int>(int n) { return endianswap32(n); }
26 template<> inline ullong endianswap<ullong>(ullong n) { return endianswap64(n); }
27 template<> inline llong endianswap<llong>(llong n) { return endianswap64(n); }
28 template<> inline double endianswap<double>(double n) { union { double t; uint i; } conv; conv.t = n; conv.i = endianswap64(conv.i); return conv.t; }
29 template<class T> inline void endianswap(T *buf, size_t len) { for(T *end = &buf[len]; buf < end; buf++) *buf = endianswap(*buf); }
30 template<class T> inline T endiansame(T n) { return n; }
31 template<class T> inline void endiansame(T *buf, size_t len) {}
32 #ifdef SDL_BYTEORDER
33 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
34 #define lilswap endiansame
35 #define bigswap endianswap
36 #else
37 #define lilswap endianswap
38 #define bigswap endiansame
39 #endif
40 #else
41 template<class T> inline T lilswap(T n) { return *(const uchar *)&islittleendian ? n : endianswap(n); }
42 template<class T> inline void lilswap(T *buf, size_t len) { if(!*(const uchar *)&islittleendian) endianswap(buf, len); }
43 template<class T> inline T bigswap(T n) { return *(const uchar *)&islittleendian ? endianswap(n) : n; }
44 template<class T> inline void bigswap(T *buf, size_t len) { if(*(const uchar *)&islittleendian) endianswap(buf, len); }
45 #endif
46 
T bigswap(T n)
Definition: cube_endian.hpp:43
uint endianswap< uint >(uint n)
Definition: cube_endian.hpp:24
ushort endianswap16(ushort n)
Definition: cube_endian.hpp:17
unsigned int uint
Definition: cube_types.hpp:9
double endianswap< double >(double n)
Definition: cube_endian.hpp:28
ullong endianswap< ullong >(ullong n)
Definition: cube_endian.hpp:26
T endianswap(T n)
Definition: cube_endian.hpp:21
const int islittleendian
Legacy manual byte order swapping for serialisation.
Definition: cube_endian.hpp:10
unsigned short ushort
Definition: cube_types.hpp:8
unsigned long long int ullong
Definition: cube_types.hpp:12
short endianswap< short >(short n)
Definition: cube_endian.hpp:23
T endiansame(T n)
Definition: cube_endian.hpp:30
void t(T x, const char *cmp)
Definition: utilTest.cpp:52
int endianswap< int >(int n)
Definition: cube_endian.hpp:25
uint endianswap32(uint n)
Definition: cube_endian.hpp:18
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
ushort endianswap< ushort >(ushort n)
Definition: cube_endian.hpp:22
int end()
Definition: glemu.cpp:256
llong endianswap< llong >(llong n)
Definition: cube_endian.hpp:27
ullong endianswap64(ullong n)
Definition: cube_endian.hpp:19
signed long long int llong
Definition: cube_types.hpp:11
T lilswap(T n)
Definition: cube_endian.hpp:41