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/machine.h>
65 # if BYTE_ORDER == LITTLE_ENDIAN
66 # define OSL_LITENDIAN
67 # elif BYTE_ORDER == BIG_ENDIAN
68 # define OSL_BIGENDIAN
71 # include <sys/isa_defs.h>
72 # if defined _LITTLE_ENDIAN
73 # define OSL_LITENDIAN
74 # elif defined _BIG_ENDIAN
75 # define OSL_BIGENDIAN
77 #elif defined EMSCRIPTEN
78 # define OSL_LITENDIAN
80 # error "Target platform not specified !"
82 #if defined OSL_LITENDIAN == defined OSL_BIGENDIAN
83 # error undetermined endianness
87 /** Define macros for byte order manipulation.
90 # define OSL_MAKEBYTE(nl, nh) ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
93 # define OSL_LONIBBLE(b) ((sal_uInt8)((b) & 0x0F))
96 # define OSL_HINIBBLE(b) ((sal_uInt8)(((b) >> 4) & 0x0F))
100 # define OSL_MAKEWORD(bl, bh) ((sal_uInt16)((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8)))
103 # define OSL_LOBYTE(w) ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
106 # define OSL_HIBYTE(w) ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
109 #ifndef OSL_MAKEDWORD
110 # define OSL_MAKEDWORD(wl, wh) ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
113 # define OSL_LOWORD(d) ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
116 # define OSL_HIWORD(d) ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
120 /** Define macros for swapping between host and network byte order.
124 # define OSL_NETWORD(w) (sal_uInt16)(w)
127 # define OSL_NETDWORD(d) (sal_uInt32)(d)
129 #else /* OSL_LITENDIAN */
131 # define OSL_NETWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
134 # define OSL_NETDWORD(d) OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
136 #endif /* OSL_BIGENDIAN */
139 /** Define macros for swapping between byte orders.
142 # define OSL_SWAPWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
144 #ifndef OSL_SWAPDWORD
145 # define OSL_SWAPDWORD(d) OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
153 #endif // INCLUDED_OSL_ENDIAN_H
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */