1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
   | #pragma once
#include "Inline.h"
#include "Casting.h"
 
#ifndef COMPILE_TIME_ASSERT
#define COMPILE_TIME_ASSERT(name, cond) typedef char COMPILE_TIME_ASSERT_ ## name [ (cond) ? 1 : -1 ]
#endif
/*
"Standard" XtoYS functions :
	Always work, regardless of current host endianness.
	But, they may seem kind of slow, especially on big-endian hosts.
*/
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4204)//Non-constant agregate initializer (MS extension)
#endif
 
/*--- Big-Endian ---*/
#pragma region Big-Endian
 
static CCPP_INLINE UINT16 HtoBES(UINT16 h)
{
	UINT8 const c[2] = { HIBYTE(h), LOBYTE(h) };
	COMPILE_TIME_ASSERT(SIZE, sizeof(c)==sizeof(h));
	UINT16 const n = * REINTERPRET_CAST(UINT16 const *, &c);
	return n;
} 
 
static CCPP_INLINE UINT16 BEtoHS(UINT16 n)
{
	UINT8 const * const c = REINTERPRET_CAST(UINT8 const *, &n);
	UINT16 const h_high = c[0];
	UINT16 const h_low = c[1];
	return ((h_high << 8) | h_low);
}
 
static CCPP_INLINE UINT32 HtoBEL(UINT32 h)
{
	UINT8 const c[4] = { HIBYTE(HIWORD(h)), LOBYTE(HIWORD(h)), HIBYTE(LOWORD(h)), LOBYTE(LOWORD(h)) };
	COMPILE_TIME_ASSERT(SIZE, sizeof(c)==sizeof(h));
	UINT32 const n = * REINTERPRET_CAST(UINT32 const *, &c);
	return n;
} 
 
static CCPP_INLINE UINT32 NtoBEL(UINT32 n)
{
	UINT8 const * const c = REINTERPRET_CAST(UINT8 const *, &n);
	UINT32 const h_highhigh = c[0];
	UINT32 const h_highlow = c[1];
	UINT32 const h_lowhigh = c[2];
	UINT32 const h_lowlow = c[3];
	return ((h_highhigh << 24) | (h_highlow << 16) | (h_lowhigh << 8) | h_lowlow);
} 
#pragma endregion
 
/*--- Little-Endian ---*/
#pragma region Little-Endian
 
static CCPP_INLINE UINT16 HtoLES(UINT16 h)
{
	UINT8 const c[2] = { LOBYTE(h), HIBYTE(h) };
	COMPILE_TIME_ASSERT(SIZE, sizeof(c)==sizeof(h));
	UINT16 const n = * REINTERPRET_CAST(UINT16 const *, &c);
	return n;
} 
 
static CCPP_INLINE UINT16 LEtoHS(UINT16 n)
{
	UINT8 const * c = REINTERPRET_CAST(UINT8 const *, &n);
	UINT16 const h_high = c[1];
	UINT16 const h_low = c[0];
	return ((h_high << 8) | h_low);
}
 
static CCPP_INLINE UINT32 HtoLEL(UINT32 h)
{
	UINT8 const c[4] = { LOBYTE(LOWORD(h)), HIBYTE(LOWORD(h)), LOBYTE(HIWORD(h)), HIBYTE(HIWORD(h)) };
	COMPILE_TIME_ASSERT(SIZE, sizeof(c)==sizeof(h));
	UINT32 const n = * REINTERPRET_CAST(UINT32 const *, &c);
	return n;
} 
 
static CCPP_INLINE UINT32 NtoLEL(UINT32 n)
{
	UINT8 const * c = REINTERPRET_CAST(UINT8 const *, &n);
	UINT32 const h_highhigh = c[3];
	UINT32 const h_highlow = c[2];
	UINT32 const h_lowhigh = c[1];
	UINT32 const h_lowlow = c[0];
	return ((h_highhigh << 24) | (h_highlow << 16) | (h_lowhigh << 8) | h_lowlow);
} 
#pragma endregion
 
#ifdef _MSC_VER
#pragma warning(pop)
#endif
 
/*Macros from "Network" byte order*/
#define HtoNS HtoBES
#define NtoHS BEtoHS
#define HtoNL HtoBEL
#define NtoHL BEtoHL | 
Partager