_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

myendian.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 2000-2011 by Lutz Sammer and Pali Rohár
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __ENDIAN_H__
31 #define __ENDIAN_H__
32 
34 
35 /*----------------------------------------------------------------------------
36 -- Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include "SDL_byteorder.h"
40 #include "SDL_endian.h"
41 
42 /*----------------------------------------------------------------------------
43 -- Macros
44 ----------------------------------------------------------------------------*/
45 
49 #define ConvertLE16(v) SDL_SwapLE16((v))
50 
54 #define ConvertLE32(v) SDL_SwapLE32((v))
55 
59 #ifdef __ULTRA_SPARC__
60 
61 extern unsigned short inline AccessLE16(unsigned char *p)
62 {
63  return p[0] + (p[1] << 8);
64 }
65 
66 #else
67 
68 #define AccessLE16(p) SDL_SwapLE16(*((unsigned short *)(p)))
69 
70 #endif
71 
75 #ifdef __ULTRA_SPARC__
76 
77 extern unsigned inline AccessLE32(unsigned char *p)
78 {
79  return p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
80 }
81 
82 #else
83 
84 #define AccessLE32(p) SDL_SwapLE32(*((unsigned int *)(p)))
85 
86 #endif
87 
92 #ifdef __ULTRA_SPARC__
93 
94 extern unsigned short inline _FetchLE16(unsigned char **pp)
95 {
96  unsigned char *p = *pp;
97  unsigned short i = p[0] + (p[1] << 8);
98  (*pp) += 2;
99  return i;
100 }
101 #define FetchLE16(p) _FetchLE16(&p)
102 
103 #else
104 
105 #define FetchLE16(p) SDL_SwapLE16(*((unsigned short *)(p))); p += 2
106 
107 #endif
108 
113 #ifdef __ULTRA_SPARC__
114 
115 extern unsigned inline _FetchLE32(unsigned char **pp)
116 {
117  unsigned char *p = *pp;
118  unsigned int i = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
119  (*pp) += 4;
120  return i;
121 }
122 #define FetchLE32(p) _FetchLE32(&p)
123 
124 #else
125 
126 #define FetchLE32(p) SDL_SwapLE32(*((unsigned int *)(p))); p += 4
127 
128 #endif
129 
130 /* byte order defines */
131 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
132 #define STRATAGUS_LITTLE_ENDIAN
133 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
134 #define STRATAGUS_BIG_ENDIAN
135 #else
136 #error Neither SDL_BIG_ENDIAN nor SDL_LIL_ENDIAN is set
137 #endif
138 
139 // ============================================================================
140 #else // }{ SDL
141 // ============================================================================
142 
143 /*----------------------------------------------------------------------------
144 -- Includes
145 ----------------------------------------------------------------------------*/
146 
147 #if !defined(USE_WIN32)
148 
149 #if defined(USE_BSD)
150 #include <sys/types.h>
151 #else
152 #include <endian.h>
153 #endif // USE_BSD
154 
155 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
156 #include <byteswap.h>
157 #endif
158 
159 #if defined(USE_MAC)
160 #include <architecture/byte_order.h>
161 #endif
162 
163 #endif
164 
165 /*----------------------------------------------------------------------------
166 -- Macros
167 ----------------------------------------------------------------------------*/
168 
169 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
170 
174 #define ConvertLE16(v) bswap_16((v))
175 
179 #define ConvertLE32(v) bswap_32((v))
180 
181 #else
182 
183 #if defined(USE_MAC)
184 
188 #define ConvertLE16(v) NXSwapLittleShortToHost(v)
189 
193 #define ConvertLE32(v) NXSwapLittleIntToHost(v)
194 
195 #else
196 
200 #define ConvertLE16(v) (v)
201 
205 #define ConvertLE32(v) (v)
206 
207 #endif // defined(USE_MAC)
208 
209 #endif // ! defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
210 
214 #define AccessLE16(p) ConvertLE16(*((unsigned short *)(p)))
215 
219 #define AccessLE32(p) ConvertLE32(*((unsigned int *)(p)))
220 
225 #define FetchLE16(p) ConvertLE16(*((unsigned short *)(p))++)
226 
231 #define FetchLE32(p) ConvertLE32(*((unsigned int *)(p))++)
232 
237 #define FetchByte(p) (*((unsigned char *)(p))++)
238 
240 
241 #endif // !__ENDIAN_H__
AccessLE16
#define AccessLE16(p)
Definition: myendian.h:68
AccessLE32
#define AccessLE32(p)
Definition: myendian.h:84
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.