1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: endian.h,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _OSL_ENDIAN_H_
32 #define _OSL_ENDIAN_H_
34 #include <sal/types.h>
40 /** Determine the platform byte order as _BIG_ENDIAN, _LITTLE_ENDIAN, ...
44 # define _LITTLE_ENDIAN
45 # elif defined(_M_MRX000)
46 # define _LITTLE_ENDIAN
47 # elif defined(_M_ALPHA)
48 # define _LITTLE_ENDIAN
49 # elif defined(_M_PPC)
50 # define _LITTLE_ENDIAN
56 # if __BYTE_ORDER == __LITTLE_ENDIAN
57 # ifndef _LITTLE_ENDIAN
58 # define _LITTLE_ENDIAN
60 # elif __BYTE_ORDER == __BIG_ENDIAN
64 # elif __BYTE_ORDER == __PDP_ENDIAN
70 # include <machine/endian.h>
71 # if BYTE_ORDER == LITTLE_ENDIAN
72 # define _LITTLE_ENDIAN
73 # elif BYTE_ORDER == BIG_ENDIAN
75 # elif BYTE_ORDER == PDP_ENDIAN
81 # include <sys/param.h>
82 # include <machine/endian.h>
83 #if __FreeBSD_version < 500000
84 # if BYTE_ORDER == LITTLE_ENDIAN
85 # define _LITTLE_ENDIAN
86 # elif BYTE_ORDER == BIG_ENDIAN
88 # elif BYTE_ORDER == PDP_ENDIAN
95 # include <sys/types.h>
96 # include <sys/byteorder.h>
97 # if BYTE_ORDER == LITTLE_ENDIAN
98 # define _LITTLE_ENDIAN
99 # elif BYTE_ORDER == BIG_ENDIAN
101 # elif BYTE_ORDER == PDP_ENDIAN
107 # include <sys/machine.h>
108 # if BYTE_ORDER == LITTLE_ENDIAN
109 # define _LITTLE_ENDIAN
110 # elif BYTE_ORDER == BIG_ENDIAN
112 # elif BYTE_ORDER == PDP_ENDIAN
118 # include <machine/param.h>
122 # include <sys/endian.h>
123 # if BYTE_ORDER == LITTLE_ENDIAN
126 # elif BYTE_ORDER == BIG_ENDIAN
127 # undef _LITTLE_ENDIAN
129 # elif BYTE_ORDER == PDP_ENDIAN
130 # undef _LITTLE_ENDIAN
136 # define _LITTLE_ENDIAN
140 # include <machine/endian.h>
144 # include <sys/isa_defs.h>
148 # include <machine/endian.h>
149 # if BYTE_ORDER == LITTLE_ENDIAN
150 # ifndef _LITTLE_ENDIAN
151 # define _LITTLE_ENDIAN
153 # elif BYTE_ORDER == BIG_ENDIAN
157 # elif BYTE_ORDER == PDP_ENDIAN
164 /** Check supported platform.
166 #if !defined(_WIN32) && !defined(_WIN16) && !defined(OS2) && \
167 !defined(LINUX) && !defined(NETBSD) && !defined(SCO) && \
168 !defined(AIX) && !defined(HPUX) && \
169 !defined(SOLARIS) && !defined(IRIX) && \
170 !defined(MACOSX) && !defined(FREEBSD)
171 # error "Target platform not specified !"
175 /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
177 #if defined _LITTLE_ENDIAN
178 # define OSL_LITENDIAN
179 #elif defined _BIG_ENDIAN
180 # define OSL_BIGENDIAN
182 # error undetermined endianess
186 /** Define macros for byte order manipulation.
189 # define OSL_MAKEBYTE(nl, nh) ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
192 # define OSL_LONIBBLE(b) ((sal_uInt8)((b) & 0x0F))
195 # define OSL_HINIBBLE(b) ((sal_uInt8)(((b) >> 4) & 0x0F))
199 # define OSL_MAKEWORD(bl, bh) ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
202 # define OSL_LOBYTE(w) ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
205 # define OSL_HIBYTE(w) ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
208 #ifndef OSL_MAKEDWORD
209 # define OSL_MAKEDWORD(wl, wh) ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
212 # define OSL_LOWORD(d) ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
215 # define OSL_HIWORD(d) ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
219 /** Define macros for swapping between host and network byte order.
223 # define OSL_NETWORD(w) (sal_uInt16)(w)
226 # define OSL_NETDWORD(d) (sal_uInt32)(d)
228 #else /* OSL_LITENDIAN */
230 # define OSL_NETWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
233 # define OSL_NETDWORD(d) OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
235 #endif /* OSL_BIGENDIAN */
238 /** Define macros for swapping between byte orders.
241 # define OSL_SWAPWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
243 #ifndef OSL_SWAPDWORD
244 # define OSL_SWAPDWORD(d) OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
252 #endif /*_OSL_ENDIAN_H_ */