From 197e3f144d2b8707307ebad98d79ff22373b9ee7 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 22 Sep 2005 09:11:27 +0000 Subject: [PATCH] Create our own stdint.h(xx) implementation and use it where needed. --- simgear/compiler.h | 14 +---- simgear/io/lowlevel.hxx | 52 +----------------- simgear/io/lowtest.cxx | 4 +- simgear/misc/Makefile.am | 7 ++- simgear/misc/stdint.hxx | 106 +++++++++++++++++++++++++++++++++++++ simgear/misc/swap_test.cpp | 20 +++++++ 6 files changed, 135 insertions(+), 68 deletions(-) create mode 100644 simgear/misc/stdint.hxx create mode 100644 simgear/misc/swap_test.cpp diff --git a/simgear/compiler.h b/simgear/compiler.h index e1e683b3..b168252d 100644 --- a/simgear/compiler.h +++ b/simgear/compiler.h @@ -467,17 +467,5 @@ inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const) #endif // SG_INCOMPLETE_FUNCTIONAL - -// stdint.h defines -#if defined( _MSC_VER ) || defined(__MINGW32__) || defined(sun) -typedef signed char int8_t; -typedef signed short int16_t; -typedef signed int int32_t; -typedef signed __int64 int64_t; -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned __int64 uint64_t; -#endif - #endif // _SG_COMPILER_H + diff --git a/simgear/io/lowlevel.hxx b/simgear/io/lowlevel.hxx index 1672a72f..a8af66ae 100644 --- a/simgear/io/lowlevel.hxx +++ b/simgear/io/lowlevel.hxx @@ -29,13 +29,11 @@ #include #include -#ifndef _MSC_VER -# include -#endif #include #include +#include // Note that output is written in little endian form (and converted as // necessary for big endian machines) @@ -117,52 +115,4 @@ void sgClearWriteError(); int sgReadError(); int sgWriteError(); -inline bool sgIsLittleEndian() { - static const int sgEndianTest = 1; - return (*((char *) &sgEndianTest ) != 0); -} - -inline bool sgIsBigEndian() { - static const int sgEndianTest = 1; - return (*((char *) &sgEndianTest ) == 0); -} - -inline void sgEndianSwap(unsigned short *x) { - *x = - (( *x >> 8 ) & 0x00FF ) | - (( *x << 8 ) & 0xFF00 ) ; -} - -inline void sgEndianSwap(unsigned int *x) { - *x = - (( *x >> 24 ) & 0x000000FF ) | - (( *x >> 8 ) & 0x0000FF00 ) | - (( *x << 8 ) & 0x00FF0000 ) | - (( *x << 24 ) & 0xFF000000 ) ; -} - -inline void sgEndianSwap(uint64_t *x) { -#ifndef _MSC_VER - *x = - (( *x >> 56 ) & 0x00000000000000FFULL ) | - (( *x >> 40 ) & 0x000000000000FF00ULL ) | - (( *x >> 24 ) & 0x0000000000FF0000ULL ) | - (( *x >> 8 ) & 0x00000000FF000000ULL ) | - (( *x << 8 ) & 0x000000FF00000000ULL ) | - (( *x << 24 ) & 0x0000FF0000000000ULL ) | - (( *x << 40 ) & 0x00FF000000000000ULL ) | - (( *x << 56 ) & 0xFF00000000000000ULL ) ; -#else - *x = - (( *x >> 56 ) & 0x00000000000000FF ) | - (( *x >> 40 ) & 0x000000000000FF00 ) | - (( *x >> 24 ) & 0x0000000000FF0000 ) | - (( *x >> 8 ) & 0x00000000FF000000 ) | - (( *x << 8 ) & 0x000000FF00000000 ) | - (( *x << 24 ) & 0x0000FF0000000000 ) | - (( *x << 40 ) & 0x00FF000000000000 ) | - (( *x << 56 ) & 0xFF00000000000000 ) ; -#endif -} - #endif // _SG_LOWLEVEL_HXX diff --git a/simgear/io/lowtest.cxx b/simgear/io/lowtest.cxx index 85ecf6b3..924e1e11 100644 --- a/simgear/io/lowtest.cxx +++ b/simgear/io/lowtest.cxx @@ -39,8 +39,8 @@ int main() { double x = 1111111111; cout << "double x = " << x << endl; - sgEndianSwap((unsigned long long *)&x); + sgEndianSwap((uint64_t *)&x); cout << "double x = " << x << endl; - sgEndianSwap((unsigned long long *)&x); + sgEndianSwap((uint64_t *)&x); cout << "double x = " << x << endl; } diff --git a/simgear/misc/Makefile.am b/simgear/misc/Makefile.am index f54ba139..4e9b64ee 100644 --- a/simgear/misc/Makefile.am +++ b/simgear/misc/Makefile.am @@ -10,7 +10,8 @@ include_HEADERS = \ tabbed_values.hxx \ texcoord.hxx \ zfstream.hxx \ - interpolator.hxx + interpolator.hxx \ + stdint.hxx libsgmisc_a_SOURCES = \ sg_path.cxx \ @@ -21,7 +22,7 @@ libsgmisc_a_SOURCES = \ zfstream.cxx \ interpolator.cxx -noinst_PROGRAMS = tabbed_value_test +noinst_PROGRAMS = tabbed_value_test swap_test tabbed_value_test_SOURCES = tabbed_values_test.cxx tabbed_value_test_LDADD = \ @@ -29,4 +30,6 @@ tabbed_value_test_LDADD = \ $(top_builddir)/simgear/xml/libsgxml.a \ $(top_builddir)/simgear/debug/libsgdebug.a +swap_test_SOURCES = swap_test.cpp + INCLUDES = -I$(top_srcdir) diff --git a/simgear/misc/stdint.hxx b/simgear/misc/stdint.hxx new file mode 100644 index 00000000..22dbcdc2 --- /dev/null +++ b/simgear/misc/stdint.hxx @@ -0,0 +1,106 @@ + +#ifndef _STDINT_HXX +#define _STDINT_HXX 1 + +// Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. +// +// $Id$ + + +////////////////////////////////////////////////////////////////////// +// +// There are many sick systems out there: +// +// check for sizeof(float) and sizeof(double) +// if sizeof(float) != 4 this code must be patched +// if sizeof(double) != 8 this code must be patched +// +// Those are comments I fetched out of glibc source: +// - s390 is big-endian +// - Sparc is big-endian, but v9 supports endian conversion +// on loads/stores and GCC supports such a mode. Be prepared. +// - The MIPS architecture has selectable endianness. +// - x86_64 is little-endian. +// - CRIS is little-endian. +// - m68k is big-endian. +// - Alpha is little-endian. +// - PowerPC can be little or big endian. +// - SH is bi-endian but with a big-endian FPU. +// - hppa1.1 big-endian. +// - ARM is (usually) little-endian but with a big-endian FPU. +// +////////////////////////////////////////////////////////////////////// + + +#ifdef _MSC_VER +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef signed __int64 int64_t; +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned __int64 uint64_t; +#else +# include +#endif + +#include + + +inline uint16_t sg_bswap_16(uint16_t b) { + unsigned short x = b; + ulEndianSwap(&x); + return x; +} + +inline uint32_t sg_bswap_32(uint32_t b) { + unsigned x = b; + ulEndianSwap(&x); + return x; +} + +inline uint64_t sg_bswap_64(uint64_t b) { + uint64_t x = b; + x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL); x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL); x = (x >> 32) | (x << 32); + return x; +} + + +inline bool sgIsLittleEndian() { + static const int sgEndianTest = 1; + return (*((char *) &sgEndianTest ) != 0); +} + +inline bool sgIsBigEndian() { + static const int sgEndianTest = 1; + return (*((char *) &sgEndianTest ) == 0); +} + +inline void sgEndianSwap(unsigned short *x) { ulEndianSwap(x); } +inline void sgEndianSwap(unsigned int *x) { ulEndianSwap(x); } +#if (SIZEOF_LONG_INT == 8) +inline void sgEndianSwap(unsigned long int *x) { *x = sg_bswap_64(*x); } +#else +inline void sgEndianSwap(unsigned long long *x) { *x = sg_bswap_64(*x); } +#endif + + + +#endif // !_STDINT_HXX + diff --git a/simgear/misc/swap_test.cpp b/simgear/misc/swap_test.cpp new file mode 100644 index 00000000..2101d45b --- /dev/null +++ b/simgear/misc/swap_test.cpp @@ -0,0 +1,20 @@ + +#include +#include "stdint.hxx" + +int main() +{ + uint16_t sui16, ui16 = 0x0123; + uint32_t sui32, ui32 = 0x01234567; + uint64_t sui64, ui64 = 0x0123456789ABCDEFLL; + + sui16 = ui16; sgEndianSwap(&sui16); + sui32 = ui32; sgEndianSwap(&sui32); + sui64 = ui64; sgEndianSwap(&sui64); + + printf("\nUI16: %x (normal)\t\t %x (swapped)\n", ui16, sui16 ); + printf("UI32: %x (normal)\t\t %x (swapped)\n", ui32, sui32 ); + printf("UI64: %llx (normal)\t %llx (swapped)\n\n", ui64, sui64 ); + + return 0; +} -- 2.39.5