xref: /trunk/main/sal/inc/osl/endian.h (revision 11ac63c8)
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
38*11ac63c8SDamjan Jovanovic #	elif defined(_M_AMD64)
39*11ac63c8SDamjan Jovanovic #		define _LITTLE_ENDIAN
40cdf0e10cSrcweir #	elif defined(_M_MRX000)
41cdf0e10cSrcweir #		define _LITTLE_ENDIAN
42cdf0e10cSrcweir #	elif defined(_M_ALPHA)
43cdf0e10cSrcweir #		define _LITTLE_ENDIAN
44cdf0e10cSrcweir #	elif defined(_M_PPC)
45cdf0e10cSrcweir #		define _LITTLE_ENDIAN
46cdf0e10cSrcweir #	endif
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #ifdef LINUX
50cdf0e10cSrcweir #	include <endian.h>
51cdf0e10cSrcweir #	if __BYTE_ORDER == __LITTLE_ENDIAN
52cdf0e10cSrcweir #           ifndef _LITTLE_ENDIAN
53cdf0e10cSrcweir #		define _LITTLE_ENDIAN
54cdf0e10cSrcweir #           endif
55cdf0e10cSrcweir #	elif __BYTE_ORDER == __BIG_ENDIAN
56cdf0e10cSrcweir #           ifndef _BIG_ENDIAN
57cdf0e10cSrcweir #		define _BIG_ENDIAN
58cdf0e10cSrcweir #           endif
59cdf0e10cSrcweir #	elif __BYTE_ORDER == __PDP_ENDIAN
60cdf0e10cSrcweir #		define _PDP_ENDIAN
61cdf0e10cSrcweir #	endif
62cdf0e10cSrcweir #endif
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #ifdef NETBSD
65cdf0e10cSrcweir #   include <machine/endian.h>
66cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
67cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
68cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
69cdf0e10cSrcweir #   	define _BIG_ENDIAN
70cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
71cdf0e10cSrcweir #   	define _PDP_ENDIAN
72cdf0e10cSrcweir #   endif
73cdf0e10cSrcweir #endif
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #ifdef FREEBSD
76cdf0e10cSrcweir #   include <sys/param.h>
77cdf0e10cSrcweir #   include <machine/endian.h>
78cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
79e15a4534Spfg #   	undef _BIG_ENDIAN
80cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
81e15a4534Spfg #   	undef _LITTLE_ENDIAN
82cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
83cdf0e10cSrcweir #   	define _PDP_ENDIAN
84cdf0e10cSrcweir #   endif
85cdf0e10cSrcweir #endif
86cdf0e10cSrcweir 
87cdf0e10cSrcweir #ifdef SCO
88cdf0e10cSrcweir #   include <sys/types.h>
89cdf0e10cSrcweir #   include <sys/byteorder.h>
90cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
91cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
92cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
93cdf0e10cSrcweir #   	define _BIG_ENDIAN
94cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
95cdf0e10cSrcweir #   	define _PDP_ENDIAN
96cdf0e10cSrcweir #   endif
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir 
99cdf0e10cSrcweir #ifdef AIX
100cdf0e10cSrcweir #	include <sys/machine.h>
101cdf0e10cSrcweir #   if BYTE_ORDER == LITTLE_ENDIAN
102cdf0e10cSrcweir #   	define _LITTLE_ENDIAN
103cdf0e10cSrcweir #   elif BYTE_ORDER == BIG_ENDIAN
104cdf0e10cSrcweir #   	define _BIG_ENDIAN
105cdf0e10cSrcweir #   elif BYTE_ORDER == PDP_ENDIAN
106cdf0e10cSrcweir #   	define _PDP_ENDIAN
107cdf0e10cSrcweir #   endif
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir 
110cdf0e10cSrcweir #ifdef HPUX
111cdf0e10cSrcweir #	include <machine/param.h>
112cdf0e10cSrcweir #endif
113cdf0e10cSrcweir 
114cdf0e10cSrcweir #ifdef _WIN16
115cdf0e10cSrcweir #	define	_LITTLE_ENDIAN
116cdf0e10cSrcweir #endif
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #ifdef OS2
119cdf0e10cSrcweir #	include <machine/endian.h>
120cdf0e10cSrcweir #endif
121cdf0e10cSrcweir 
122cdf0e10cSrcweir #ifdef SOLARIS
123cdf0e10cSrcweir #	include <sys/isa_defs.h>
124cdf0e10cSrcweir #endif
125cdf0e10cSrcweir 
126cdf0e10cSrcweir #ifdef MACOSX
127cdf0e10cSrcweir #	include <machine/endian.h>
128cdf0e10cSrcweir #	if BYTE_ORDER == LITTLE_ENDIAN
129cdf0e10cSrcweir #		ifndef _LITTLE_ENDIAN
130cdf0e10cSrcweir #		define _LITTLE_ENDIAN
131cdf0e10cSrcweir #		endif
132cdf0e10cSrcweir #	elif BYTE_ORDER == BIG_ENDIAN
133cdf0e10cSrcweir #		ifndef _BIG_ENDIAN
134cdf0e10cSrcweir #		define _BIG_ENDIAN
135cdf0e10cSrcweir #		endif
136cdf0e10cSrcweir #	elif BYTE_ORDER == PDP_ENDIAN
137cdf0e10cSrcweir #		ifndef _PDP_ENDIAN
138cdf0e10cSrcweir #		define _PDP_ENDIAN
139cdf0e10cSrcweir #		endif
140cdf0e10cSrcweir #	endif
141cdf0e10cSrcweir #endif
142cdf0e10cSrcweir 
143cdf0e10cSrcweir /** Check supported platform.
144cdf0e10cSrcweir  */
145cdf0e10cSrcweir #if !defined(_WIN32)  && !defined(_WIN16) && !defined(OS2)   && \
146cdf0e10cSrcweir     !defined(LINUX)   && !defined(NETBSD) && !defined(SCO)   && \
147cdf0e10cSrcweir 	!defined(AIX)     && !defined(HPUX)   && \
148cdf0e10cSrcweir 	!defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD)
149cdf0e10cSrcweir #	error "Target platform not specified !"
150cdf0e10cSrcweir #endif
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
154cdf0e10cSrcweir  */
155cdf0e10cSrcweir #if defined _LITTLE_ENDIAN
156cdf0e10cSrcweir #	define OSL_LITENDIAN
157cdf0e10cSrcweir #elif defined _BIG_ENDIAN
158cdf0e10cSrcweir #	define OSL_BIGENDIAN
159cdf0e10cSrcweir #else
16086e1cf34SPedro Giffuni #	error undetermined endianness
161cdf0e10cSrcweir #endif
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir /** Define macros for byte order manipulation.
165cdf0e10cSrcweir  */
166cdf0e10cSrcweir #ifndef OSL_MAKEBYTE
167cdf0e10cSrcweir #	define OSL_MAKEBYTE(nl, nh)    ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
168cdf0e10cSrcweir #endif
169cdf0e10cSrcweir #ifndef OSL_LONIBBLE
170cdf0e10cSrcweir #	define OSL_LONIBBLE(b)         ((sal_uInt8)((b) & 0x0F))
171cdf0e10cSrcweir #endif
172cdf0e10cSrcweir #ifndef OSL_HINIBBLE
173cdf0e10cSrcweir #	define OSL_HINIBBLE(b)         ((sal_uInt8)(((b) >> 4) & 0x0F))
174cdf0e10cSrcweir #endif
175cdf0e10cSrcweir 
176cdf0e10cSrcweir #ifndef OSL_MAKEWORD
177cdf0e10cSrcweir #	define OSL_MAKEWORD(bl, bh)    ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
178cdf0e10cSrcweir #endif
179cdf0e10cSrcweir #ifndef OSL_LOBYTE
180cdf0e10cSrcweir #	define OSL_LOBYTE(w)           ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
181cdf0e10cSrcweir #endif
182cdf0e10cSrcweir #ifndef OSL_HIBYTE
183cdf0e10cSrcweir #	define OSL_HIBYTE(w)           ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
184cdf0e10cSrcweir #endif
185cdf0e10cSrcweir 
186cdf0e10cSrcweir #ifndef OSL_MAKEDWORD
187cdf0e10cSrcweir #	define OSL_MAKEDWORD(wl, wh)   ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
188cdf0e10cSrcweir #endif
189cdf0e10cSrcweir #ifndef OSL_LOWORD
190cdf0e10cSrcweir #	define OSL_LOWORD(d)           ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
191cdf0e10cSrcweir #endif
192cdf0e10cSrcweir #ifndef OSL_HIWORD
193cdf0e10cSrcweir #	define OSL_HIWORD(d)           ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
194cdf0e10cSrcweir #endif
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir /** Define macros for swapping between host and network byte order.
198cdf0e10cSrcweir  */
199cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
200cdf0e10cSrcweir #ifndef OSL_NETWORD
201cdf0e10cSrcweir #	define OSL_NETWORD(w)          (sal_uInt16)(w)
202cdf0e10cSrcweir #endif
203cdf0e10cSrcweir #ifndef OSL_NETDWORD
204cdf0e10cSrcweir #	define OSL_NETDWORD(d)         (sal_uInt32)(d)
205cdf0e10cSrcweir #endif
206cdf0e10cSrcweir #else  /* OSL_LITENDIAN */
207cdf0e10cSrcweir #ifndef OSL_NETWORD
208cdf0e10cSrcweir #	define OSL_NETWORD(w)          OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
209cdf0e10cSrcweir #endif
210cdf0e10cSrcweir #ifndef OSL_NETDWORD
211cdf0e10cSrcweir #	define OSL_NETDWORD(d)         OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
212cdf0e10cSrcweir #endif
213cdf0e10cSrcweir #endif /* OSL_BIGENDIAN */
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir /** Define macros for swapping between byte orders.
217cdf0e10cSrcweir  */
218cdf0e10cSrcweir #ifndef OSL_SWAPWORD
219cdf0e10cSrcweir #	define OSL_SWAPWORD(w)         OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
220cdf0e10cSrcweir #endif
221cdf0e10cSrcweir #ifndef OSL_SWAPDWORD
222cdf0e10cSrcweir #	define OSL_SWAPDWORD(d)        OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
223cdf0e10cSrcweir #endif
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir #ifdef __cplusplus
227cdf0e10cSrcweir }
228cdf0e10cSrcweir #endif
229cdf0e10cSrcweir 
230cdf0e10cSrcweir #endif /*_OSL_ENDIAN_H_ */
231cdf0e10cSrcweir 
232