Byteswap.h

Go to the documentation of this file.
00001 /* @(#)root/base:$Id: Byteswap.h 29820 2009-08-19 14:12:40Z rdm $ */
00002 
00003 /*************************************************************************
00004  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 #ifndef ROOT_Byteswap
00012 #define ROOT_Byteswap
00013 
00014 /* Copyright (C) 1997 Free Software Foundation, Inc.
00015    This file is part of the GNU C Library.
00016 
00017    The GNU C Library is free software; you can redistribute it and/or
00018    modify it under the terms of the GNU Library General Public License as
00019    published by the Free Software Foundation; either version 2 of the
00020    License, or (at your option) any later version.
00021 
00022    The GNU C Library is distributed in the hope that it will be useful,
00023    but WITHOUT ANY WARRANTY; without even the implied warranty of
00024    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025    Library General Public License for more details.
00026 
00027    You should have received a copy of the GNU Library General Public
00028    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00029    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00030    Boston, MA 02111-1307, USA.  */
00031 
00032 
00033 /* Get the machine specific, optimized definitions.  */
00034 /* The following is copied from <bits/byteswap.h> (only from RH6.0 and above) */
00035 
00036 /* Swap bytes in 16 bit value.  */
00037 #define R__bswap_constant_16(x) \
00038      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
00039 
00040 #if defined __GNUC__ && __GNUC__ >= 2
00041 # define R__bswap_16(x) \
00042      (__extension__                                                           \
00043       ({ register unsigned short int __v;                                     \
00044          if (__builtin_constant_p (x))                                        \
00045            __v = R__bswap_constant_16 (x);                                    \
00046          else                                                                 \
00047            __asm__ __volatile__ ("rorw $8, %w0"                               \
00048                                  : "=r" (__v)                                 \
00049                                  : "0" ((unsigned short int) (x))             \
00050                                  : "cc");                                     \
00051          __v; }))
00052 #else
00053 /* This is better than nothing.  */
00054 # define R__bswap_16(x) R__bswap_constant_16 (x)
00055 #endif
00056 
00057 
00058 /* Swap bytes in 32 bit value.  */
00059 #define R__bswap_constant_32(x) \
00060      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |               \
00061       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00062 
00063 #if defined __GNUC__ && __GNUC__ >= 2
00064 /* To swap the bytes in a word the i486 processors and up provide the
00065    `bswap' opcode.  On i386 we have to use three instructions.  */
00066 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ &&  \
00067      !defined __pentium4__ && !defined __x86_64__
00068 #  define R__bswap_32(x) \
00069      (__extension__                                                           \
00070       ({ register unsigned int __v;                                           \
00071          if (__builtin_constant_p (x))                                        \
00072            __v = R__bswap_constant_32 (x);                                    \
00073          else                                                                 \
00074            __asm__ __volatile__ ("rorw $8, %w0;"                              \
00075                                  "rorl $16, %0;"                              \
00076                                  "rorw $8, %w0"                               \
00077                                  : "=r" (__v)                                 \
00078                                  : "0" ((unsigned int) (x))                   \
00079                                  : "cc");                                     \
00080          __v; }))
00081 # else
00082 #  define R__bswap_32(x) \
00083      (__extension__                                                           \
00084       ({ register unsigned int __v;                                           \
00085          if (__builtin_constant_p (x))                                        \
00086            __v = R__bswap_constant_32 (x);                                    \
00087          else                                                                 \
00088            __asm__ __volatile__ ("bswap %0"                                   \
00089                                  : "=r" (__v)                                 \
00090                                  : "0" ((unsigned int) (x)));                 \
00091          __v; }))
00092 # endif
00093 #else
00094 # define R__bswap_32(x) R__bswap_constant_32 (x)
00095 #endif
00096 
00097 
00098 #if defined __GNUC__ && __GNUC__ >= 2
00099 /* Swap bytes in 64 bit value.  */
00100 # define R__bswap_64(x) \
00101      (__extension__                                                           \
00102       ({ union { __extension__ unsigned long long int __ll;                   \
00103                  UInt_t __l[2]; } __w, __r;                                   \
00104          __w.__ll = (x);                                                      \
00105          __r.__l[0] = R__bswap_32 (__w.__l[1]);                               \
00106          __r.__l[1] = R__bswap_32 (__w.__l[0]);                               \
00107          __r.__ll; }))
00108 #endif /* bits/byteswap.h */
00109 
00110 
00111 /* The following definitions must all be macros since otherwise some
00112    of the possible optimizations are not possible.  */
00113 
00114 /* Return a value with all bytes in the 16 bit argument swapped.  */
00115 #define Rbswap_16(x) R__bswap_16 (x)
00116 
00117 /* Return a value with all bytes in the 32 bit argument swapped.  */
00118 #define Rbswap_32(x) R__bswap_32 (x)
00119 
00120 #if defined __GNUC__ && __GNUC__ >= 2
00121 /* Return a value with all bytes in the 64 bit argument swapped.  */
00122 # define Rbswap_64(x) R__bswap_64 (x)
00123 #endif
00124 
00125 #endif /* Byteswap.h */

Generated on Tue Jul 5 14:10:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1