]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stdint.hxx
a66f8c8cbb0b373dfb97bb7cd42ecc12c0bf2a4e
[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
60 typedef int ssize_t;
61 #elif defined(sgi) || defined(__sun)
62 # include <sys/types.h>
63 #else
64 # include <stdint.h>
65 #endif
66
67
68 inline uint16_t sg_bswap_16(uint16_t x) {
69     x = (x >> 8) | (x << 8);
70     return x;
71 }
72
73 inline uint32_t sg_bswap_32(uint32_t x) {
74     x = ((x >>  8) & 0x00FF00FFL) | ((x <<  8) & 0xFF00FF00L);
75     x = (x >> 16) | (x << 16);
76     return x;
77 }
78
79 inline uint64_t sg_bswap_64(uint64_t x) {
80     x = ((x >>  8) & 0x00FF00FF00FF00FFLL) | ((x <<  8) & 0xFF00FF00FF00FF00LL);
81     x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
82     x = (x >> 32) | (x << 32);
83     return x;
84 }
85
86
87 inline bool sgIsLittleEndian() {
88     static const int sgEndianTest = 1;
89     return (*((char *) &sgEndianTest ) != 0);
90 }
91
92 inline bool sgIsBigEndian() {
93     static const int sgEndianTest = 1;
94     return (*((char *) &sgEndianTest ) == 0);
95 }
96
97 inline void sgEndianSwap(uint16_t *x) { *x = sg_bswap_16(*x); }
98 inline void sgEndianSwap(uint32_t *x) { *x = sg_bswap_32(*x); }
99 inline void sgEndianSwap(uint64_t *x) { *x = sg_bswap_64(*x); }
100
101
102
103 #endif // !_STDINT_HXX
104