]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stdint.hxx
Cygwin fixes.
[simgear.git] / simgear / misc / stdint.hxx
1
2 #ifndef _STDINT_HXX
3 #define _STDINT_HXX 1
4
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 //////////////////////////////////////////////////////////////////////
26 //
27 //  There are many sick systems out there:
28 //
29 //  check for sizeof(float) and sizeof(double)
30 //  if sizeof(float) != 4 this code must be patched
31 //  if sizeof(double) != 8 this code must be patched
32 //
33 //  Those are comments I fetched out of glibc source:
34 //  - s390 is big-endian
35 //  - Sparc is big-endian, but v9 supports endian conversion
36 //    on loads/stores and GCC supports such a mode.  Be prepared.
37 //  - The MIPS architecture has selectable endianness.
38 //  - x86_64 is little-endian.
39 //  - CRIS is little-endian.
40 //  - m68k is big-endian.
41 //  - Alpha is little-endian.
42 //  - PowerPC can be little or big endian.
43 //  - SH is bi-endian but with a big-endian FPU.
44 //  - hppa1.1 big-endian.
45 //  - ARM is (usually) little-endian but with a big-endian FPU.
46 //
47 //////////////////////////////////////////////////////////////////////
48
49
50 #ifdef _MSC_VER
51 typedef signed char      int8_t;
52 typedef signed short     int16_t;
53 typedef signed int       int32_t;
54 typedef signed __int64   int64_t;
55 typedef unsigned char    uint8_t;
56 typedef unsigned short   uint16_t;
57 typedef unsigned int     uint32_t;
58 typedef unsigned __int64 uint64_t;
59 #elif defined(sgi)
60 # include <sys/types.h>
61 #else
62 # include <stdint.h>
63 #endif
64
65
66 inline uint16_t sg_bswap_16(uint16_t x) {
67     x = (x >> 8) | (x << 8);
68     return x;
69 }
70
71 inline uint32_t sg_bswap_32(uint32_t x) {
72     x = ((x >>  8) & 0x00FF00FFL) | ((x <<  8) & 0xFF00FF00L);
73     x = (x >> 16) | (x << 16);
74     return x;
75 }
76
77 inline uint64_t sg_bswap_64(uint64_t x) {
78     x = ((x >>  8) & 0x00FF00FF00FF00FFLL) | ((x <<  8) & 0xFF00FF00FF00FF00LL);
79     x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
80     x = (x >> 32) | (x << 32);
81     return x;
82 }
83
84
85 inline bool sgIsLittleEndian() {
86     static const int sgEndianTest = 1;
87     return (*((char *) &sgEndianTest ) != 0);
88 }
89
90 inline bool sgIsBigEndian() {
91     static const int sgEndianTest = 1;
92     return (*((char *) &sgEndianTest ) == 0);
93 }
94
95 inline void sgEndianSwap(uint16_t *x) { *x = sg_bswap_16(*x); }
96 inline void sgEndianSwap(uint32_t *x) { *x = sg_bswap_32(*x); }
97 inline void sgEndianSwap(uint64_t *x) { *x = sg_bswap_64(*x); }
98
99
100
101 #endif // !_STDINT_HXX
102