(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / x86_64 / bits / byteswap.h
blobe1c861c75f6ba5e22d4e76c4596430e82b493a67
1 /* Macros to swap the order of bytes in integer values.
2 Copyright (C) 1997, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
21 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
22 #endif
24 #ifndef _BITS_BYTESWAP_H
25 #define _BITS_BYTESWAP_H 1
27 #include <bits/wordsize.h>
29 /* Swap bytes in 16 bit value. */
30 #define __bswap_constant_16(x) \
31 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
33 #if defined __GNUC__ && __GNUC__ >= 2
34 # define __bswap_16(x) \
35 (__extension__ \
36 ({ register unsigned short int __v, __x = (x); \
37 if (__builtin_constant_p (__x)) \
38 __v = __bswap_constant_16 (__x); \
39 else \
40 __asm__ ("rorw $8, %w0" \
41 : "=r" (__v) \
42 : "0" (__x) \
43 : "cc"); \
44 __v; }))
45 #else
46 /* This is better than nothing. */
47 # define __bswap_16(x) \
48 (__extension__ \
49 ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
50 #endif
53 /* Swap bytes in 32 bit value. */
54 #define __bswap_constant_32(x) \
55 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
56 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
58 #if defined __GNUC__ && __GNUC__ >= 2
59 # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__ \
60 || defined __pentiumpro__ || defined __pentium4__ \
61 || defined __k8__ || defined __athlon__ \
62 || defined __k6__)
63 /* To swap the bytes in a word the i486 processors and up provide the
64 `bswap' opcode. On i386 we have to use three instructions. */
65 # define __bswap_32(x) \
66 (__extension__ \
67 ({ register unsigned int __v, __x = (x); \
68 if (__builtin_constant_p (__x)) \
69 __v = __bswap_constant_32 (__x); \
70 else \
71 __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
72 __v; }))
73 # else
74 # define __bswap_32(x) \
75 (__extension__ \
76 ({ register unsigned int __v, __x = (x); \
77 if (__builtin_constant_p (__x)) \
78 __v = __bswap_constant_32 (__x); \
79 else \
80 __asm__ ("rorw $8, %w0;" \
81 "rorl $16, %0;" \
82 "rorw $8, %w0" \
83 : "=r" (__v) \
84 : "0" (__x) \
85 : "cc"); \
86 __v; }))
87 # endif
88 #else
89 # define __bswap_32(x) \
90 (__extension__ \
91 ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
92 #endif
95 #if defined __GNUC__ && __GNUC__ >= 2
96 /* Swap bytes in 64 bit value. */
97 # define __bswap_constant_64(x) \
98 ((((x) & 0xff00000000000000ull) >> 56) \
99 | (((x) & 0x00ff000000000000ull) >> 40) \
100 | (((x) & 0x0000ff0000000000ull) >> 24) \
101 | (((x) & 0x000000ff00000000ull) >> 8) \
102 | (((x) & 0x00000000ff000000ull) << 8) \
103 | (((x) & 0x0000000000ff0000ull) << 24) \
104 | (((x) & 0x000000000000ff00ull) << 40) \
105 | (((x) & 0x00000000000000ffull) << 56))
107 # if __WORDSIZE == 64
108 # define __bswap_64(x) \
109 (__extension__ \
110 ({ register unsigned long __v, __x = (x); \
111 if (__builtin_constant_p (__x)) \
112 __v = __bswap_constant_64 (__x); \
113 else \
114 __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x)); \
115 __v; }))
116 # else
117 # define __bswap_64(x) \
118 (__extension__ \
119 ({ union { __extension__ unsigned long long int __ll; \
120 unsigned int __l[2]; } __w, __r; \
121 if (__builtin_constant_p (x)) \
122 __r.__ll = __bswap_constant_64 (x); \
123 else \
125 __w.__ll = (x); \
126 __r.__l[0] = __bswap_32 (__w.__l[1]); \
127 __r.__l[1] = __bswap_32 (__w.__l[0]); \
129 __r.__ll; }))
130 # endif
131 #endif
133 #endif /* _BITS_BYTESWAP_H */