cl_byteswap.h revision 219820
1169695Skan/* 2169695Skan * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved. 3169695Skan * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved. 4169695Skan * Copyright (c) 1996-2003 Intel Corporation. All rights reserved. 5169695Skan * 6169695Skan * This software is available to you under a choice of one of two 7169695Skan * licenses. You may choose to be licensed under the terms of the GNU 8169695Skan * General Public License (GPL) Version 2, available from the file 9169695Skan * COPYING in the main directory of this source tree, or the 10169695Skan * OpenIB.org BSD license below: 11169695Skan * 12169695Skan * Redistribution and use in source and binary forms, with or 13169695Skan * without modification, are permitted provided that the following 14169695Skan * conditions are met: 15169695Skan * 16169695Skan * - Redistributions of source code must retain the above 17169695Skan * copyright notice, this list of conditions and the following 18169695Skan * disclaimer. 19169695Skan * 20169695Skan * - Redistributions in binary form must reproduce the above 21169695Skan * copyright notice, this list of conditions and the following 22169695Skan * disclaimer in the documentation and/or other materials 23169695Skan * provided with the distribution. 24169695Skan * 25169695Skan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26169695Skan * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27169695Skan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28169695Skan * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29169695Skan * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30169695Skan * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31169695Skan * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32169695Skan * SOFTWARE. 33169695Skan * 34169695Skan */ 35169695Skan 36169695Skan/* 37169695Skan * Abstract: 38169695Skan * provides byteswapping utilities. Basic functions are obtained from 39169695Skan * platform specific implementations from byteswap_osd.h. 40169695Skan */ 41169695Skan 42169695Skan#ifndef _CL_BYTESWAP_H_ 43169695Skan#define _CL_BYTESWAP_H_ 44169695Skan 45169695Skan#include <string.h> 46169695Skan#include <complib/cl_byteswap_osd.h> 47169695Skan 48169695Skan#ifdef __cplusplus 49169695Skan# define BEGIN_C_DECLS extern "C" { 50169695Skan# define END_C_DECLS } 51169695Skan#else /* !__cplusplus */ 52169695Skan# define BEGIN_C_DECLS 53169695Skan# define END_C_DECLS 54169695Skan#endif /* __cplusplus */ 55169695Skan 56169695SkanBEGIN_C_DECLS 57169695Skan/****h* Component Library/Byte Swapping 58169695Skan* NAME 59169695Skan* Byte Swapping 60169695Skan* 61169695Skan* DESCRIPTION 62169695Skan* The byte swapping functions and macros allow swapping bytes from network 63169695Skan* byte order to host byte order. 64169695Skan* 65169695Skan* All data transmitted between systems should be in network byte order. 66169695Skan* In order to utilize such data, it must be converted to host byte order 67169695Skan* before use. 68169695Skan* 69169695Skan* SEE ALSO 70169695Skan* Functions: 71169695Skan* cl_ntoh16, cl_hton16, cl_ntoh32, cl_hton32, cl_ntoh64, cl_hton64, 72169695Skan* cl_ntoh 73169695Skan* 74169695Skan* Macros: 75169695Skan* CL_NTOH16, CL_HTON16, CL_NTOH32, CL_HTON32, CL_NTOH64, CL_HTON64 76169695Skan*********/ 77169695Skan/* 78169695Skan * The byteswap_osd.h provides the following macros. 79169695Skan * __LITTLE_ENDIAN 80169695Skan * __BIG_ENDIAN 81169695Skan * __BYTE_ORDER 82169695Skan * 83169695Skan * If the platform provides byte swapping functions, byteswap_osd.h also 84169695Skan * provides the following macros. 85169695Skan * ntoh16, hton16 86169695Skan * ntoh32, hton32 87169695Skan * ntoh64, hton64 88169695Skan */ 89169695Skan 90169695Skan#ifndef __BYTE_ORDER 91169695Skan#error "__BYTE_ORDER macro undefined. Missing in endian.h?" 92169695Skan#endif 93169695Skan#if __BYTE_ORDER == __LITTLE_ENDIAN 94169695Skan#define CPU_LE 1 95169695Skan#define CPU_BE 0 96169695Skan#else 97169695Skan#define CPU_LE 0 98169695Skan#define CPU_BE 1 99169695Skan#endif 100169695Skan/****d* Component Library: Byte Swapping/CL_NTOH16 101169695Skan* NAME 102169695Skan* CL_NTOH16 103169695Skan* 104169695Skan* DESCRIPTION 105169695Skan* The CL_NTOH16 macro converts a 16-bit value from network byte order to 106169695Skan* host byte order. The CL_NTOH16 macro will cause constant values to be 107169695Skan* swapped by the pre-processor. For variables, CL_NTOH16 is less efficient 108169695Skan* than the cl_ntoh16 function. 109169695Skan* 110169695Skan* SYNOPSIS 111169695Skan* CL_NTOH16( val ); 112169695Skan* 113169695Skan* PARAMETERS 114169695Skan* val 115169695Skan* [in] 16-bit value to swap from network byte order to host byte order. 116169695Skan* 117169695Skan* RESULT 118169695Skan* Value of val converted to host byte order. 119169695Skan* 120169695Skan* NOTES 121169695Skan* This macro is analogous to CL_HTON16. 122169695Skan* 123169695Skan* SEE ALSO 124169695Skan* Byte Swapping, CL_HTON16, CL_NTOH32, CL_NTOH64, 125169695Skan* cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh 126169695Skan*********/ 127169695Skan/****d* Component Library: Byte Swapping/CL_HTON16 128169695Skan* NAME 129169695Skan* CL_HTON16 130169695Skan* 131169695Skan* DESCRIPTION 132169695Skan* The CL_HTON16 macro converts a 16-bit value from host byte order to 133169695Skan* network byte order. The CL_HTON16 macro will cause constant values to be 134169695Skan* swapped by the pre-processor. For variables, CL_HTON16 is less efficient 135169695Skan* than the cl_hton16 function. 136169695Skan* 137169695Skan* SYNOPSIS 138169695Skan* CL_HTON16( val ); 139169695Skan* 140169695Skan* PARAMETERS 141169695Skan* val 142169695Skan* [in] 16-bit value to swap from host byte order to network byte order. 143169695Skan* 144169695Skan* RESULT 145169695Skan* Value of val converted to network byte order. 146169695Skan* 147169695Skan* NOTES 148169695Skan* This macro is analogous to CL_NTOH16. 149169695Skan* 150169695Skan* SEE ALSO 151169695Skan* Byte Swapping, CL_NTOH16, CL_HTON32, CL_HTON64, 152169695Skan* cl_hton16, cl_hton32, cl_hton64, cl_ntoh 153169695Skan*********/ 154169695Skan#if CPU_LE 155169695Skan#define CL_NTOH16( x ) (uint16_t)( \ 156169695Skan (((uint16_t)(x) & 0x00FF) << 8) | \ 157169695Skan (((uint16_t)(x) & 0xFF00) >> 8) ) 158169695Skan#else 159169695Skan#define CL_NTOH16( x ) (x) 160169695Skan#endif 161169695Skan#define CL_HTON16 CL_NTOH16 162169695Skan/****f* Component Library: Byte Swapping/cl_ntoh16 163169695Skan* NAME 164169695Skan* cl_ntoh16 165169695Skan* 166169695Skan* DESCRIPTION 167169695Skan* The cl_ntoh16 function converts a 16-bit value from network byte order to 168169695Skan* host byte order. 169169695Skan* 170169695Skan* SYNOPSIS 171169695Skan* uint16_t 172169695Skan* cl_ntoh16( 173169695Skan* IN const uint16_t val ); 174169695Skan* 175169695Skan* PARAMETERS 176169695Skan* val 177169695Skan* [in] Value to swap from network byte order to host byte order. 178169695Skan* 179169695Skan* RETURN VALUE 180169695Skan* Value of val converted to host byte order. 181169695Skan* 182169695Skan* NOTES 183169695Skan* This function is analogous to cl_hton16. 184169695Skan* 185169695Skan* SEE ALSO 186169695Skan* Byte Swapping, cl_hton16, cl_ntoh32, cl_ntoh64, cl_ntoh 187169695Skan*********/ 188169695Skan/****f* Component Library: Byte Swapping/cl_hton16 189169695Skan* NAME 190169695Skan* cl_hton16 191169695Skan* 192169695Skan* DESCRIPTION 193169695Skan* The cl_hton16 function converts a 16-bit value from host byte order to 194169695Skan* network byte order. 195169695Skan* 196169695Skan* SYNOPSIS 197169695Skan* uint16_t 198169695Skan* cl_hton16( 199169695Skan* IN const uint16_t val ); 200169695Skan* 201169695Skan* PARAMETERS 202169695Skan* val 203169695Skan* [in] Value to swap from host byte order to network byte order . 204169695Skan* 205169695Skan* RETURN VALUE 206169695Skan* Value of val converted to network byte order. 207169695Skan* 208169695Skan* NOTES 209169695Skan* This function is analogous to cl_ntoh16. 210169695Skan* 211169695Skan* SEE ALSO 212169695Skan* Byte Swapping, cl_ntoh16, cl_hton32, cl_hton64, cl_ntoh 213169695Skan*********/ 214169695Skan#ifndef cl_ntoh16 215169695Skan#define cl_ntoh16 CL_NTOH16 216169695Skan#define cl_hton16 CL_HTON16 217169695Skan#endif 218169695Skan/****d* Component Library: Byte Swapping/CL_NTOH32 219169695Skan* NAME 220169695Skan* CL_NTOH32 221169695Skan* 222169695Skan* DESCRIPTION 223169695Skan* The CL_NTOH32 macro converts a 32-bit value from network byte order to 224169695Skan* host byte order. The CL_NTOH32 macro will cause constant values to be 225169695Skan* swapped by the pre-processor. For variables, CL_NTOH32 is less efficient 226169695Skan* than the cl_ntoh32 function. 227169695Skan* 228169695Skan* SYNOPSIS 229169695Skan* CL_NTOH32( val ); 230169695Skan* 231169695Skan* PARAMETERS 232169695Skan* val 233169695Skan* [in] 32-bit value to swap from network byte order to host byte order. 234169695Skan* 235169695Skan* RESULT 236169695Skan* Value of val converted to host byte order. 237169695Skan* 238169695Skan* NOTES 239169695Skan* This macro is analogous to CL_HTON32. 240169695Skan* 241169695Skan* SEE ALSO 242169695Skan* Byte Swapping, CL_HTON32, CL_NTOH16, CL_NTOH64, 243169695Skan* cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh 244169695Skan*********/ 245169695Skan/****d* Component Library: Byte Swapping/CL_HTON32 246169695Skan* NAME 247169695Skan* CL_HTON32 248169695Skan* 249169695Skan* DESCRIPTION 250169695Skan* The CL_HTON32 macro converts a 32-bit value from host byte order to 251169695Skan* network byte order. The CL_HTON32 macro will cause constant values to be 252169695Skan* swapped by the pre-processor. For variables, CL_HTON32 is less efficient 253169695Skan* than the cl_hton32 function. 254169695Skan* 255169695Skan* SYNOPSIS 256169695Skan* CL_HTON32( val ); 257169695Skan* 258169695Skan* PARAMETERS 259169695Skan* val 260169695Skan* [in] 32-bit value to swap from host byte order to network byte order. 261169695Skan* 262169695Skan* RESULT 263169695Skan* Value of val converted to network byte order. 264169695Skan* 265169695Skan* NOTES 266169695Skan* This macro is analogous to CL_NTOH32. 267169695Skan* 268169695Skan* SEE ALSO 269169695Skan* Byte Swapping, CL_NTOH32, CL_HTON16, CL_HTON64, 270169695Skan* cl_hton16, cl_hton32, cl_hton64, cl_ntoh 271169695Skan*********/ 272169695Skan#if CPU_LE 273169695Skan#define CL_NTOH32( x ) (uint32_t)( \ 274169695Skan (((uint32_t)(x) & 0x000000FF) << 24) | \ 275169695Skan (((uint32_t)(x) & 0x0000FF00) << 8) | \ 276169695Skan (((uint32_t)(x) & 0x00FF0000) >> 8) | \ 277169695Skan (((uint32_t)(x) & 0xFF000000) >> 24) ) 278169695Skan#else 279169695Skan#define CL_NTOH32( x ) (x) 280169695Skan#endif 281169695Skan#define CL_HTON32 CL_NTOH32 282169695Skan/****f* Component Library: Byte Swapping/cl_ntoh32 283169695Skan* NAME 284169695Skan* cl_ntoh32 285169695Skan* 286169695Skan* DESCRIPTION 287169695Skan* The cl_ntoh32 function converts a 32-bit value from network byte order to 288169695Skan* host byte order. 289169695Skan* 290169695Skan* SYNOPSIS 291169695Skan* uint32_t 292169695Skan* cl_ntoh32( 293169695Skan* IN const uint32_t val ); 294169695Skan* 295169695Skan* PARAMETERS 296169695Skan* val 297169695Skan* [in] Value to swap from network byte order to host byte order. 298169695Skan* 299169695Skan* RETURN VALUE 300169695Skan* Value of val converted in host byte order. 301169695Skan* 302169695Skan* NOTES 303169695Skan* This function is analogous to cl_hton32. 304169695Skan* 305169695Skan* SEE ALSO 306169695Skan* Byte Swapping, cl_hton32, cl_ntoh16, cl_ntoh64, cl_ntoh 307169695Skan*********/ 308169695Skan/****f* Component Library: Byte Swapping/cl_hton32 309169695Skan* NAME 310169695Skan* cl_hton32 311169695Skan* 312169695Skan* DESCRIPTION 313169695Skan* The cl_hton32 function converts a 32-bit value from host byte order to 314169695Skan* network byte order. 315169695Skan* 316169695Skan* SYNOPSIS 317169695Skan* uint32_t 318169695Skan* cl_hton32( 319169695Skan* IN const uint32_t val ); 320169695Skan* 321169695Skan* PARAMETERS 322169695Skan* val 323169695Skan* [in] Value to swap from host byte order to network byte order . 324169695Skan* 325169695Skan* RETURN VALUE 326169695Skan* Value of val converted to network byte order. 327169695Skan* 328169695Skan* NOTES 329169695Skan* This function is analogous to cl_ntoh32. 330169695Skan* 331169695Skan* SEE ALSO 332169695Skan* Byte Swapping, cl_ntoh32, cl_hton16, cl_hton64, cl_ntoh 333169695Skan*********/ 334169695Skan#ifndef cl_ntoh32 335169695Skan#define cl_ntoh32 CL_NTOH32 336169695Skan#define cl_hton32 CL_HTON32 337169695Skan#endif 338169695Skan/****d* Component Library: Byte Swapping/CL_NTOH64 339169695Skan* NAME 340169695Skan* CL_NTOH64 341169695Skan* 342169695Skan* DESCRIPTION 343169695Skan* The CL_NTOH64 macro converts a 64-bit value from network byte order to 344169695Skan* host byte order. The CL_NTOH64 macro will cause constant values to be 345169695Skan* swapped by the pre-processor. For variables, CL_NTOH64 is less efficient 346169695Skan* than the cl_ntoh64 function. 347169695Skan* 348169695Skan* SYNOPSIS 349169695Skan* CL_NTOH64( val ); 350169695Skan* 351169695Skan* PARAMETERS 352169695Skan* val 353169695Skan* [in] 64-bit value to swap from network byte order to host byte order. 354169695Skan* 355169695Skan* RESULT 356169695Skan* Value of val converted to host byte order. 357169695Skan* 358169695Skan* NOTES 359169695Skan* This macro is analogous to CL_HTON64. 360169695Skan* 361169695Skan* SEE ALSO 362169695Skan* Byte Swapping, CL_HTON64, CL_NTOH16, CL_NTOH32, 363169695Skan* cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh 364169695Skan*********/ 365169695Skan/****d* Component Library: Byte Swapping/CL_HTON64 366169695Skan* NAME 367169695Skan* CL_HTON64 368169695Skan* 369169695Skan* DESCRIPTION 370169695Skan* The CL_HTON64 macro converts a 64-bit value from host byte order to 371169695Skan* network byte order. The CL_HTON64 macro will cause constant values to be 372169695Skan* swapped by the pre-processor. For variables, CL_HTON64 is less efficient 373169695Skan* than the cl_hton64 function. 374169695Skan* 375169695Skan* SYNOPSIS 376169695Skan* CL_HTON64( val ); 377169695Skan* 378169695Skan* PARAMETERS 379169695Skan* val 380169695Skan* [in] 64-bit value to swap from host byte order to network byte order. 381169695Skan* 382169695Skan* RESULT 383169695Skan* Value of val converted to network byte order. 384169695Skan* 385169695Skan* NOTES 386169695Skan* This macro is analogous to CL_NTOH64. 387169695Skan* 388169695Skan* SEE ALSO 389169695Skan* Byte Swapping, CL_NTOH64, CL_HTON16, CL_HTON32, 390169695Skan* cl_hton16, cl_hton32, cl_hton64, cl_ntoh 391169695Skan*********/ 392169695Skan#if CPU_LE 393169695Skan#define CL_NTOH64( x ) (uint64_t)( \ 394169695Skan (((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \ 395169695Skan (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \ 396169695Skan (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \ 397169695Skan (((uint64_t)(x) & 0x00000000FF000000ULL) << 8 ) | \ 398169695Skan (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8 ) | \ 399169695Skan (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \ 400169695Skan (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \ 401169695Skan (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56) ) 402169695Skan#else 403169695Skan#define CL_NTOH64( x ) (x) 404169695Skan#endif 405169695Skan#define CL_HTON64 CL_NTOH64 406169695Skan/****f* Component Library: Byte Swapping/cl_ntoh64 407169695Skan* NAME 408169695Skan* cl_ntoh64 409169695Skan* 410169695Skan* DESCRIPTION 411169695Skan* The cl_ntoh64 function converts a 64-bit value from network byte order to 412169695Skan* host byte order. 413169695Skan* 414169695Skan* SYNOPSIS 415169695Skan* uint64_t 416169695Skan* cl_ntoh64( 417169695Skan* IN const uint64_t val ); 418169695Skan* 419169695Skan* PARAMETERS 420169695Skan* val 421169695Skan* [in] Value to swap from network byte order to host byte order. 422169695Skan* 423169695Skan* RETURN VALUE 424169695Skan* Value of val converted in host byte order. 425169695Skan* 426169695Skan* NOTES 427169695Skan* This function is analogous to cl_hton64. 428169695Skan* 429169695Skan* SEE ALSO 430169695Skan* Byte Swapping, cl_hton64, cl_ntoh16, cl_ntoh32, cl_ntoh 431169695Skan*********/ 432169695Skan/****f* Component Library: Byte Swapping/cl_hton64 433169695Skan* NAME 434169695Skan* cl_hton64 435169695Skan* 436169695Skan* DESCRIPTION 437169695Skan* The cl_hton64 function converts a 64-bit value from host byte order to 438169695Skan* network byte order. 439169695Skan* 440169695Skan* SYNOPSIS 441169695Skan* uint64_t 442169695Skan* cl_hton64( 443169695Skan* IN const uint64_t val ); 444169695Skan* 445169695Skan* PARAMETERS 446169695Skan* val 447169695Skan* [in] Value to swap from host byte order to network byte order . 448169695Skan* 449169695Skan* RETURN VALUE 450169695Skan* Value of val converted to network byte order. 451169695Skan* 452169695Skan* NOTES 453169695Skan* This function is analogous to cl_ntoh64. 454169695Skan* 455169695Skan* SEE ALSO 456169695Skan* Byte Swapping, cl_ntoh64, cl_hton16, cl_hton32, cl_ntoh 457169695Skan*********/ 458169695Skan#ifndef cl_ntoh64 459169695Skan#define cl_ntoh64 CL_NTOH64 460169695Skan#define cl_hton64 CL_HTON64 461169695Skan#endif 462169695Skan/****f* Component Library: Byte Swapping/cl_ntoh 463169695Skan* NAME 464169695Skan* cl_ntoh 465169695Skan* 466169695Skan* DESCRIPTION 467169695Skan* The cl_ntoh function converts a value from network byte order to 468169695Skan* host byte order. 469169695Skan* 470169695Skan* SYNOPSIS 471169695Skan*/ 472169695Skanstatic inline void 473169695Skancl_ntoh(OUT char *const p_dest, 474169695Skan IN const char *const p_src, IN const uint8_t size) 475169695Skan{ 476169695Skan#if CPU_LE 477169695Skan uint8_t i; 478169695Skan char temp; 479169695Skan 480169695Skan if (p_src == p_dest) { 481169695Skan /* Swap in place if source and destination are the same. */ 482169695Skan for (i = 0; i < size / 2; i++) { 483169695Skan temp = p_dest[i]; 484169695Skan p_dest[i] = p_src[size - 1 - i]; 485169695Skan p_dest[size - 1 - i] = temp; 486169695Skan } 487169695Skan } else { 488169695Skan for (i = 0; i < size; i++) 489169695Skan p_dest[i] = p_src[size - 1 - i]; 490169695Skan } 491169695Skan#else 492169695Skan /* 493169695Skan * If the source and destination are not the same, copy the source to 494169695Skan * the destination. 495169695Skan */ 496169695Skan if (p_src != p_dest) 497169695Skan memcpy(p_dest, p_src, size); 498169695Skan#endif 499169695Skan} 500169695Skan 501169695Skan/* 502169695Skan* PARAMETERS 503169695Skan* p_dest 504169695Skan* [in] Pointer to a byte array to contain the converted value of p_src. 505169695Skan* 506169695Skan* p_src 507169695Skan* [in] Pointer to a byte array to be converted from network byte 508169695Skan* ordering. 509169695Skan* 510169695Skan* size 511169695Skan* [in] Number of bytes to swap.p_dest 512169695Skan* 513169695Skan* RETURN VALUE 514169695Skan* This function does not return a value. 515169695Skan* 516169695Skan* NOTES 517169695Skan* cl_ntoh can perform in place swapping if both p_src and p_dest point to 518169695Skan* the same buffer. 519169695Skan* 520169695Skan* SEE ALSO 521169695Skan* Byte Swapping, cl_ntoh16, cl_ntoh32, cl_ntoh64 522169695Skan*********/ 523169695Skan 524169695SkanEND_C_DECLS 525169695Skan#endif /* _CL_BYTESWAP_H_ */ 526169695Skan