home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue6 / SDL.ZIP / !gcc / include / unixlib / bits / h / byteswap next >
Encoding:
Text File  |  2006-09-17  |  3.3 KB  |  93 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /usr/local/cvsroot/gccsdk/unixlib/source/clib/bits/byteswap.h,v $
  4.  * $Date: 2002/12/22 18:22:28 $
  5.  * $Revision: 1.1 $
  6.  * $State: Exp $
  7.  * $Author: admin $
  8.  *
  9.  ***************************************************************************/
  10.  
  11. /*
  12.  * File taken from glibc 2.2.5.
  13.  * Following changes were made:
  14.  *  - For the non __GNUC__ case : declare __bswap_16() and __bswap_32() as
  15.  *    non inline functions.
  16.  */
  17.  
  18. /* Macros to swap the order of bytes in integer values.
  19.    Copyright (C) 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  20.    This file is part of the GNU C Library.
  21.  
  22.    The GNU C Library is free software; you can redistribute it and/or
  23.    modify it under the terms of the GNU Lesser General Public
  24.    License as published by the Free Software Foundation; either
  25.    version 2.1 of the License, or (at your option) any later version.
  26.  
  27.    The GNU C Library is distributed in the hope that it will be useful,
  28.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  30.    Lesser General Public License for more details.
  31.  
  32.    You should have received a copy of the GNU Lesser General Public
  33.    License along with the GNU C Library; if not, write to the Free
  34.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  35.    02111-1307 USA.  */
  36.  
  37. #if !defined __BYTESWAP_H && !defined __NETINET_IN_H
  38. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  39. #endif
  40.  
  41. #ifndef __BITS_BYTESWAP_H
  42. #define __BITS_BYTESWAP_H 1
  43.  
  44. /* Swap bytes in 16 bit value.  */
  45. #ifdef __GNUC__
  46. # define __bswap_16(x) \
  47.     (__extension__                                  \
  48.      ({ unsigned short int __bsx = (x);                          \
  49.         ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8)); }))
  50. #else
  51. extern unsigned short int __bswap_16 (unsigned short int __bsx);
  52. #endif
  53.  
  54. /* Swap bytes in 32 bit value.  */
  55. #ifdef __GNUC__
  56. # define __bswap_32(x) \
  57.     (__extension__                                  \
  58.      ({ unsigned int __bsx = (x);                          \
  59.         ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >>  8) |    \
  60.      (((__bsx) & 0x0000ff00) <<  8) | (((__bsx) & 0x000000ff) << 24)); }))
  61. #else
  62. extern unsigned int __bswap_32 (unsigned int __bsx);
  63. #endif
  64.  
  65. #ifdef __GNUC__
  66. /* Swap bytes in 64 bit value.  */
  67. # define __bswap_constant_64(x) \
  68.      ((((x) & 0xff00000000000000ull) >> 56)                      \
  69.       | (((x) & 0x00ff000000000000ull) >> 40)                      \
  70.       | (((x) & 0x0000ff0000000000ull) >> 24)                      \
  71.       | (((x) & 0x000000ff00000000ull) >> 8)                      \
  72.       | (((x) & 0x00000000ff000000ull) << 8)                      \
  73.       | (((x) & 0x0000000000ff0000ull) << 24)                      \
  74.       | (((x) & 0x000000000000ff00ull) << 40)                      \
  75.       | (((x) & 0x00000000000000ffull) << 56))
  76.  
  77. # define __bswap_64(x) \
  78.      (__extension__                                  \
  79.       ({ union { __extension__ unsigned long long int __ll;              \
  80.          unsigned int __l[2]; } __w, __r;                  \
  81.          if (__builtin_constant_p (x))                          \
  82.        __r.__ll = __bswap_constant_64 (x);                      \
  83.      else                                      \
  84.        {                                      \
  85.          __w.__ll = (x);                              \
  86.          __r.__l[0] = __bswap_32 (__w.__l[1]);                  \
  87.          __r.__l[1] = __bswap_32 (__w.__l[0]);                  \
  88.        }                                      \
  89.      __r.__ll; }))
  90. #endif
  91.  
  92. #endif /* _BITS_BYTESWAP_H */
  93.