////////////////////////////////////////////////////////////////////////////////
//
//  GSByteSwap.h
//
//	Copyright Nemetschek North America, Inc.,  2004.
//	All Rights Reserved.
//
//	Started 2004-07-29 by John Williams
//
//  This file contains generic byte-swapping routines. 
//	It is part of the SDK, and does not include platform-specific data types.
//
////////////////////////////////////////////////////////////////////////////////


#pragma once

#include "GSTypes.h"



// ---------- 16 bit --------------------------------------------------------

inline void GSByteSwap(SInt16 &data) 
{
	char *p1 = (char*)&data;
	char *p2 = p1 + 1;
	const char t = *p1;
	*p1 = *p2;
	*p2 = t;
}

inline void GSByteSwap(UInt16 &data)	{ GSByteSwap(*(Sint16*)&data); }


// ---------- 32 bit --------------------------------------------------------

inline void GSByteSwap(SInt32 &data)
{
	char t;
	char* p1 = (char*)&data;
	char* p2 = p1 + 3;

	t = *p1;
	*p1 = *p2;
	*p2 = t;

	++p1; --p2;

	t = *p1;
	*p1 = *p2;
	*p2 = t;
}

inline void GSByteSwap(UInt32 &data)	{ GSByteSwap(*(Sint32*)&data); }


// ---------- 64 bit --------------------------------------------------------

inline void GSByteSwap(Real64& data )
{
	union
	{	
		double	theDouble;
		char	theBytes[ sizeof(double) ];
	} src, dst;

	src.theDouble = data;
	dst.theBytes[ 0 ] = src.theBytes[ 7 ];
	dst.theBytes[ 1 ] = src.theBytes[ 6 ];
	dst.theBytes[ 2 ] = src.theBytes[ 5 ];
	dst.theBytes[ 3 ] = src.theBytes[ 4 ];
	dst.theBytes[ 4 ] = src.theBytes[ 3 ];
	dst.theBytes[ 5 ] = src.theBytes[ 2 ];
	dst.theBytes[ 6 ] = src.theBytes[ 1 ];
	dst.theBytes[ 7 ] = src.theBytes[ 0 ];
	data = dst.theDouble;
}

