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.  s390 version.
00002    Copyright (C) 2000, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
00003    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
00022 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
00023 #endif
00024 
00025 #include <bits/wordsize.h>
00026 
00027 #ifndef _BITS_BYTESWAP_H
00028 #define _BITS_BYTESWAP_H 1
00029 
00030 #define __bswap_constant_16(x) \
00031      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
00032 
00033 /* Swap bytes in 16 bit value. */
00034 #if defined __GNUC__ && __GNUC__ >= 2
00035 # if __WORDSIZE == 64
00036 #  define __bswap_16(x) \
00037      (__extension__                                                  \
00038       ({ unsigned short int __v, __x = (x);                                    \
00039         if (__builtin_constant_p (x))                                       \
00040           __v = __bswap_constant_16 (__x);                                  \
00041         else {                                                              \
00042            unsigned short int __tmp = (unsigned short int) (__x);             \
00043            __asm__ __volatile__ (                                             \
00044               "lrvh %0,%1"                                                    \
00045               : "=&d" (__v) : "m" (__tmp) );                                  \
00046          }                                                                    \
00047         __v; }))
00048 # else
00049 #  define __bswap_16(x) \
00050      (__extension__                                                  \
00051       ({ unsigned short int __v, __x = (x);                                    \
00052         if (__builtin_constant_p (x))                                       \
00053           __v = __bswap_constant_16 (__x);                                  \
00054         else {                                                              \
00055            unsigned short int __tmp = (unsigned short int) (__x);             \
00056            __asm__ __volatile__ (                                             \
00057               "sr   %0,%0\n"                                                  \
00058               "la   1,%1\n"                                                   \
00059               "icm  %0,2,1(1)\n"                                              \
00060               "ic   %0,0(1)"                                                  \
00061               : "=&d" (__v) : "m" (__tmp) : "1");                             \
00062          }                                                                    \
00063         __v; }))
00064 # endif
00065 #else
00066 /* This is better than nothing.  */
00067 static __inline unsigned short int
00068 __bswap_16 (unsigned short int __bsx)
00069 {
00070   return __bswap_constant_16 (__bsx);
00071 }
00072 #endif
00073 
00074 /* Swap bytes in 32 bit value.  */
00075 #define __bswap_constant_32(x) \
00076      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |                    \
00077       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00078 
00079 #if defined __GNUC__ && __GNUC__ >= 2
00080 # if __WORDSIZE == 64
00081 #  define __bswap_32(x) \
00082      (__extension__                                                  \
00083       ({ unsigned int __v, __x = (x);                                        \
00084         if (__builtin_constant_p (x))                                       \
00085           __v = __bswap_constant_32 (__x);                                  \
00086         else {                                                              \
00087            unsigned int __tmp = (unsigned int) (__x);                         \
00088            __asm__ __volatile__ (                                             \
00089               "lrv   %0,%1"                                                   \
00090               : "=&d" (__v) : "m" (__tmp));                                   \
00091          }                                                                    \
00092         __v; }))
00093 # else
00094 #  define __bswap_32(x) \
00095      (__extension__                                                  \
00096       ({ unsigned int __v, __x = (x);                                         \
00097         if (__builtin_constant_p (x))                                       \
00098           __v = __bswap_constant_32 (__x);                                  \
00099         else {                                                              \
00100            unsigned int __tmp = (unsigned int) (__x);                         \
00101            __asm__ __volatile__ (                                             \
00102               "la    1,%1\n"                                                  \
00103               "icm   %0,8,3(1)\n"                                             \
00104               "icm   %0,4,2(1)\n"                                             \
00105               "icm   %0,2,1(1)\n"                                             \
00106               "ic    %0,0(1)"                                                 \
00107               : "=&d" (__v) : "m" (__tmp) : "1");                             \
00108          }                                                                    \
00109         __v; }))
00110 # endif
00111 #else
00112 static __inline unsigned int
00113 __bswap_32 (unsigned int __bsx)
00114 {
00115   return __bswap_constant_32 (__bsx);
00116 }
00117 #endif
00118 
00119 /* Swap bytes in 64 bit value.  */
00120 #define __bswap_constant_64(x) \
00121      ((((x)&0xff00000000000000) >> 56) | (((x)&0x00ff000000000000) >> 40) |  \
00122       (((x)&0x0000ff0000000000) >> 24) | (((x)&0x000000ff00000000) >>  8) |  \
00123       (((x)&0x00000000ff000000) <<  8) | (((x)&0x0000000000ff0000) << 24) |  \
00124       (((x)&0x000000000000ff00) << 40) | (((x)&0x00000000000000ff) << 56))
00125 
00126 #if defined __GNUC__ && __GNUC__ >= 2
00127 # if __WORDSIZE == 64
00128 #  define __bswap_64(x) \
00129      (__extension__                                                  \
00130       ({ unsigned long __w, __x = (x);                                       \
00131         if (__builtin_constant_p (x))                                       \
00132           __w = __bswap_constant_64 (__x);                                  \
00133         else {                                                              \
00134            unsigned long __tmp = (unsigned long) (__x);                       \
00135            __asm__ __volatile__ (                                             \
00136               "lrvg  %0,%1"                                                   \
00137               : "=&d" (__w) : "m" (__tmp));                                   \
00138          }                                                                    \
00139         __w; }))
00140 # else
00141 #  define __bswap_64(x) \
00142      __extension__                               \
00143        ({ union { unsigned long long int __ll;          \
00144                  unsigned long int __l[2]; } __w, __r;  \
00145           __w.__ll = (x);                        \
00146           __r.__l[0] = __bswap_32 (__w.__l[1]);         \
00147           __r.__l[1] = __bswap_32 (__w.__l[0]);         \
00148           __r.__ll; })
00149 # endif
00150 #else
00151 static __inline unsigned long long int
00152 __bswap_64 (unsigned long long int __bsx)
00153 {
00154   return __bswap_constant_64 (__bsx);
00155 }
00156 #endif
00157 
00158 #endif /* _BITS_BYTESWAP_H */