byte.c

Go to the documentation of this file.
00001 
00006 /*
00007 All original material Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 Copyright (C) 1997-2001 Id Software, Inc.
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020 See the GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00025 
00026 */
00027 
00028 #include <SDL/SDL_endian.h>
00029 #include "../common/common.h"
00030 
00031 /* can't just use function pointers, or dll linkage can */
00032 /* mess up when common is included in multiple places */
00033 static float (*_BigFloat)(float l);
00034 static float (*_LittleFloat)(float l);
00035 
00036 short BigShort (uint16_t l)
00037 {
00038     return SDL_SwapBE16(l);
00039 }
00040 short LittleShort (uint16_t l)
00041 {
00042     return SDL_SwapLE16(l);
00043 }
00044 int BigLong (uint32_t l)
00045 {
00046     return SDL_SwapBE32(l);
00047 }
00048 int LittleLong (uint32_t l)
00049 {
00050     return SDL_SwapLE32(l);
00051 }
00052 float BigFloat (float l)
00053 {
00054     return _BigFloat(l);
00055 }
00056 float LittleFloat (float l)
00057 {
00058     return _LittleFloat(l);
00059 }
00060 
00064 static float FloatSwap (float f)
00065 {
00066     union float_u {
00067         float f;
00068         byte b[4];
00069     } dat1, dat2;
00070 
00071     dat1.f = f;
00072     dat2.b[0] = dat1.b[3];
00073     dat2.b[1] = dat1.b[2];
00074     dat2.b[2] = dat1.b[1];
00075     dat2.b[3] = dat1.b[0];
00076     return dat2.f;
00077 }
00078 
00082 static float FloatNoSwap (float f)
00083 {
00084     return f;
00085 }
00086 
00087 void Swap_Init (void)
00088 {
00089     Com_Printf("---- endianness initialization -----\n");
00090 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00091     Com_Printf("found little endian system\n");
00092     _BigFloat = FloatSwap;
00093     _LittleFloat = FloatNoSwap;
00094 #else
00095     Com_Printf("found big endian system\n");
00096     _BigFloat = FloatNoSwap;
00097     _LittleFloat = FloatSwap;
00098 #endif
00099 }

Generated by  doxygen 1.6.2