0ad/source/lib/endian.cpp
janwas 27f8a68de3 some smaller fixes
This was SVN commit r293.
2004-05-29 12:04:42 +00:00

70 lines
772 B
C++
Executable File

#include "precompiled.h"
#include "lib.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
void bswap32(const u8* data, int cnt)
{
#ifdef _M_IX86
UNUSED(data)
UNUSED(cnt)
__asm
{
mov edx, [data]
mov ecx, [cnt]
$loop: mov eax, [edx]
bswap eax
mov [edx], eax
add edx, 4
dec ecx
jnz $loop
}
#else
u32* p = (u32*)data;
for(int i = 0; i < cnt; i++, p++)
*p = bswap32(*p);
#endif
}