xref: /trunk/main/sal/inc/osl/endian.h (revision e15a4534)
19eab2a37SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39eab2a37SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49eab2a37SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59eab2a37SAndrew Rist  * distributed with this work for additional information
69eab2a37SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79eab2a37SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89eab2a37SAndrew Rist  * "License"); you may not use this file except in compliance
99eab2a37SAndrew Rist  * with the License.  You may obtain a copy of the License at
109eab2a37SAndrew Rist  *
119eab2a37SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129eab2a37SAndrew Rist  *
139eab2a37SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149eab2a37SAndrew Rist  * software distributed under the License is distributed on an
159eab2a37SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169eab2a37SAndrew Rist  * KIND, either express or implied.  See the License for the
179eab2a37SAndrew Rist  * specific language governing permissions and limitations
189eab2a37SAndrew Rist  * under the License.
199eab2a37SAndrew Rist  *
209eab2a37SAndrew Rist  *************************************************************/
219eab2a37SAndrew Rist 
229eab2a37SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _OSL_ENDIAN_H_
25cdf0e10cSrcweir #define _OSL_ENDIAN_H_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifdef __cplusplus
30cdf0e10cSrcweir extern "C" {
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /** Determine the platform byte order as _BIG_ENDIAN, _LITTLE_ENDIAN, ...
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir #ifdef _WIN32
36cdf0e10cSrcweir #	if defined(_M_IX86)
37cdf0e10cSrcweir #		define _LITTLE_ENDIAN
38cdf0e10cSrcweir #	elif defined(_M_MRX000)
39cdf0e10cSrcweir #		define _LITTLE_ENDIAN
40cdf0e10cSrcweir #	elif defined(_M_ALPHA)
41cdf0e10cSrcweir #		define _LITTLE_ENDIAN
42cdf0e10cSrcweir #	elif defined(_M_PPC)
43cdf0e10cSrcweir #		define _LITTLE_ENDIAN
44cdf0e10cSrcweir #	endif
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #ifdef LINUX
48cdf0e10cSrcweir #	include <endian.h>
49cdf0e10cSrcweir #	if __BYTE_ORDER == __LITTLE_ENDIAN
50cdf0e10cSrcweir #           ifndef _LITTLE_ENDIAN
51cdf0e10cSrcweir #		define _LITTLE_ENDIAN
52cdf0e10cSrcweir #           endif
53cdf0e10cSrcweir #	elif __BYTE_ORDER == __BIG_ENDIAN
54cdf0e10cSrcweir #           ifndef _BIG_ENDIAN
55cdf0e10cSrcweir #		define _BIG_ENDIAN
56cdf0e10cSrcweir #           endif
57cdf0e10cSrcweir #	elif __BYTE_ORDER == __PDP_ENDIAN
58cdf0e10cSrcweir #		define _PDP_ENDIAN
59cdf0e10cSrcweir #	endif
60cdf0e10cSrcweir #endif
61cdf0e10cSrcweir 
62cdf0e10cSrcweir #ifdef NETBSD
63cdf0e10cSrcweir #   include <machine/endian.h>
64cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
65cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
66cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
67cdf0e10cSrcweir #   	define _BIG_ENDIAN
68cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
69cdf0e10cSrcweir #   	define _PDP_ENDIAN
70cdf0e10cSrcweir #   endif
71cdf0e10cSrcweir #endif
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #ifdef FREEBSD
74cdf0e10cSrcweir #   include <sys/param.h>
75cdf0e10cSrcweir #   include <machine/endian.h>
76cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
77*e15a4534Spfg #   	undef _BIG_ENDIAN
78cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
79*e15a4534Spfg #   	undef _LITTLE_ENDIAN
80cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
81cdf0e10cSrcweir #   	define _PDP_ENDIAN
82cdf0e10cSrcweir #   endif
83cdf0e10cSrcweir #endif
84cdf0e10cSrcweir 
85cdf0e10cSrcweir #ifdef SCO
86cdf0e10cSrcweir #   include <sys/types.h>
87cdf0e10cSrcweir #   include <sys/byteorder.h>
88cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
89cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
90cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
91cdf0e10cSrcweir #   	define _BIG_ENDIAN
92cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
93cdf0e10cSrcweir #   	define _PDP_ENDIAN
94cdf0e10cSrcweir #   endif
95cdf0e10cSrcweir #endif
96cdf0e10cSrcweir 
97cdf0e10cSrcweir #ifdef AIX
98cdf0e10cSrcweir #	include <sys/machine.h>
99cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
100cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
101cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
102cdf0e10cSrcweir #   	define _BIG_ENDIAN
103cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
104cdf0e10cSrcweir #   	define _PDP_ENDIAN
105cdf0e10cSrcweir #   endif
106cdf0e10cSrcweir #endif
107cdf0e10cSrcweir 
108cdf0e10cSrcweir #ifdef HPUX
109cdf0e10cSrcweir #	include <machine/param.h>
110cdf0e10cSrcweir #endif
111cdf0e10cSrcweir 
112cdf0e10cSrcweir #ifdef _WIN16
113cdf0e10cSrcweir #	define	_LITTLE_ENDIAN
114cdf0e10cSrcweir #endif
115cdf0e10cSrcweir 
116cdf0e10cSrcweir #ifdef OS2
117cdf0e10cSrcweir #	include <machine/endian.h>
118cdf0e10cSrcweir #endif
119cdf0e10cSrcweir 
120cdf0e10cSrcweir #ifdef SOLARIS
121cdf0e10cSrcweir #	include <sys/isa_defs.h>
122cdf0e10cSrcweir #endif
123cdf0e10cSrcweir 
124cdf0e10cSrcweir #ifdef MACOSX
125cdf0e10cSrcweir #	include <machine/endian.h>
126cdf0e10cSrcweir #	if BYTE_ORDER == LITTLE_ENDIAN
127cdf0e10cSrcweir #		ifndef _LITTLE_ENDIAN
128cdf0e10cSrcweir #		define _LITTLE_ENDIAN
129cdf0e10cSrcweir #		endif
130cdf0e10cSrcweir #	elif BYTE_ORDER == BIG_ENDIAN
131cdf0e10cSrcweir #		ifndef _BIG_ENDIAN
132cdf0e10cSrcweir #		define _BIG_ENDIAN
133cdf0e10cSrcweir #		endif
134cdf0e10cSrcweir #	elif BYTE_ORDER == PDP_ENDIAN
135cdf0e10cSrcweir #		ifndef _PDP_ENDIAN
136cdf0e10cSrcweir #		define _PDP_ENDIAN
137cdf0e10cSrcweir #		endif
138cdf0e10cSrcweir #	endif
139cdf0e10cSrcweir #endif
140cdf0e10cSrcweir 
141cdf0e10cSrcweir /** Check supported platform.
142cdf0e10cSrcweir  */
143cdf0e10cSrcweir #if !defined(_WIN32)  && !defined(_WIN16) && !defined(OS2)   && \
144cdf0e10cSrcweir     !defined(LINUX)   && !defined(NETBSD) && !defined(SCO)   && \
145cdf0e10cSrcweir 	!defined(AIX)     && !defined(HPUX)   && \
146cdf0e10cSrcweir 	!defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD)
147cdf0e10cSrcweir #	error "Target platform not specified !"
148cdf0e10cSrcweir #endif
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
152cdf0e10cSrcweir  */
153cdf0e10cSrcweir #if defined _LITTLE_ENDIAN
154cdf0e10cSrcweir #	define OSL_LITENDIAN
155cdf0e10cSrcweir #elif defined _BIG_ENDIAN
156cdf0e10cSrcweir #	define OSL_BIGENDIAN
157cdf0e10cSrcweir #else
15886e1cf34SPedro Giffuni #	error undetermined endianness
159cdf0e10cSrcweir #endif
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir /** Define macros for byte order manipulation.
163cdf0e10cSrcweir  */
164cdf0e10cSrcweir #ifndef OSL_MAKEBYTE
165cdf0e10cSrcweir #	define OSL_MAKEBYTE(nl, nh)    ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
166cdf0e10cSrcweir #endif
167cdf0e10cSrcweir #ifndef OSL_LONIBBLE
168cdf0e10cSrcweir #	define OSL_LONIBBLE(b)         ((sal_uInt8)((b) & 0x0F))
169cdf0e10cSrcweir #endif
170cdf0e10cSrcweir #ifndef OSL_HINIBBLE
171cdf0e10cSrcweir #	define OSL_HINIBBLE(b)         ((sal_uInt8)(((b) >> 4) & 0x0F))
172cdf0e10cSrcweir #endif
173cdf0e10cSrcweir 
174cdf0e10cSrcweir #ifndef OSL_MAKEWORD
175cdf0e10cSrcweir #	define OSL_MAKEWORD(bl, bh)    ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
176cdf0e10cSrcweir #endif
177cdf0e10cSrcweir #ifndef OSL_LOBYTE
178cdf0e10cSrcweir #	define OSL_LOBYTE(w)           ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
179cdf0e10cSrcweir #endif
180cdf0e10cSrcweir #ifndef OSL_HIBYTE
181cdf0e10cSrcweir #	define OSL_HIBYTE(w)           ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
182cdf0e10cSrcweir #endif
183cdf0e10cSrcweir 
184cdf0e10cSrcweir #ifndef OSL_MAKEDWORD
185cdf0e10cSrcweir #	define OSL_MAKEDWORD(wl, wh)   ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
186cdf0e10cSrcweir #endif
187cdf0e10cSrcweir #ifndef OSL_LOWORD
188cdf0e10cSrcweir #	define OSL_LOWORD(d)           ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
189cdf0e10cSrcweir #endif
190cdf0e10cSrcweir #ifndef OSL_HIWORD
191cdf0e10cSrcweir #	define OSL_HIWORD(d)           ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
192cdf0e10cSrcweir #endif
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195cdf0e10cSrcweir /** Define macros for swapping between host and network byte order.
196cdf0e10cSrcweir  */
197cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
198cdf0e10cSrcweir #ifndef OSL_NETWORD
199cdf0e10cSrcweir #	define OSL_NETWORD(w)          (sal_uInt16)(w)
200cdf0e10cSrcweir #endif
201cdf0e10cSrcweir #ifndef OSL_NETDWORD
202cdf0e10cSrcweir #	define OSL_NETDWORD(d)         (sal_uInt32)(d)
203cdf0e10cSrcweir #endif
204cdf0e10cSrcweir #else  /* OSL_LITENDIAN */
205cdf0e10cSrcweir #ifndef OSL_NETWORD
206cdf0e10cSrcweir #	define OSL_NETWORD(w)          OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
207cdf0e10cSrcweir #endif
208cdf0e10cSrcweir #ifndef OSL_NETDWORD
209cdf0e10cSrcweir #	define OSL_NETDWORD(d)         OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
210cdf0e10cSrcweir #endif
211cdf0e10cSrcweir #endif /* OSL_BIGENDIAN */
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 
214cdf0e10cSrcweir /** Define macros for swapping between byte orders.
215cdf0e10cSrcweir  */
216cdf0e10cSrcweir #ifndef OSL_SWAPWORD
217cdf0e10cSrcweir #	define OSL_SWAPWORD(w)         OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
218cdf0e10cSrcweir #endif
219cdf0e10cSrcweir #ifndef OSL_SWAPDWORD
220cdf0e10cSrcweir #	define OSL_SWAPDWORD(d)        OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
221cdf0e10cSrcweir #endif
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 
224cdf0e10cSrcweir #ifdef __cplusplus
225cdf0e10cSrcweir }
226cdf0e10cSrcweir #endif
227cdf0e10cSrcweir 
228cdf0e10cSrcweir #endif /*_OSL_ENDIAN_H_ */
229cdf0e10cSrcweir 
230