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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_ENDIAN_H
25 #define INCLUDED_OSL_ENDIAN_H
27 #include "sal/types.h"
33 /** Define the platform byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
37 # if defined _M_ALPHA || defined _M_AMD64 || defined _M_IX86 \
38 || defined _M_MRX000 || defined _M_PPC || defined _M_ARM64
39 # define OSL_LITENDIAN
41 #elif defined ANDROID || defined LINUX || defined HAIKU
43 # if __BYTE_ORDER == __LITTLE_ENDIAN
44 # define OSL_LITENDIAN
45 # elif __BYTE_ORDER == __BIG_ENDIAN
46 # define OSL_BIGENDIAN
48 #elif defined IOS || defined MACOSX || defined NETBSD
49 # include <machine/endian.h>
50 # if BYTE_ORDER == LITTLE_ENDIAN
51 # define OSL_LITENDIAN
52 # elif BYTE_ORDER == BIG_ENDIAN
53 # define OSL_BIGENDIAN
56 # include <sys/param.h>
57 # include <machine/endian.h>
58 # if defined _LITTLE_ENDIAN
59 # define OSL_LITENDIAN
60 # elif defined _BIG_ENDIAN
61 # define OSL_BIGENDIAN
64 # include <sys/isa_defs.h>
65 # if defined _LITTLE_ENDIAN
66 # define OSL_LITENDIAN
67 # elif defined _BIG_ENDIAN
68 # define OSL_BIGENDIAN
70 #elif defined EMSCRIPTEN
71 # define OSL_LITENDIAN
73 # error "Target platform not specified !"
75 #if defined OSL_LITENDIAN == defined OSL_BIGENDIAN
76 # error undetermined endianness
80 /** Define macros for byte order manipulation.
83 # define OSL_MAKEBYTE(nl, nh) ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
86 # define OSL_LONIBBLE(b) ((sal_uInt8)((b) & 0x0F))
89 # define OSL_HINIBBLE(b) ((sal_uInt8)(((b) >> 4) & 0x0F))
93 # define OSL_MAKEWORD(bl, bh) ((sal_uInt16)((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8)))
96 # define OSL_LOBYTE(w) ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
99 # define OSL_HIBYTE(w) ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
102 #ifndef OSL_MAKEDWORD
103 # define OSL_MAKEDWORD(wl, wh) ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
106 # define OSL_LOWORD(d) ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
109 # define OSL_HIWORD(d) ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
113 /** Define macros for swapping between host and network byte order.
117 # define OSL_NETWORD(w) (sal_uInt16)(w)
120 # define OSL_NETDWORD(d) (sal_uInt32)(d)
122 #else /* OSL_LITENDIAN */
124 # define OSL_NETWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
127 # define OSL_NETDWORD(d) OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
129 #endif /* OSL_BIGENDIAN */
132 /** Define macros for swapping between byte orders.
135 # define OSL_SWAPWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
137 #ifndef OSL_SWAPDWORD
138 # define OSL_SWAPDWORD(d) OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
146 #endif // INCLUDED_OSL_ENDIAN_H
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */