home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / io / bmi / bmi-byteswap.h next >
C/C++ Source or Header  |  2006-09-13  |  4KB  |  114 lines

  1. /* Macros to swap the order of bytes in integer values.
  2.    Copyright (C) 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.  
  5.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Lesser General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2.1 of the License, or (at your option) any later version.
  9.  
  10.    The GNU C Library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Lesser General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Lesser General Public
  16.    License along with the GNU C Library; if not, write to the Free
  17.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18.    02111-1307 USA.  */
  19.  
  20. /* Modified by Phil Carns (pcarns@parl.clemson.edu) 
  21.  * June 2003
  22.  * For use with custom network encoding routines in the BMI component of 
  23.  * the Parallel Virtual File System version 2.
  24.  */
  25.  
  26. #ifndef __BMI_BYTESWAP_H
  27. #define __BMI_BYTESWAP_H 
  28.  
  29. #include "pvfs2-config.h"
  30.  
  31. #ifndef __bswap_16
  32. /* Swap bytes in 16 bit value.  */
  33. #ifdef __GNUC__
  34. # define __bswap_16(x) \
  35.     (__extension__                                  \
  36.      ({ unsigned short int __bsx = (x);                          \
  37.         ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8)); }))
  38. #else
  39. static __inline unsigned short int
  40. __bswap_16 (unsigned short int __bsx)
  41. {
  42.   return ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8));
  43. }
  44. #endif
  45. #endif
  46.  
  47. #ifndef __bswap_32
  48. /* Swap bytes in 32 bit value.  */
  49. #ifdef __GNUC__
  50. # define __bswap_32(x) \
  51.     (__extension__                                  \
  52.      ({ unsigned int __bsx = (x);                          \
  53.         ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >>  8) |    \
  54.      (((__bsx) & 0x0000ff00) <<  8) | (((__bsx) & 0x000000ff) << 24)); }))
  55. #else
  56. static __inline unsigned int
  57. __bswap_32 (unsigned int __bsx)
  58. {
  59.   return ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >>  8) |
  60.       (((__bsx) & 0x0000ff00) <<  8) | (((__bsx) & 0x000000ff) << 24));
  61. }
  62. #endif
  63. #endif
  64.  
  65. #ifndef __bswap_64
  66. #if defined __GNUC__ && __GNUC__ >= 2
  67. /* Swap bytes in 64 bit value.  */
  68. # define __bswap_constant_64(x) \
  69.      ((((x) & 0xff00000000000000ull) >> 56)                      \
  70.       | (((x) & 0x00ff000000000000ull) >> 40)                      \
  71.       | (((x) & 0x0000ff0000000000ull) >> 24)                      \
  72.       | (((x) & 0x000000ff00000000ull) >> 8)                      \
  73.       | (((x) & 0x00000000ff000000ull) << 8)                      \
  74.       | (((x) & 0x0000000000ff0000ull) << 24)                      \
  75.       | (((x) & 0x000000000000ff00ull) << 40)                      \
  76.       | (((x) & 0x00000000000000ffull) << 56))
  77.  
  78. # define __bswap_64(x) \
  79.      (__extension__                                  \
  80.       ({ union { __extension__ unsigned long long int __ll;              \
  81.          unsigned int __l[2]; } __w, __r;                  \
  82.          if (__builtin_constant_p (x))                          \
  83.        __r.__ll = __bswap_constant_64 (x);                      \
  84.      else                                      \
  85.        {                                      \
  86.          __w.__ll = (x);                              \
  87.          __r.__l[0] = __bswap_32 (__w.__l[1]);                  \
  88.          __r.__l[1] = __bswap_32 (__w.__l[0]);                  \
  89.        }                                      \
  90.      __r.__ll; }))
  91. #else
  92. #ifdef WORDS_BIGENDIAN
  93. #error FIX ME: no 64 bit bswap routine for non GNUC preprocessor.
  94. #endif
  95. #endif
  96. #endif 
  97.  
  98. #ifdef WORDS_BIGENDIAN
  99. #define htobmi16(x) __bswap_16(x)
  100. #define htobmi32(x) __bswap_32(x)
  101. #define htobmi64(x) __bswap_64(x)
  102. #else
  103. #define htobmi16(x) x
  104. #define htobmi32(x) x
  105. #define htobmi64(x) x
  106. #endif
  107.  
  108. #define bmitoh16(x) htobmi16(x) 
  109. #define bmitoh32(x) htobmi32(x)
  110. #define bmitoh64(x) htobmi64(x)
  111.  
  112. #endif /* __BMI_BYTESWAP_H */
  113.  
  114.