Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenbyteswap1.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 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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H 00022 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead." 00023 #endif 00024 00025 #ifndef _BITS_BYTESWAP_H 00026 #define _BITS_BYTESWAP_H 1 00027 00028 /* Swap bytes in 16 bit value. */ 00029 #define __bswap_constant_16(x) \ 00030 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) 00031 00032 #ifdef __GNUC__ 00033 # if __GNUC__ >= 2 00034 # define __bswap_16(x) \ 00035 (__extension__ \ 00036 ({ register unsigned short int __v, __x = (x); \ 00037 if (__builtin_constant_p (__x)) \ 00038 __v = __bswap_constant_16 (__x); \ 00039 else \ 00040 __asm__ ("rorw $8, %w0" \ 00041 : "=r" (__v) \ 00042 : "0" (__x) \ 00043 : "cc"); \ 00044 __v; })) 00045 # else 00046 /* This is better than nothing. */ 00047 # define __bswap_16(x) \ 00048 (__extension__ \ 00049 ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); })) 00050 # endif 00051 #else 00052 static __inline unsigned short int 00053 __bswap_16 (unsigned short int __bsx) 00054 { 00055 return __bswap_constant_16 (__bsx); 00056 } 00057 #endif 00058 00059 /* Swap bytes in 32 bit value. */ 00060 #define __bswap_constant_32(x) \ 00061 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ 00062 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) 00063 00064 #ifdef __GNUC__ 00065 # if __GNUC__ >= 2 00066 /* To swap the bytes in a word the i486 processors and up provide the 00067 `bswap' opcode. On i386 we have to use three instructions. */ 00068 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ \ 00069 && !defined __pentium4__ 00070 # define __bswap_32(x) \ 00071 (__extension__ \ 00072 ({ register unsigned int __v, __x = (x); \ 00073 if (__builtin_constant_p (__x)) \ 00074 __v = __bswap_constant_32 (__x); \ 00075 else \ 00076 __asm__ ("rorw $8, %w0;" \ 00077 "rorl $16, %0;" \ 00078 "rorw $8, %w0" \ 00079 : "=r" (__v) \ 00080 : "0" (__x) \ 00081 : "cc"); \ 00082 __v; })) 00083 # else 00084 # define __bswap_32(x) \ 00085 (__extension__ \ 00086 ({ register unsigned int __v, __x = (x); \ 00087 if (__builtin_constant_p (__x)) \ 00088 __v = __bswap_constant_32 (__x); \ 00089 else \ 00090 __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \ 00091 __v; })) 00092 # endif 00093 # else 00094 # define __bswap_32(x) \ 00095 (__extension__ \ 00096 ({ register unsigned int __x = (x); __bswap_constant_32 (__x); })) 00097 # endif 00098 #else 00099 static __inline unsigned int 00100 __bswap_32 (unsigned int __bsx) 00101 { 00102 return __bswap_constant_32 (__bsx); 00103 } 00104 #endif 00105 00106 00107 #if defined __GNUC__ && __GNUC__ >= 2 00108 /* Swap bytes in 64 bit value. */ 00109 #define __bswap_constant_64(x) \ 00110 ((((x) & 0xff00000000000000ull) >> 56) \ 00111 | (((x) & 0x00ff000000000000ull) >> 40) \ 00112 | (((x) & 0x0000ff0000000000ull) >> 24) \ 00113 | (((x) & 0x000000ff00000000ull) >> 8) \ 00114 | (((x) & 0x00000000ff000000ull) << 8) \ 00115 | (((x) & 0x0000000000ff0000ull) << 24) \ 00116 | (((x) & 0x000000000000ff00ull) << 40) \ 00117 | (((x) & 0x00000000000000ffull) << 56)) 00118 00119 # define __bswap_64(x) \ 00120 (__extension__ \ 00121 ({ union { __extension__ unsigned long long int __ll; \ 00122 unsigned long int __l[2]; } __w, __r; \ 00123 if (__builtin_constant_p (x)) \ 00124 __r.__ll = __bswap_constant_64 (x); \ 00125 else \ 00126 { \ 00127 __w.__ll = (x); \ 00128 __r.__l[0] = __bswap_32 (__w.__l[1]); \ 00129 __r.__l[1] = __bswap_32 (__w.__l[0]); \ 00130 } \ 00131 __r.__ll; })) 00132 #endif 00133 00134 #endif /* _BITS_BYTESWAP_H */ Generated on Sat May 26 2012 04:35:04 for ReactOS by
1.7.6.1
|