1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_OSL_ENDIAN_H
21 #define INCLUDED_OSL_ENDIAN_H
23 #include <sal/types.h>
29 /** Determine the platform byte order as _BIG_ENDIAN, _LITTLE_ENDIAN, ...
33 # define _LITTLE_ENDIAN
34 # elif defined(_M_AMD64)
35 # define _LITTLE_ENDIAN
36 # elif defined(_M_MRX000)
37 # define _LITTLE_ENDIAN
38 # elif defined(_M_ALPHA)
39 # define _LITTLE_ENDIAN
40 # elif defined(_M_PPC)
41 # define _LITTLE_ENDIAN
47 # if __BYTE_ORDER == __LITTLE_ENDIAN
48 # ifndef _LITTLE_ENDIAN
49 # define _LITTLE_ENDIAN
51 # elif __BYTE_ORDER == __BIG_ENDIAN
60 # if __BYTE_ORDER == __LITTLE_ENDIAN
61 # ifndef _LITTLE_ENDIAN
62 # define _LITTLE_ENDIAN
64 # elif __BYTE_ORDER == __BIG_ENDIAN
72 # include <machine/endian.h>
73 # if BYTE_ORDER == LITTLE_ENDIAN
75 # elif BYTE_ORDER == BIG_ENDIAN
76 # undef _LITTLE_ENDIAN
81 # include <sys/param.h>
82 # include <machine/endian.h>
86 # include <sys/machine.h>
87 # if BYTE_ORDER == LITTLE_ENDIAN
88 # ifndef _LITTLE_ENDIAN
89 # define _LITTLE_ENDIAN
91 # elif BYTE_ORDER == BIG_ENDIAN
99 # include <sys/isa_defs.h>
103 # include <machine/endian.h>
104 # if BYTE_ORDER == LITTLE_ENDIAN
105 # ifndef _LITTLE_ENDIAN
106 # define _LITTLE_ENDIAN
108 # elif BYTE_ORDER == BIG_ENDIAN
116 # include <machine/endian.h>
117 # if BYTE_ORDER == LITTLE_ENDIAN
118 # ifndef _LITTLE_ENDIAN
119 # define _LITTLE_ENDIAN
121 # elif BYTE_ORDER == BIG_ENDIAN
128 /** Check supported platform.
130 #if !defined(_WIN32) && \
131 !defined(LINUX) && !defined(NETBSD) && \
132 !defined(AIX) && !defined(OPENBSD) && \
133 !defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD) && \
134 !defined(DRAGONFLY) && \
135 !defined(IOS) && !defined(ANDROID)
136 # error "Target platform not specified !"
140 /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
142 #if defined _LITTLE_ENDIAN
143 # define OSL_LITENDIAN
144 #elif defined _BIG_ENDIAN
145 # define OSL_BIGENDIAN
147 # error undetermined endianness
151 /** Define macros for byte order manipulation.
154 # define OSL_MAKEBYTE(nl, nh) ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
157 # define OSL_LONIBBLE(b) ((sal_uInt8)((b) & 0x0F))
160 # define OSL_HINIBBLE(b) ((sal_uInt8)(((b) >> 4) & 0x0F))
164 # define OSL_MAKEWORD(bl, bh) ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
167 # define OSL_LOBYTE(w) ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
170 # define OSL_HIBYTE(w) ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
173 #ifndef OSL_MAKEDWORD
174 # define OSL_MAKEDWORD(wl, wh) ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
177 # define OSL_LOWORD(d) ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
180 # define OSL_HIWORD(d) ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
184 /** Define macros for swapping between host and network byte order.
188 # define OSL_NETWORD(w) (sal_uInt16)(w)
191 # define OSL_NETDWORD(d) (sal_uInt32)(d)
193 #else /* OSL_LITENDIAN */
195 # define OSL_NETWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
198 # define OSL_NETDWORD(d) OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
200 #endif /* OSL_BIGENDIAN */
203 /** Define macros for swapping between byte orders.
206 # define OSL_SWAPWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
208 #ifndef OSL_SWAPDWORD
209 # define OSL_SWAPDWORD(d) OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
217 #endif // INCLUDED_OSL_ENDIAN_H
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */