2 * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * provides byteswapping utilities. Basic functions are obtained from
39 * platform specific implementations from byteswap_osd.h.
42 #ifndef _CL_BYTESWAP_H_
43 #define _CL_BYTESWAP_H_
46 #include <complib/cl_byteswap_osd.h>
49 # define BEGIN_C_DECLS extern "C" {
50 # define END_C_DECLS }
51 #else /* !__cplusplus */
52 # define BEGIN_C_DECLS
54 #endif /* __cplusplus */
57 /****h* Component Library/Byte Swapping
62 * The byte swapping functions and macros allow swapping bytes from network
63 * byte order to host byte order.
65 * All data transmitted between systems should be in network byte order.
66 * In order to utilize such data, it must be converted to host byte order
71 * cl_ntoh16, cl_hton16, cl_ntoh32, cl_hton32, cl_ntoh64, cl_hton64,
75 * CL_NTOH16, CL_HTON16, CL_NTOH32, CL_HTON32, CL_NTOH64, CL_HTON64
78 * The byteswap_osd.h provides the following macros.
83 * If the platform provides byte swapping functions, byteswap_osd.h also
84 * provides the following macros.
91 #error "__BYTE_ORDER macro undefined. Missing in endian.h?"
93 #if __BYTE_ORDER == __LITTLE_ENDIAN
100 /****d* Component Library: Byte Swapping/CL_NTOH16
105 * The CL_NTOH16 macro converts a 16-bit value from network byte order to
106 * host byte order. The CL_NTOH16 macro will cause constant values to be
107 * swapped by the pre-processor. For variables, CL_NTOH16 is less efficient
108 * than the cl_ntoh16 function.
115 * [in] 16-bit value to swap from network byte order to host byte order.
118 * Value of val converted to host byte order.
121 * This macro is analogous to CL_HTON16.
124 * Byte Swapping, CL_HTON16, CL_NTOH32, CL_NTOH64,
125 * cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
127 /****d* Component Library: Byte Swapping/CL_HTON16
132 * The CL_HTON16 macro converts a 16-bit value from host byte order to
133 * network byte order. The CL_HTON16 macro will cause constant values to be
134 * swapped by the pre-processor. For variables, CL_HTON16 is less efficient
135 * than the cl_hton16 function.
142 * [in] 16-bit value to swap from host byte order to network byte order.
145 * Value of val converted to network byte order.
148 * This macro is analogous to CL_NTOH16.
151 * Byte Swapping, CL_NTOH16, CL_HTON32, CL_HTON64,
152 * cl_hton16, cl_hton32, cl_hton64, cl_ntoh
155 #define CL_NTOH16( x ) (uint16_t)( \
156 (((uint16_t)(x) & 0x00FF) << 8) | \
157 (((uint16_t)(x) & 0xFF00) >> 8) )
159 #define CL_NTOH16( x ) (x)
161 #define CL_HTON16 CL_NTOH16
162 /****f* Component Library: Byte Swapping/cl_ntoh16
167 * The cl_ntoh16 function converts a 16-bit value from network byte order to
173 * IN const uint16_t val );
177 * [in] Value to swap from network byte order to host byte order.
180 * Value of val converted to host byte order.
183 * This function is analogous to cl_hton16.
186 * Byte Swapping, cl_hton16, cl_ntoh32, cl_ntoh64, cl_ntoh
188 /****f* Component Library: Byte Swapping/cl_hton16
193 * The cl_hton16 function converts a 16-bit value from host byte order to
194 * network byte order.
199 * IN const uint16_t val );
203 * [in] Value to swap from host byte order to network byte order .
206 * Value of val converted to network byte order.
209 * This function is analogous to cl_ntoh16.
212 * Byte Swapping, cl_ntoh16, cl_hton32, cl_hton64, cl_ntoh
215 #define cl_ntoh16 CL_NTOH16
216 #define cl_hton16 CL_HTON16
218 /****d* Component Library: Byte Swapping/CL_NTOH32
223 * The CL_NTOH32 macro converts a 32-bit value from network byte order to
224 * host byte order. The CL_NTOH32 macro will cause constant values to be
225 * swapped by the pre-processor. For variables, CL_NTOH32 is less efficient
226 * than the cl_ntoh32 function.
233 * [in] 32-bit value to swap from network byte order to host byte order.
236 * Value of val converted to host byte order.
239 * This macro is analogous to CL_HTON32.
242 * Byte Swapping, CL_HTON32, CL_NTOH16, CL_NTOH64,
243 * cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
245 /****d* Component Library: Byte Swapping/CL_HTON32
250 * The CL_HTON32 macro converts a 32-bit value from host byte order to
251 * network byte order. The CL_HTON32 macro will cause constant values to be
252 * swapped by the pre-processor. For variables, CL_HTON32 is less efficient
253 * than the cl_hton32 function.
260 * [in] 32-bit value to swap from host byte order to network byte order.
263 * Value of val converted to network byte order.
266 * This macro is analogous to CL_NTOH32.
269 * Byte Swapping, CL_NTOH32, CL_HTON16, CL_HTON64,
270 * cl_hton16, cl_hton32, cl_hton64, cl_ntoh
273 #define CL_NTOH32( x ) (uint32_t)( \
274 (((uint32_t)(x) & 0x000000FF) << 24) | \
275 (((uint32_t)(x) & 0x0000FF00) << 8) | \
276 (((uint32_t)(x) & 0x00FF0000) >> 8) | \
277 (((uint32_t)(x) & 0xFF000000) >> 24) )
279 #define CL_NTOH32( x ) (x)
281 #define CL_HTON32 CL_NTOH32
282 /****f* Component Library: Byte Swapping/cl_ntoh32
287 * The cl_ntoh32 function converts a 32-bit value from network byte order to
293 * IN const uint32_t val );
297 * [in] Value to swap from network byte order to host byte order.
300 * Value of val converted in host byte order.
303 * This function is analogous to cl_hton32.
306 * Byte Swapping, cl_hton32, cl_ntoh16, cl_ntoh64, cl_ntoh
308 /****f* Component Library: Byte Swapping/cl_hton32
313 * The cl_hton32 function converts a 32-bit value from host byte order to
314 * network byte order.
319 * IN const uint32_t val );
323 * [in] Value to swap from host byte order to network byte order .
326 * Value of val converted to network byte order.
329 * This function is analogous to cl_ntoh32.
332 * Byte Swapping, cl_ntoh32, cl_hton16, cl_hton64, cl_ntoh
335 #define cl_ntoh32 CL_NTOH32
336 #define cl_hton32 CL_HTON32
338 /****d* Component Library: Byte Swapping/CL_NTOH64
343 * The CL_NTOH64 macro converts a 64-bit value from network byte order to
344 * host byte order. The CL_NTOH64 macro will cause constant values to be
345 * swapped by the pre-processor. For variables, CL_NTOH64 is less efficient
346 * than the cl_ntoh64 function.
353 * [in] 64-bit value to swap from network byte order to host byte order.
356 * Value of val converted to host byte order.
359 * This macro is analogous to CL_HTON64.
362 * Byte Swapping, CL_HTON64, CL_NTOH16, CL_NTOH32,
363 * cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
365 /****d* Component Library: Byte Swapping/CL_HTON64
370 * The CL_HTON64 macro converts a 64-bit value from host byte order to
371 * network byte order. The CL_HTON64 macro will cause constant values to be
372 * swapped by the pre-processor. For variables, CL_HTON64 is less efficient
373 * than the cl_hton64 function.
380 * [in] 64-bit value to swap from host byte order to network byte order.
383 * Value of val converted to network byte order.
386 * This macro is analogous to CL_NTOH64.
389 * Byte Swapping, CL_NTOH64, CL_HTON16, CL_HTON32,
390 * cl_hton16, cl_hton32, cl_hton64, cl_ntoh
393 #define CL_NTOH64( x ) (uint64_t)( \
394 (((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
395 (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
396 (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
397 (((uint64_t)(x) & 0x00000000FF000000ULL) << 8 ) | \
398 (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8 ) | \
399 (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
400 (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
401 (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56) )
403 #define CL_NTOH64( x ) (x)
405 #define CL_HTON64 CL_NTOH64
406 /****f* Component Library: Byte Swapping/cl_ntoh64
411 * The cl_ntoh64 function converts a 64-bit value from network byte order to
417 * IN const uint64_t val );
421 * [in] Value to swap from network byte order to host byte order.
424 * Value of val converted in host byte order.
427 * This function is analogous to cl_hton64.
430 * Byte Swapping, cl_hton64, cl_ntoh16, cl_ntoh32, cl_ntoh
432 /****f* Component Library: Byte Swapping/cl_hton64
437 * The cl_hton64 function converts a 64-bit value from host byte order to
438 * network byte order.
443 * IN const uint64_t val );
447 * [in] Value to swap from host byte order to network byte order .
450 * Value of val converted to network byte order.
453 * This function is analogous to cl_ntoh64.
456 * Byte Swapping, cl_ntoh64, cl_hton16, cl_hton32, cl_ntoh
459 #define cl_ntoh64 CL_NTOH64
460 #define cl_hton64 CL_HTON64
462 /****f* Component Library: Byte Swapping/cl_ntoh
467 * The cl_ntoh function converts a value from network byte order to
473 cl_ntoh(OUT char *const p_dest,
474 IN const char *const p_src, IN const uint8_t size)
480 if (p_src == p_dest) {
481 /* Swap in place if source and destination are the same. */
482 for (i = 0; i < size / 2; i++) {
484 p_dest[i] = p_src[size - 1 - i];
485 p_dest[size - 1 - i] = temp;
488 for (i = 0; i < size; i++)
489 p_dest[i] = p_src[size - 1 - i];
493 * If the source and destination are not the same, copy the source to
497 memcpy(p_dest, p_src, size);
504 * [in] Pointer to a byte array to contain the converted value of p_src.
507 * [in] Pointer to a byte array to be converted from network byte
511 * [in] Number of bytes to swap.p_dest
514 * This function does not return a value.
517 * cl_ntoh can perform in place swapping if both p_src and p_dest point to
521 * Byte Swapping, cl_ntoh16, cl_ntoh32, cl_ntoh64
525 #endif /* _CL_BYTESWAP_H_ */