5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Written by Curtis Olson - http://www.flightgear.org/~curt
8 // Started September 2001.
10 // This file is in the Public Domain, and comes with no warranty.
15 //////////////////////////////////////////////////////////////////////
17 // There are many sick systems out there:
19 // check for sizeof(float) and sizeof(double)
20 // if sizeof(float) != 4 this code must be patched
21 // if sizeof(double) != 8 this code must be patched
23 // Those are comments I fetched out of glibc source:
24 // - s390 is big-endian
25 // - Sparc is big-endian, but v9 supports endian conversion
26 // on loads/stores and GCC supports such a mode. Be prepared.
27 // - The MIPS architecture has selectable endianness.
28 // - x86_64 is little-endian.
29 // - CRIS is little-endian.
30 // - m68k is big-endian.
31 // - Alpha is little-endian.
32 // - PowerPC can be little or big endian.
33 // - SH is bi-endian but with a big-endian FPU.
34 // - hppa1.1 big-endian.
35 // - ARM is (usually) little-endian but with a big-endian FPU.
37 //////////////////////////////////////////////////////////////////////
41 typedef signed char int8_t;
42 typedef signed short int16_t;
43 typedef signed int int32_t;
44 typedef signed __int64 int64_t;
45 typedef unsigned char uint8_t;
46 typedef unsigned short uint16_t;
47 typedef unsigned int uint32_t;
48 typedef unsigned __int64 uint64_t;
51 #elif defined(sgi) || defined(__sun)
52 # include <sys/types.h>
58 inline uint16_t sg_bswap_16(uint16_t x) {
59 x = (x >> 8) | (x << 8);
63 inline uint32_t sg_bswap_32(uint32_t x) {
64 x = ((x >> 8) & 0x00FF00FFL) | ((x << 8) & 0xFF00FF00L);
65 x = (x >> 16) | (x << 16);
69 inline uint64_t sg_bswap_64(uint64_t x) {
70 x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL);
71 x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
72 x = (x >> 32) | (x << 32);
77 inline bool sgIsLittleEndian() {
78 static const int sgEndianTest = 1;
79 return (*((char *) &sgEndianTest ) != 0);
82 inline bool sgIsBigEndian() {
83 static const int sgEndianTest = 1;
84 return (*((char *) &sgEndianTest ) == 0);
87 inline void sgEndianSwap(int32_t *x) { *x = (int32_t) sg_bswap_32((int32_t) *x); }
88 inline void sgEndianSwap(float *x) { *x = (float) sg_bswap_32((float) *x); }
90 inline void sgEndianSwap(uint16_t *x) { *x = sg_bswap_16(*x); }
91 inline void sgEndianSwap(uint32_t *x) { *x = sg_bswap_32(*x); }
92 inline void sgEndianSwap(uint64_t *x) { *x = sg_bswap_64(*x); }
96 #endif // !_STDINT_HXX