modernc.org/cc@v1.0.1/v2/headers/linux_386/usr/include/i386-linux-gnu/bits/byteswap.h (about) 1 /* Macros to swap the order of bytes in integer values. 2 Copyright (C) 1997-2018 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, see 17 <http://www.gnu.org/licenses/>. */ 18 19 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H 20 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead." 21 #endif 22 23 #ifndef _BITS_BYTESWAP_H 24 #define _BITS_BYTESWAP_H 1 25 26 #include <features.h> 27 #include <bits/types.h> 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 /* Get __bswap_16. */ 35 #include <bits/byteswap-16.h> 36 37 /* Swap bytes in 32 bit value. */ 38 #define __bswap_constant_32(x) \ 39 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ 40 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) 41 42 #ifdef __GNUC__ 43 # if __GNUC_PREREQ (4, 3) 44 static __inline unsigned int __bswap_32(unsigned int __bsx) 45 { 46 return __builtin_bswap32(__bsx); 47 } 48 # elif __GNUC__ >= 2 49 # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__ \ 50 || defined __pentiumpro__ || defined __pentium4__ \ 51 || defined __k8__ || defined __athlon__ \ 52 || defined __k6__ || defined __nocona__ \ 53 || defined __core2__ || defined __geode__ \ 54 || defined __amdfam10__) 55 /* To swap the bytes in a word the i486 processors and up provide the 56 `bswap' opcode. On i386 we have to use three instructions. */ 57 # define __bswap_32(x) \ 58 (__extension__ \ 59 ({ unsigned int __v, __x = (x); \ 60 if (__builtin_constant_p (__x)) \ 61 __v = __bswap_constant_32 (__x); \ 62 else \ 63 __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \ 64 __v; })) 65 # else 66 # define __bswap_32(x) \ 67 (__extension__ \ 68 ({ unsigned int __v, __x = (x); \ 69 if (__builtin_constant_p (__x)) \ 70 __v = __bswap_constant_32 (__x); \ 71 else \ 72 __asm__ ("rorw $8, %w0;" \ 73 "rorl $16, %0;" \ 74 "rorw $8, %w0" \ 75 : "=r" (__v) \ 76 : "0" (__x) \ 77 : "cc"); \ 78 __v; })) 79 # endif 80 # else 81 # define __bswap_32(x) \ 82 (__extension__ \ 83 ({ unsigned int __x = (x); __bswap_constant_32 (__x); })) 84 # endif 85 #else 86 static __inline unsigned int __bswap_32(unsigned int __bsx) 87 { 88 return __bswap_constant_32(__bsx); 89 } 90 #endif 91 92 #if __GNUC_PREREQ (2, 0) 93 /* Swap bytes in 64 bit value. */ 94 # define __bswap_constant_64(x) \ 95 (__extension__ ((((x) & 0xff00000000000000ull) >> 56) \ 96 | (((x) & 0x00ff000000000000ull) >> 40) \ 97 | (((x) & 0x0000ff0000000000ull) >> 24) \ 98 | (((x) & 0x000000ff00000000ull) >> 8) \ 99 | (((x) & 0x00000000ff000000ull) << 8) \ 100 | (((x) & 0x0000000000ff0000ull) << 24) \ 101 | (((x) & 0x000000000000ff00ull) << 40) \ 102 | (((x) & 0x00000000000000ffull) << 56))) 103 104 # if __GNUC_PREREQ (4, 3) 105 static __inline __uint64_t __bswap_64(__uint64_t __bsx) 106 { 107 return __builtin_bswap64(__bsx); 108 } 109 # elif __WORDSIZE == 64 110 # define __bswap_64(x) \ 111 (__extension__ \ 112 ({ __uint64_t __v, __x = (x); \ 113 if (__builtin_constant_p (__x)) \ 114 __v = __bswap_constant_64 (__x); \ 115 else \ 116 __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x)); \ 117 __v; })) 118 # else 119 # define __bswap_64(x) \ 120 (__extension__ \ 121 ({ union { __extension__ __uint64_t __ll; \ 122 unsigned int __l[2]; } __w, __r; \ 123 if (__builtin_constant_p (x)) \ 124 __r.__ll = __bswap_constant_64 (x); \ 125 else \ 126 { \ 127 __w.__ll = (x); \ 128 __r.__l[0] = __bswap_32 (__w.__l[1]); \ 129 __r.__l[1] = __bswap_32 (__w.__l[0]); \ 130 } \ 131 __r.__ll; })) 132 # endif 133 #else 134 # define __bswap_constant_64(x) \ 135 ((((x) & 0xff00000000000000ull) >> 56) \ 136 | (((x) & 0x00ff000000000000ull) >> 40) \ 137 | (((x) & 0x0000ff0000000000ull) >> 24) \ 138 | (((x) & 0x000000ff00000000ull) >> 8) \ 139 | (((x) & 0x00000000ff000000ull) << 8) \ 140 | (((x) & 0x0000000000ff0000ull) << 24) \ 141 | (((x) & 0x000000000000ff00ull) << 40) \ 142 | (((x) & 0x00000000000000ffull) << 56)) 143 144 static __inline __uint64_t __bswap_64(__uint64_t __bsx) 145 { 146 return __bswap_constant_64(__bsx); 147 } 148 #endif 149 150 #endif /* _BITS_BYTESWAP_H */