forked from 0ad/0ad
adding endian routines i wrote independently; maybe it's useful, and less clutter in cvs ;)
This was SVN commit r237.
This commit is contained in:
parent
fbba2e463d
commit
b709de582d
40
source/lib/endian.cpp
Executable file
40
source/lib/endian.cpp
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#include "precompiled.h"
|
||||||
|
|
||||||
|
// don't need to implement on VC - header maps bswap* to instrinsics
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
|
||||||
|
u16 bswap16(u16 x)
|
||||||
|
{
|
||||||
|
return (u16)(((x & 0xff) << 8) | (x >> 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
u32 bswap32(u32 x)
|
||||||
|
{
|
||||||
|
u32 t = x;
|
||||||
|
|
||||||
|
for(int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
x <<= 8;
|
||||||
|
x |= t & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
u64 bswap64(u64 x)
|
||||||
|
{
|
||||||
|
u64 t = x;
|
||||||
|
|
||||||
|
for(int i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
x <<= 8;
|
||||||
|
x |= t & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // #ifndef _MSC_VER
|
17
source/lib/endian.h
Executable file
17
source/lib/endian.h
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
// map to intrinsics on VC
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define bswap16 _byteswap_ushort
|
||||||
|
#define bswap32 _byteswap_ulong
|
||||||
|
#define bswap64 _byteswap_uint64
|
||||||
|
|
||||||
|
// otherwise, define our own functions
|
||||||
|
#else
|
||||||
|
|
||||||
|
extern u16 bswap16(u16);
|
||||||
|
extern u32 bswap32(u32);
|
||||||
|
extern u64 bswap64(u64);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user