Dr Andrew Scott G7VAV

My photo
 
June 2025
Mo Tu We Th Fr Sa Su
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6


byteswap.h
001: /* Macros to swap the order of bytes in integer values.
002:    Copyright (C) 1997, 1998, 2000, 2002, 2003, 2007, 2008, 2010, 2011
003:    Free Software Foundation, Inc.
004:    This file is part of the GNU C Library.
005: 
006:    The GNU C Library is free software; you can redistribute it and/or
007:    modify it under the terms of the GNU Lesser General Public
008:    License as published by the Free Software Foundation; either
009:    version 2.1 of the License, or (at your option) any later version.
010: 
011:    The GNU C Library is distributed in the hope that it will be useful,
012:    but WITHOUT ANY WARRANTY; without even the implied warranty of
013:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:    Lesser General Public License for more details.
015: 
016:    You should have received a copy of the GNU Lesser General Public
017:    License along with the GNU C Library; if not, write to the Free
018:    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
019:    02111-1307 USA.  */
020: 
021: #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
022: # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
023: #endif
024: 
025: #ifndef _BITS_BYTESWAP_H
026: #define _BITS_BYTESWAP_H 1
027: 
028: #include <bits/wordsize.h>
029: 
030: /* Swap bytes in 16 bit value.  */
031: #define __bswap_constant_16(x) \
032:      ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
033: 
034: #if defined __GNUC__ && __GNUC__ >= 2
035: # define __bswap_16(x) \
036:      (__extension__                                                           \
037:       ({ register unsigned short int __v, __x = (unsigned short int) (x);     \
038:          if (__builtin_constant_p (__x))                                      \
039:            __v = __bswap_constant_16 (__x);                                   \
040:          else                                                                 \
041:            __asm__ ("rorw $8, %w0"                                            \
042:                     : "=r" (__v)                                              \
043:                     : "0" (__x)                                               \
044:                     : "cc");                                                  \
045:          __v; }))
046: #else
047: /* This is better than nothing.  */
048: # define __bswap_16(x) \
049:      (__extension__                                                           \
050:       ({ register unsigned short int __x = (unsigned short int) (x);          \
051:          __bswap_constant_16 (__x); }))
052: #endif
053: 
054: 
055: /* Swap bytes in 32 bit value.  */
056: #define __bswap_constant_32(x) \
057:      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |               \
058:       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
059: 
060: #if defined __GNUC__ && __GNUC__ >= 2
061: # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__             \
062:                           || defined __pentiumpro__ || defined __pentium4__   \
063:                           || defined __k8__ || defined __athlon__             \
064:                           || defined __k6__ || defined __nocona__             \
065:                           || defined __core2__ || defined __geode__           \
066:                           || defined __amdfam10__)
067: /* To swap the bytes in a word the i486 processors and up provide the
068:    `bswap' opcode.  On i386 we have to use three instructions.  */
069: #  define __bswap_32(x) \
070:      (__extension__                                                           \
071:       ({ register unsigned int __v, __x = (x);                                \
072:          if (__builtin_constant_p (__x))                                      \
073:            __v = __bswap_constant_32 (__x);                                   \
074:          else                                                                 \
075:            __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));                     \
076:          __v; }))
077: # else
078: #  define __bswap_32(x)                                                       \
079:      (__extension__                                                           \
080:       ({ register unsigned int __v, __x = (x);                                \
081:          if (__builtin_constant_p (__x))                                      \
082:            __v = __bswap_constant_32 (__x);                                   \
083:          else                                                                 \
084:            __asm__ ("rorw $8, %w0;"                                           \
085:                     "rorl $16, %0;"                                           \
086:                     "rorw $8, %w0"                                            \
087:                     : "=r" (__v)                                              \
088:                     : "0" (__x)                                               \
089:                     : "cc");                                                  \
090:          __v; }))
091: # endif
092: #else
093: # define __bswap_32(x) \
094:      (__extension__                                                           \
095:       ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
096: #endif
097: 
098: 
099: #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: 


for client (none)
© Andrew Scott 2006 - 2025,
All Rights Reserved
http://www.andrew-scott.uk/
Andrew Scott
http://www.andrew-scott.co.uk/