Newer
Older
sample / sdl-d / import / sdl / c / sdl_endian.d
/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2006 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
module sdl.c.sdl_endian;

public
{
	import sdl.c.sdl_stdinc;
}


extern (C):

const
{
	int SDL_LIL_ENDIAN = 1234;
	int SDL_BIG_ENDIAN = 4321;
}

version (BigEndian)
{
	const int SDL_BYTEORDER = SDL_BIG_ENDIAN;
}
else
{
	const int SDL_BYTEORDER = SDL_LIL_ENDIAN;
}

/* Use inline functions for compilers that support them, and static
   functions for those that do not.  Because these functions become
   static for compilers that do not support inline functions, this
   header should only be included in files that actually use them.
*/
Uint16 SDL_Swap16(Uint16 x)
{
	return cast(Uint16) (( cast(int) x << 8) | (cast(int) x >> 8));
}

Uint32 SDL_Swap32(Uint32 x)
{
	return ((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
}

Uint64 SDL_Swap64(Uint64 x)
{
	Uint32 hi, lo;

	/* Separate into high and low 32-bit values and swap them */
	lo = cast(Uint32) (x&0xFFFFFFFF);
	x >>= 32;
	hi = cast(Uint32) (x&0xFFFFFFFF);
	x = SDL_Swap32(lo);
	x <<= 32;
	x |= SDL_Swap32(hi);
	return (x);
}

/* Byteswap item from the specified endianness to the native endianness */
version (LittleEndian)
{
	Uint16 SDL_SwapLE16(Uint16 x) { return x; }
	Uint32 SDL_SwapLE32(Uint32 x) { return x; }
	Uint64 SDL_SwapLE64(Uint64 x) { return x; }
	alias SDL_Swap16 SDL_SwapBE16;
	alias SDL_Swap32 SDL_SwapBE32;
	alias SDL_Swap64 SDL_SwapBE64;
}
else
{
	alias SDL_Swap16 SDL_SwapLE16;
	alias SDL_Swap32 SDL_SwapLE32;
	alias SDL_Swap64 SDL_SwapLE64;
	Uint16 SDL_SwapBE16(Uint16 x) { return x; }
	Uint32 SDL_SwapBE32(Uint32 x) { return x; }
	Uint64 SDL_SwapBE64(Uint63 x) { return x; }
}