xref: /trunk/main/sal/inc/osl/endian.h (revision 11ac63c8)
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_AMD64)
39 #		define _LITTLE_ENDIAN
40 #	elif defined(_M_MRX000)
41 #		define _LITTLE_ENDIAN
42 #	elif defined(_M_ALPHA)
43 #		define _LITTLE_ENDIAN
44 #	elif defined(_M_PPC)
45 #		define _LITTLE_ENDIAN
46 #	endif
47 #endif
48 
49 #ifdef LINUX
50 #	include <endian.h>
51 #	if __BYTE_ORDER == __LITTLE_ENDIAN
52 #           ifndef _LITTLE_ENDIAN
53 #		define _LITTLE_ENDIAN
54 #           endif
55 #	elif __BYTE_ORDER == __BIG_ENDIAN
56 #           ifndef _BIG_ENDIAN
57 #		define _BIG_ENDIAN
58 #           endif
59 #	elif __BYTE_ORDER == __PDP_ENDIAN
60 #		define _PDP_ENDIAN
61 #	endif
62 #endif
63 
64 #ifdef NETBSD
65 #   include <machine/endian.h>
66 #   if BYTE_ORDER == LITTLE_ENDIAN
67 #   	define _LITTLE_ENDIAN
68 #   elif BYTE_ORDER == BIG_ENDIAN
69 #   	define _BIG_ENDIAN
70 #   elif BYTE_ORDER == PDP_ENDIAN
71 #   	define _PDP_ENDIAN
72 #   endif
73 #endif
74 
75 #ifdef FREEBSD
76 #   include <sys/param.h>
77 #   include <machine/endian.h>
78 #   if BYTE_ORDER == LITTLE_ENDIAN
79 #   	undef _BIG_ENDIAN
80 #   elif BYTE_ORDER == BIG_ENDIAN
81 #   	undef _LITTLE_ENDIAN
82 #   elif BYTE_ORDER == PDP_ENDIAN
83 #   	define _PDP_ENDIAN
84 #   endif
85 #endif
86 
87 #ifdef SCO
88 #   include <sys/types.h>
89 #   include <sys/byteorder.h>
90 #   if BYTE_ORDER == LITTLE_ENDIAN
91 #   	define _LITTLE_ENDIAN
92 #   elif BYTE_ORDER == BIG_ENDIAN
93 #   	define _BIG_ENDIAN
94 #   elif BYTE_ORDER == PDP_ENDIAN
95 #   	define _PDP_ENDIAN
96 #   endif
97 #endif
98 
99 #ifdef AIX
100 #	include <sys/machine.h>
101 #   if BYTE_ORDER == LITTLE_ENDIAN
102 #   	define _LITTLE_ENDIAN
103 #   elif BYTE_ORDER == BIG_ENDIAN
104 #   	define _BIG_ENDIAN
105 #   elif BYTE_ORDER == PDP_ENDIAN
106 #   	define _PDP_ENDIAN
107 #   endif
108 #endif
109 
110 #ifdef HPUX
111 #	include <machine/param.h>
112 #endif
113 
114 #ifdef _WIN16
115 #	define	_LITTLE_ENDIAN
116 #endif
117 
118 #ifdef OS2
119 #	include <machine/endian.h>
120 #endif
121 
122 #ifdef SOLARIS
123 #	include <sys/isa_defs.h>
124 #endif
125 
126 #ifdef MACOSX
127 #	include <machine/endian.h>
128 #	if BYTE_ORDER == LITTLE_ENDIAN
129 #		ifndef _LITTLE_ENDIAN
130 #		define _LITTLE_ENDIAN
131 #		endif
132 #	elif BYTE_ORDER == BIG_ENDIAN
133 #		ifndef _BIG_ENDIAN
134 #		define _BIG_ENDIAN
135 #		endif
136 #	elif BYTE_ORDER == PDP_ENDIAN
137 #		ifndef _PDP_ENDIAN
138 #		define _PDP_ENDIAN
139 #		endif
140 #	endif
141 #endif
142 
143 /** Check supported platform.
144  */
145 #if !defined(_WIN32)  && !defined(_WIN16) && !defined(OS2)   && \
146     !defined(LINUX)   && !defined(NETBSD) && !defined(SCO)   && \
147 	!defined(AIX)     && !defined(HPUX)   && \
148 	!defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD)
149 #	error "Target platform not specified !"
150 #endif
151 
152 
153 /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN.
154  */
155 #if defined _LITTLE_ENDIAN
156 #	define OSL_LITENDIAN
157 #elif defined _BIG_ENDIAN
158 #	define OSL_BIGENDIAN
159 #else
160 #	error undetermined endianness
161 #endif
162 
163 
164 /** Define macros for byte order manipulation.
165  */
166 #ifndef OSL_MAKEBYTE
167 #	define OSL_MAKEBYTE(nl, nh)    ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4)))
168 #endif
169 #ifndef OSL_LONIBBLE
170 #	define OSL_LONIBBLE(b)         ((sal_uInt8)((b) & 0x0F))
171 #endif
172 #ifndef OSL_HINIBBLE
173 #	define OSL_HINIBBLE(b)         ((sal_uInt8)(((b) >> 4) & 0x0F))
174 #endif
175 
176 #ifndef OSL_MAKEWORD
177 #	define OSL_MAKEWORD(bl, bh)    ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8))
178 #endif
179 #ifndef OSL_LOBYTE
180 #	define OSL_LOBYTE(w)           ((sal_uInt8)((sal_uInt16)(w) & 0xFF))
181 #endif
182 #ifndef OSL_HIBYTE
183 #	define OSL_HIBYTE(w)           ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF))
184 #endif
185 
186 #ifndef OSL_MAKEDWORD
187 #	define OSL_MAKEDWORD(wl, wh)   ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16))
188 #endif
189 #ifndef OSL_LOWORD
190 #	define OSL_LOWORD(d)           ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF))
191 #endif
192 #ifndef OSL_HIWORD
193 #	define OSL_HIWORD(d)           ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF))
194 #endif
195 
196 
197 /** Define macros for swapping between host and network byte order.
198  */
199 #ifdef OSL_BIGENDIAN
200 #ifndef OSL_NETWORD
201 #	define OSL_NETWORD(w)          (sal_uInt16)(w)
202 #endif
203 #ifndef OSL_NETDWORD
204 #	define OSL_NETDWORD(d)         (sal_uInt32)(d)
205 #endif
206 #else  /* OSL_LITENDIAN */
207 #ifndef OSL_NETWORD
208 #	define OSL_NETWORD(w)          OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
209 #endif
210 #ifndef OSL_NETDWORD
211 #	define OSL_NETDWORD(d)         OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d)))
212 #endif
213 #endif /* OSL_BIGENDIAN */
214 
215 
216 /** Define macros for swapping between byte orders.
217  */
218 #ifndef OSL_SWAPWORD
219 #	define OSL_SWAPWORD(w)         OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w))
220 #endif
221 #ifndef OSL_SWAPDWORD
222 #	define OSL_SWAPDWORD(d)        OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d)))
223 #endif
224 
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif /*_OSL_ENDIAN_H_ */
231 
232