xref: /aoo42x/main/sal/inc/osl/endian.h (revision e15a4534)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _OSL_ENDIAN_H_
25 #define _OSL_ENDIAN_H_
26 
27 #include <sal/types.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /** Determine the platform byte order as _BIG_ENDIAN, _LITTLE_ENDIAN, ...
34  */
35 #ifdef _WIN32
36 #	if defined(_M_IX86)
37 #		define _LITTLE_ENDIAN
38 #	elif defined(_M_MRX000)
39 #		define _LITTLE_ENDIAN
40 #	elif defined(_M_ALPHA)
41 #		define _LITTLE_ENDIAN
42 #	elif defined(_M_PPC)
43 #		define _LITTLE_ENDIAN
44 #	endif
45 #endif
46 
47 #ifdef LINUX
48 #	include <endian.h>
49 #	if __BYTE_ORDER == __LITTLE_ENDIAN
50 #           ifndef _LITTLE_ENDIAN
51 #		define _LITTLE_ENDIAN
52 #           endif
53 #	elif __BYTE_ORDER == __BIG_ENDIAN
54 #           ifndef _BIG_ENDIAN
55 #		define _BIG_ENDIAN
56 #           endif
57 #	elif __BYTE_ORDER == __PDP_ENDIAN
58 #		define _PDP_ENDIAN
59 #	endif
60 #endif
61 
62 #ifdef NETBSD
63 #   include <machine/endian.h>
64 #   if BYTE_ORDER == LITTLE_ENDIAN
65 #   	define _LITTLE_ENDIAN
66 #   elif BYTE_ORDER == BIG_ENDIAN
67 #   	define _BIG_ENDIAN
68 #   elif BYTE_ORDER == PDP_ENDIAN
69 #   	define _PDP_ENDIAN
70 #   endif
71 #endif
72 
73 #ifdef FREEBSD
74 #   include <sys/param.h>
75 #   include <machine/endian.h>
76 #   if BYTE_ORDER == LITTLE_ENDIAN
77 #   	undef _BIG_ENDIAN
78 #   elif BYTE_ORDER == BIG_ENDIAN
79 #   	undef _LITTLE_ENDIAN
80 #   elif BYTE_ORDER == PDP_ENDIAN
81 #   	define _PDP_ENDIAN
82 #   endif
83 #endif
84 
85 #ifdef SCO
86 #   include <sys/types.h>
87 #   include <sys/byteorder.h>
88 #   if BYTE_ORDER == LITTLE_ENDIAN
89 #   	define _LITTLE_ENDIAN
90 #   elif BYTE_ORDER == BIG_ENDIAN
91 #   	define _BIG_ENDIAN
92 #   elif BYTE_ORDER == PDP_ENDIAN
93 #   	define _PDP_ENDIAN
94 #   endif
95 #endif
96 
97 #ifdef AIX
98 #	include <sys/machine.h>
99 #   if BYTE_ORDER == LITTLE_ENDIAN
100 #   	define _LITTLE_ENDIAN
101 #   elif BYTE_ORDER == BIG_ENDIAN
102 #   	define _BIG_ENDIAN
103 #   elif BYTE_ORDER == PDP_ENDIAN
104 #   	define _PDP_ENDIAN
105 #   endif
106 #endif
107 
108 #ifdef HPUX
109 #	include <machine/param.h>
110 #endif
111 
112 #ifdef _WIN16
113 #	define	_LITTLE_ENDIAN
114 #endif
115 
116 #ifdef OS2
117 #	include <machine/endian.h>
118 #endif
119 
120 #ifdef SOLARIS
121 #	include <sys/isa_defs.h>
122 #endif
123 
124 #ifdef MACOSX
125 #	include <machine/endian.h>
126 #	if BYTE_ORDER == LITTLE_ENDIAN
127 #		ifndef _LITTLE_ENDIAN
128 #		define _LITTLE_ENDIAN
129 #		endif
130 #	elif BYTE_ORDER == BIG_ENDIAN
131 #		ifndef _BIG_ENDIAN
132 #		define _BIG_ENDIAN
133 #		endif
134 #	elif BYTE_ORDER == PDP_ENDIAN
135 #		ifndef _PDP_ENDIAN
136 #		define _PDP_ENDIAN
137 #		endif
138 #	endif
139 #endif
140 
141 /** Check supported platform.
142  */
143 #if !defined(_WIN32)  && !defined(_WIN16) && !defined(OS2)   && \
144     !defined(LINUX)   && !defined(NETBSD) && !defined(SCO)   && \
145 	!defined(AIX)     && !defined(HPUX)   && \
146 	!defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD)
147 #	error "Target platform not specified !"
148 #endif
149 
150 
151 /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
152  */
153 #if defined _LITTLE_ENDIAN
154 #	define OSL_LITENDIAN
155 #elif defined _BIG_ENDIAN
156 #	define OSL_BIGENDIAN
157 #else
158 #	error undetermined endianness
159 #endif
160 
161 
162 /** Define macros for byte order manipulation.
163  */
164 #ifndef OSL_MAKEBYTE
165 #	define OSL_MAKEBYTE(nl, nh)    ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
166 #endif
167 #ifndef OSL_LONIBBLE
168 #	define OSL_LONIBBLE(b)         ((sal_uInt8)((b) & 0x0F))
169 #endif
170 #ifndef OSL_HINIBBLE
171 #	define OSL_HINIBBLE(b)         ((sal_uInt8)(((b) >> 4) & 0x0F))
172 #endif
173 
174 #ifndef OSL_MAKEWORD
175 #	define OSL_MAKEWORD(bl, bh)    ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
176 #endif
177 #ifndef OSL_LOBYTE
178 #	define OSL_LOBYTE(w)           ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
179 #endif
180 #ifndef OSL_HIBYTE
181 #	define OSL_HIBYTE(w)           ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
182 #endif
183 
184 #ifndef OSL_MAKEDWORD
185 #	define OSL_MAKEDWORD(wl, wh)   ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
186 #endif
187 #ifndef OSL_LOWORD
188 #	define OSL_LOWORD(d)           ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
189 #endif
190 #ifndef OSL_HIWORD
191 #	define OSL_HIWORD(d)           ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
192 #endif
193 
194 
195 /** Define macros for swapping between host and network byte order.
196  */
197 #ifdef OSL_BIGENDIAN
198 #ifndef OSL_NETWORD
199 #	define OSL_NETWORD(w)          (sal_uInt16)(w)
200 #endif
201 #ifndef OSL_NETDWORD
202 #	define OSL_NETDWORD(d)         (sal_uInt32)(d)
203 #endif
204 #else  /* OSL_LITENDIAN */
205 #ifndef OSL_NETWORD
206 #	define OSL_NETWORD(w)          OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
207 #endif
208 #ifndef OSL_NETDWORD
209 #	define OSL_NETDWORD(d)         OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
210 #endif
211 #endif /* OSL_BIGENDIAN */
212 
213 
214 /** Define macros for swapping between byte orders.
215  */
216 #ifndef OSL_SWAPWORD
217 #	define OSL_SWAPWORD(w)         OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
218 #endif
219 #ifndef OSL_SWAPDWORD
220 #	define OSL_SWAPDWORD(d)        OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
221 #endif
222 
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #endif /*_OSL_ENDIAN_H_ */
229 
230