Back to index

glibc  2.9
byteswap.h
Go to the documentation of this file.
00001 /* Macros to swap the order of bytes in integer values.
00002    Copyright (C) 1997,1998,2000,2002,2003,2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
00021 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
00022 #endif
00023 
00024 #ifndef _BITS_BYTESWAP_H
00025 #define _BITS_BYTESWAP_H 1
00026 
00027 /* Swap bytes in 16 bit value.  */
00028 #define __bswap_constant_16(x) \
00029      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
00030 
00031 #if defined __GNUC__ && __GNUC__ >= 2
00032 # define __bswap_16(x) \
00033      (__extension__                                                  \
00034       ({ register unsigned short int __v, __x = (x);                        \
00035         if (__builtin_constant_p (x))                                       \
00036           __v = __bswap_constant_16 (__x);                                  \
00037         else                                                         \
00038           __asm__ __volatile__ ("shl %0 = %1, 48 ;;"                        \
00039                              "mux1 %0 = %0, @rev ;;"                 \
00040                              : "=r" (__v)                            \
00041                              : "r" ((unsigned short int) (__x)));           \
00042         __v; }))
00043 #else
00044 /* This is better than nothing.  */
00045 static __inline unsigned short int
00046 __bswap_16 (unsigned short int __bsx)
00047 {
00048   return __bswap_constant_16 (__bsx);
00049 }
00050 #endif
00051 
00052 
00053 /* Swap bytes in 32 bit value.  */
00054 #define __bswap_constant_32(x) \
00055      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |                    \
00056       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00057 
00058 #if defined __GNUC__ && __GNUC__ >= 2
00059 # define __bswap_32(x) \
00060      (__extension__                                                  \
00061       ({ register unsigned int __v, __x = (x);                              \
00062         if (__builtin_constant_p (x))                                       \
00063           __v = __bswap_constant_32 (__x);                                  \
00064         else                                                         \
00065           __asm__ __volatile__ ("shl %0 = %1, 32 ;;"                        \
00066                              "mux1 %0 = %0, @rev ;;"                 \
00067                              : "=r" (__v)                            \
00068                              : "r" ((unsigned int) (__x)));                 \
00069         __v; }))
00070 #else
00071 static __inline unsigned int
00072 __bswap_32 (unsigned int __bsx)
00073 {
00074   return __bswap_constant_32 (__bsx);
00075 }
00076 #endif
00077 
00078 
00079 /* Swap bytes in 64 bit value.  */
00080 #define __bswap_constant_64(x) \
00081      ((((x) & 0xff00000000000000ul) >> 56)                                  \
00082       | (((x) & 0x00ff000000000000ul) >>  40)                               \
00083       | (((x) & 0x0000ff0000000000ul) >> 24)                                \
00084       | (((x) & 0x000000ff00000000ul) >> 8)                                 \
00085       | (((x) & 0x00000000ff000000ul) << 8)                                 \
00086       | (((x) & 0x0000000000ff0000ul) << 24)                                \
00087       | (((x) & 0x000000000000ff00ul) << 40)                                \
00088       | (((x) & 0x00000000000000fful) << 56))
00089 
00090 #if defined __GNUC__ && __GNUC__ >= 2
00091 # define __bswap_64(x) \
00092      (__extension__                                                  \
00093       ({ register unsigned long int __v, __x = (x);                         \
00094         if (__builtin_constant_p (x))                                       \
00095           __v = __bswap_constant_64 (__x);                                  \
00096         else                                                         \
00097           __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"              \
00098                              : "=r" (__v)                            \
00099                              : "r" ((unsigned long int) (__x)));            \
00100          __v; }))
00101 
00102 #else
00103 static __inline unsigned long int
00104 __bswap_64 (unsigned long int __bsx)
00105 {
00106   return __bswap_constant_64 (__bsx);
00107 }
00108 #endif
00109 
00110 #endif /* _BITS_BYTESWAP_H */