Home | History | Annotate | Download | only in bits
      1 /* Macros to swap the order of bytes in integer values.
      2    Copyright (C) 1997, 1998, 2000, 2002, 2003, 2007, 2008, 2010, 2011
      3    Free Software Foundation, Inc.
      4    This file is part of the GNU C Library.
      5 
      6    The GNU C Library is free software; you can redistribute it and/or
      7    modify it under the terms of the GNU Lesser General Public
      8    License as published by the Free Software Foundation; either
      9    version 2.1 of the License, or (at your option) any later version.
     10 
     11    The GNU C Library is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14    Lesser General Public License for more details.
     15 
     16    You should have received a copy of the GNU Lesser General Public
     17    License along with the GNU C Library; if not, write to the Free
     18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     19    02111-1307 USA.  */
     20 
     21 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
     22 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
     23 #endif
     24 
     25 #ifndef _BITS_BYTESWAP_H
     26 #define _BITS_BYTESWAP_H 1
     27 
     28 #include <bits/wordsize.h>
     29 
     30 /* Swap bytes in 16 bit value.  */
     31 #define __bswap_constant_16(x) \
     32      ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
     33 
     34 #if defined __GNUC__ && __GNUC__ >= 2
     35 # define __bswap_16(x) \
     36      (__extension__							      \
     37       ({ register unsigned short int __v, __x = (unsigned short int) (x);     \
     38 	 if (__builtin_constant_p (__x))				      \
     39 	   __v = __bswap_constant_16 (__x);				      \
     40 	 else								      \
     41 	   __asm__ ("rorw $8, %w0"					      \
     42 		    : "=r" (__v)					      \
     43 		    : "0" (__x)						      \
     44 		    : "cc");						      \
     45 	 __v; }))
     46 #else
     47 /* This is better than nothing.  */
     48 # define __bswap_16(x) \
     49      (__extension__							      \
     50       ({ register unsigned short int __x = (unsigned short int) (x);          \
     51 	 __bswap_constant_16 (__x); }))
     52 #endif
     53 
     54 
     55 /* Swap bytes in 32 bit value.  */
     56 #define __bswap_constant_32(x) \
     57      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |		      \
     58       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
     59 
     60 #if defined __GNUC__ && __GNUC__ >= 2
     61 # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__	      \
     62 			  || defined __pentiumpro__ || defined __pentium4__   \
     63 			  || defined __k8__ || defined __athlon__	      \
     64 			  || defined __k6__ || defined __nocona__	      \
     65 			  || defined __core2__ || defined __geode__	      \
     66 			  || defined __amdfam10__)
     67 /* To swap the bytes in a word the i486 processors and up provide the
     68    `bswap' opcode.  On i386 we have to use three instructions.  */
     69 #  define __bswap_32(x) \
     70      (__extension__							      \
     71       ({ register unsigned int __v, __x = (x);				      \
     72 	 if (__builtin_constant_p (__x))				      \
     73 	   __v = __bswap_constant_32 (__x);				      \
     74 	 else								      \
     75 	   __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));		      \
     76 	 __v; }))
     77 # else
     78 #  define __bswap_32(x)							      \
     79      (__extension__							      \
     80       ({ register unsigned int __v, __x = (x);				      \
     81 	 if (__builtin_constant_p (__x))				      \
     82 	   __v = __bswap_constant_32 (__x);				      \
     83 	 else								      \
     84 	   __asm__ ("rorw $8, %w0;"					      \
     85 		    "rorl $16, %0;"					      \
     86 		    "rorw $8, %w0"					      \
     87 		    : "=r" (__v)					      \
     88 		    : "0" (__x)						      \
     89 		    : "cc");						      \
     90 	 __v; }))
     91 # endif
     92 #else
     93 # define __bswap_32(x) \
     94      (__extension__							      \
     95       ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
     96 #endif
     97 
     98 
     99 #if defined __GNUC__ && __GNUC__ >= 2
    100 /* Swap bytes in 64 bit value.  */
    101 # define __bswap_constant_64(x) \
    102      (__extension__ ((((x) & 0xff00000000000000ull) >> 56)		      \
    103 		     | (((x) & 0x00ff000000000000ull) >> 40)		      \
    104 		     | (((x) & 0x0000ff0000000000ull) >> 24)		      \
    105 		     | (((x) & 0x000000ff00000000ull) >> 8)		      \
    106 		     | (((x) & 0x00000000ff000000ull) << 8)		      \
    107 		     | (((x) & 0x0000000000ff0000ull) << 24)		      \
    108 		     | (((x) & 0x000000000000ff00ull) << 40)		      \
    109 		     | (((x) & 0x00000000000000ffull) << 56)))
    110 
    111 # if __WORDSIZE == 64
    112 #  define __bswap_64(x) \
    113      (__extension__							      \
    114       ({ register unsigned long __v, __x = (x);				      \
    115 	 if (__builtin_constant_p (__x))				      \
    116 	   __v = __bswap_constant_64 (__x);				      \
    117 	 else								      \
    118 	   __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x));		      \
    119 	 __v; }))
    120 # else
    121 #  define __bswap_64(x) \
    122      (__extension__                                                           \
    123       ({ union { __extension__ unsigned long long int __ll;                   \
    124 		 unsigned int __l[2]; } __w, __r;                             \
    125 	 if (__builtin_constant_p (x))                                        \
    126 	   __r.__ll = __bswap_constant_64 (x);                                \
    127 	 else                                                                 \
    128 	   {                                                                  \
    129 	     __w.__ll = (x);                                                  \
    130 	     __r.__l[0] = __bswap_32 (__w.__l[1]);                            \
    131 	     __r.__l[1] = __bswap_32 (__w.__l[0]);                            \
    132 	   }                                                                  \
    133 	 __r.__ll; }))
    134 # endif
    135 #endif
    136 
    137 #endif /* _BITS_BYTESWAP_H */
    138