cl_byteswap.h revision 219820
144743Smarkm/*
244743Smarkm * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
344743Smarkm * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
444743Smarkm * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
563158Sume *
663158Sume * This software is available to you under a choice of one of two
744743Smarkm * licenses.  You may choose to be licensed under the terms of the GNU
844743Smarkm * General Public License (GPL) Version 2, available from the file
963158Sume * COPYING in the main directory of this source tree, or the
1063158Sume * OpenIB.org BSD license below:
1163158Sume *
1244743Smarkm *     Redistribution and use in source and binary forms, with or
1363158Sume *     without modification, are permitted provided that the following
1444743Smarkm *     conditions are met:
1544743Smarkm *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
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.
24 *
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
32 * SOFTWARE.
33 *
34 */
35
36/*
37 * Abstract:
38 *	provides byteswapping utilities. Basic functions are obtained from
39 *  platform specific implementations from byteswap_osd.h.
40 */
41
42#ifndef _CL_BYTESWAP_H_
43#define _CL_BYTESWAP_H_
44
45#include <string.h>
46#include <complib/cl_byteswap_osd.h>
47
48#ifdef __cplusplus
49#  define BEGIN_C_DECLS extern "C" {
50#  define END_C_DECLS   }
51#else				/* !__cplusplus */
52#  define BEGIN_C_DECLS
53#  define END_C_DECLS
54#endif				/* __cplusplus */
55
56BEGIN_C_DECLS
57/****h* Component Library/Byte Swapping
58* NAME
59*	Byte Swapping
60*
61* DESCRIPTION
62*	The byte swapping functions and macros allow swapping bytes from network
63*	byte order to host byte order.
64*
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
67*	before use.
68*
69* SEE ALSO
70*	Functions:
71*		cl_ntoh16, cl_hton16, cl_ntoh32, cl_hton32, cl_ntoh64, cl_hton64,
72*		cl_ntoh
73*
74*	Macros:
75*		CL_NTOH16, CL_HTON16, CL_NTOH32, CL_HTON32, CL_NTOH64, CL_HTON64
76*********/
77/*
78 * The byteswap_osd.h provides the following macros.
79 *		__LITTLE_ENDIAN
80 *		__BIG_ENDIAN
81 *		__BYTE_ORDER
82 *
83 * If the platform provides byte swapping functions, byteswap_osd.h also
84 * provides the following macros.
85 *		ntoh16, hton16
86 *		ntoh32, hton32
87 *		ntoh64, hton64
88 */
89
90#ifndef __BYTE_ORDER
91#error "__BYTE_ORDER macro undefined. Missing in endian.h?"
92#endif
93#if __BYTE_ORDER == __LITTLE_ENDIAN
94#define CPU_LE		1
95#define CPU_BE		0
96#else
97#define CPU_LE		0
98#define CPU_BE		1
99#endif
100/****d* Component Library: Byte Swapping/CL_NTOH16
101* NAME
102*	CL_NTOH16
103*
104* DESCRIPTION
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.
109*
110* SYNOPSIS
111*	CL_NTOH16( val );
112*
113* PARAMETERS
114*	val
115*		[in] 16-bit value to swap from network byte order to host byte order.
116*
117* RESULT
118*	Value of val converted to host byte order.
119*
120* NOTES
121*	This macro is analogous to CL_HTON16.
122*
123* SEE ALSO
124*	Byte Swapping, CL_HTON16, CL_NTOH32, CL_NTOH64,
125*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
126*********/
127/****d* Component Library: Byte Swapping/CL_HTON16
128* NAME
129*	CL_HTON16
130*
131* DESCRIPTION
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.
136*
137* SYNOPSIS
138*	CL_HTON16( val );
139*
140* PARAMETERS
141*	val
142*		[in] 16-bit value to swap from host byte order to network byte order.
143*
144* RESULT
145*	Value of val converted to network byte order.
146*
147* NOTES
148*	This macro is analogous to CL_NTOH16.
149*
150* SEE ALSO
151*	Byte Swapping, CL_NTOH16, CL_HTON32, CL_HTON64,
152*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
153*********/
154#if CPU_LE
155#define CL_NTOH16( x )		(uint16_t)(		\
156			(((uint16_t)(x) & 0x00FF) << 8) |		\
157			(((uint16_t)(x) & 0xFF00) >> 8) )
158#else
159#define CL_NTOH16( x )	(x)
160#endif
161#define CL_HTON16				CL_NTOH16
162/****f* Component Library: Byte Swapping/cl_ntoh16
163* NAME
164*	cl_ntoh16
165*
166* DESCRIPTION
167*	The cl_ntoh16 function converts a 16-bit value from network byte order to
168*	host byte order.
169*
170* SYNOPSIS
171*	uint16_t
172*	cl_ntoh16(
173*		IN	const uint16_t	val );
174*
175* PARAMETERS
176*	val
177*		[in] Value to swap from network byte order to host byte order.
178*
179* RETURN VALUE
180*	Value of val converted to host byte order.
181*
182* NOTES
183*	This function is analogous to cl_hton16.
184*
185* SEE ALSO
186*	Byte Swapping, cl_hton16, cl_ntoh32, cl_ntoh64, cl_ntoh
187*********/
188/****f* Component Library: Byte Swapping/cl_hton16
189* NAME
190*	cl_hton16
191*
192* DESCRIPTION
193*	The cl_hton16 function converts a 16-bit value from host byte order to
194*	network byte order.
195*
196* SYNOPSIS
197*	uint16_t
198*	cl_hton16(
199*		IN	const uint16_t	val );
200*
201* PARAMETERS
202*	val
203*		[in] Value to swap from host byte order to network byte order .
204*
205* RETURN VALUE
206*	Value of val converted to network byte order.
207*
208* NOTES
209*	This function is analogous to cl_ntoh16.
210*
211* SEE ALSO
212*	Byte Swapping, cl_ntoh16, cl_hton32, cl_hton64, cl_ntoh
213*********/
214#ifndef cl_ntoh16
215#define cl_ntoh16	CL_NTOH16
216#define cl_hton16	CL_HTON16
217#endif
218/****d* Component Library: Byte Swapping/CL_NTOH32
219* NAME
220*	CL_NTOH32
221*
222* DESCRIPTION
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.
227*
228* SYNOPSIS
229*	CL_NTOH32( val );
230*
231* PARAMETERS
232*	val
233*		[in] 32-bit value to swap from network byte order to host byte order.
234*
235* RESULT
236*	Value of val converted to host byte order.
237*
238* NOTES
239*	This macro is analogous to CL_HTON32.
240*
241* SEE ALSO
242*	Byte Swapping, CL_HTON32, CL_NTOH16, CL_NTOH64,
243*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
244*********/
245/****d* Component Library: Byte Swapping/CL_HTON32
246* NAME
247*	CL_HTON32
248*
249* DESCRIPTION
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.
254*
255* SYNOPSIS
256*	CL_HTON32( val );
257*
258* PARAMETERS
259*	val
260*		[in] 32-bit value to swap from host byte order to network byte order.
261*
262* RESULT
263*	Value of val converted to network byte order.
264*
265* NOTES
266*	This macro is analogous to CL_NTOH32.
267*
268* SEE ALSO
269*	Byte Swapping, CL_NTOH32, CL_HTON16, CL_HTON64,
270*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
271*********/
272#if CPU_LE
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) )
278#else
279#define CL_NTOH32( x )		(x)
280#endif
281#define CL_HTON32	CL_NTOH32
282/****f* Component Library: Byte Swapping/cl_ntoh32
283* NAME
284*	cl_ntoh32
285*
286* DESCRIPTION
287*	The cl_ntoh32 function converts a 32-bit value from network byte order to
288*	host byte order.
289*
290* SYNOPSIS
291*	uint32_t
292*	cl_ntoh32(
293*		IN	const uint32_t	val );
294*
295* PARAMETERS
296*	val
297*		[in] Value to swap from network byte order to host byte order.
298*
299* RETURN VALUE
300*	Value of val converted in host byte order.
301*
302* NOTES
303*	This function is analogous to cl_hton32.
304*
305* SEE ALSO
306*	Byte Swapping, cl_hton32, cl_ntoh16, cl_ntoh64, cl_ntoh
307*********/
308/****f* Component Library: Byte Swapping/cl_hton32
309* NAME
310*	cl_hton32
311*
312* DESCRIPTION
313*	The cl_hton32 function converts a 32-bit value from host byte order to
314*	network byte order.
315*
316* SYNOPSIS
317*	uint32_t
318*	cl_hton32(
319*		IN	const uint32_t	val );
320*
321* PARAMETERS
322*	val
323*		[in] Value to swap from host byte order to network byte order .
324*
325* RETURN VALUE
326*	Value of val converted to network byte order.
327*
328* NOTES
329*	This function is analogous to cl_ntoh32.
330*
331* SEE ALSO
332*	Byte Swapping, cl_ntoh32, cl_hton16, cl_hton64, cl_ntoh
333*********/
334#ifndef cl_ntoh32
335#define cl_ntoh32	CL_NTOH32
336#define cl_hton32	CL_HTON32
337#endif
338/****d* Component Library: Byte Swapping/CL_NTOH64
339* NAME
340*	CL_NTOH64
341*
342* DESCRIPTION
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.
347*
348* SYNOPSIS
349*	CL_NTOH64( val );
350*
351* PARAMETERS
352*	val
353*		[in] 64-bit value to swap from network byte order to host byte order.
354*
355* RESULT
356*	Value of val converted to host byte order.
357*
358* NOTES
359*	This macro is analogous to CL_HTON64.
360*
361* SEE ALSO
362*	Byte Swapping, CL_HTON64, CL_NTOH16, CL_NTOH32,
363*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
364*********/
365/****d* Component Library: Byte Swapping/CL_HTON64
366* NAME
367*	CL_HTON64
368*
369* DESCRIPTION
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.
374*
375* SYNOPSIS
376*	CL_HTON64( val );
377*
378* PARAMETERS
379*	val
380*		[in] 64-bit value to swap from host byte order to network byte order.
381*
382* RESULT
383*	Value of val converted to network byte order.
384*
385* NOTES
386*	This macro is analogous to CL_NTOH64.
387*
388* SEE ALSO
389*	Byte Swapping, CL_NTOH64, CL_HTON16, CL_HTON32,
390*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
391*********/
392#if CPU_LE
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) )
402#else
403#define CL_NTOH64( x )		(x)
404#endif
405#define CL_HTON64				CL_NTOH64
406/****f* Component Library: Byte Swapping/cl_ntoh64
407* NAME
408*	cl_ntoh64
409*
410* DESCRIPTION
411*	The cl_ntoh64 function converts a 64-bit value from network byte order to
412*	host byte order.
413*
414* SYNOPSIS
415*	uint64_t
416*	cl_ntoh64(
417*		IN	const uint64_t	val );
418*
419* PARAMETERS
420*	val
421*		[in] Value to swap from network byte order to host byte order.
422*
423* RETURN VALUE
424*	Value of val converted in host byte order.
425*
426* NOTES
427*	This function is analogous to cl_hton64.
428*
429* SEE ALSO
430*	Byte Swapping, cl_hton64, cl_ntoh16, cl_ntoh32, cl_ntoh
431*********/
432/****f* Component Library: Byte Swapping/cl_hton64
433* NAME
434*	cl_hton64
435*
436* DESCRIPTION
437*	The cl_hton64 function converts a 64-bit value from host byte order to
438*	network byte order.
439*
440* SYNOPSIS
441*	uint64_t
442*	cl_hton64(
443*		IN	const uint64_t	val );
444*
445* PARAMETERS
446*	val
447*		[in] Value to swap from host byte order to network byte order .
448*
449* RETURN VALUE
450*	Value of val converted to network byte order.
451*
452* NOTES
453*	This function is analogous to cl_ntoh64.
454*
455* SEE ALSO
456*	Byte Swapping, cl_ntoh64, cl_hton16, cl_hton32, cl_ntoh
457*********/
458#ifndef cl_ntoh64
459#define cl_ntoh64	CL_NTOH64
460#define cl_hton64	CL_HTON64
461#endif
462/****f* Component Library: Byte Swapping/cl_ntoh
463* NAME
464*	cl_ntoh
465*
466* DESCRIPTION
467*	The cl_ntoh function converts a value from network byte order to
468*	host byte order.
469*
470* SYNOPSIS
471*/
472static inline void
473cl_ntoh(OUT char *const p_dest,
474	IN const char *const p_src, IN const uint8_t size)
475{
476#if CPU_LE
477	uint8_t i;
478	char temp;
479
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++) {
483			temp = p_dest[i];
484			p_dest[i] = p_src[size - 1 - i];
485			p_dest[size - 1 - i] = temp;
486		}
487	} else {
488		for (i = 0; i < size; i++)
489			p_dest[i] = p_src[size - 1 - i];
490	}
491#else
492	/*
493	 * If the source and destination are not the same, copy the source to
494	 * the destination.
495	 */
496	if (p_src != p_dest)
497		memcpy(p_dest, p_src, size);
498#endif
499}
500
501/*
502* PARAMETERS
503*	p_dest
504*		[in] Pointer to a byte array to contain the converted value of p_src.
505*
506*	p_src
507*		[in] Pointer to a byte array to be converted from network byte
508*		ordering.
509*
510*	size
511*		[in] Number of bytes to swap.p_dest
512*
513* RETURN VALUE
514*	This function does not return a value.
515*
516* NOTES
517*	cl_ntoh can perform in place swapping if both p_src and p_dest point to
518*	the same buffer.
519*
520* SEE ALSO
521*	Byte Swapping, cl_ntoh16, cl_ntoh32, cl_ntoh64
522*********/
523
524END_C_DECLS
525#endif				/* _CL_BYTESWAP_H_ */
526