63 lines
1.4 KiB
C++
63 lines
1.4 KiB
C++
/* -----------------------------------------------------------------------------
|
|
GSFramework
|
|
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
|
#include "memory/endian.h"
|
|
#include "platform_config.h"
|
|
#include "log/log.h"
|
|
|
|
|
|
namespace GS {
|
|
namespace Endian {
|
|
|
|
static Config g_host_endian = Undefined;
|
|
|
|
//------------------------------------------------------------------------------
|
|
void SwapBytes(void *in_p, size_t n)
|
|
{
|
|
if (n == 1)
|
|
return;
|
|
|
|
__ASSERT__(!(n & 1));
|
|
|
|
char *p = (char *)in_p;
|
|
for (size_t c = 0; c < n / 2; ++c)
|
|
{
|
|
char tmp = p[c];
|
|
p[c] = p[n - c - 1];
|
|
p[n - c - 1] = tmp;
|
|
}
|
|
}
|
|
Config GetHostConfiguration()
|
|
{
|
|
if (g_host_endian == Undefined)
|
|
{
|
|
union
|
|
{
|
|
uint i;
|
|
char c[4];
|
|
} bint = { 0x01020304 };
|
|
|
|
g_host_endian = bint.c[0] == 1 ? Big : Little;
|
|
|
|
if (g_host_endian == Big)
|
|
__LOG__ << "Memory configuration: Big endian.\n";
|
|
else __LOG__ << "Memory configuration: Little endian.\n";
|
|
}
|
|
return g_host_endian;
|
|
}
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
void *ToHost(void *p, size_t size, Config source_endian)
|
|
{
|
|
if (GetHostConfiguration() != source_endian)
|
|
SwapBytes(p, size);
|
|
return p;
|
|
}
|
|
//------------------------------------------------------------------------------
|
|
|
|
}
|
|
} |