File: /usr/include/bits/byteswap.h
1 /* Macros to swap the order of bytes in integer values.
2 Copyright (C) 1997, 1998, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
21 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
22 #endif
23
24 /* Swap bytes in 16 bit value. */
25 #define __bswap_constant_16(x) \
26 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
27
28 #if defined __GNUC__ && __GNUC__ >= 2
29 # define __bswap_16(x) \
30 (__extension__ \
31 ({ register unsigned short int __v; \
32 if (__builtin_constant_p (x)) \
33 __v = __bswap_constant_16 (x); \
34 else \
35 __asm__ __volatile__ ("rorw $8, %w0" \
36 : "=r" (__v) \
37 : "0" ((unsigned short int) (x)) \
38 : "cc"); \
39 __v; }))
40 #else
41 /* This is better than nothing. */
42 # define __bswap_16(x) __bswap_constant_16 (x)
43 #endif
44
45
46 /* Swap bytes in 32 bit value. */
47 #define __bswap_constant_32(x) \
48 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
49 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
50
51 #if defined __GNUC__ && __GNUC__ >= 2
52 /* To swap the bytes in a word the i486 processors and up provide the
53 `bswap' opcode. On i386 we have to use three instructions. */
54 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
55 # define __bswap_32(x) \
56 (__extension__ \
57 ({ register unsigned int __v; \
58 if (__builtin_constant_p (x)) \
59 __v = __bswap_constant_32 (x); \
60 else \
61 __asm__ __volatile__ ("rorw $8, %w0;" \
62 "rorl $16, %0;" \
63 "rorw $8, %w0" \
64 : "=r" (__v) \
65 : "0" ((unsigned int) (x)) \
66 : "cc"); \
67 __v; }))
68 # else
69 # define __bswap_32(x) \
70 (__extension__ \
71 ({ register unsigned int __v; \
72 if (__builtin_constant_p (x)) \
73 __v = __bswap_constant_32 (x); \
74 else \
75 __asm__ __volatile__ ("bswap %0" \
76 : "=r" (__v) \
77 : "0" ((unsigned int) (x))); \
78 __v; }))
79 # endif
80 #else
81 # define __bswap_32(x) __bswap_constant_32 (x)
82 #endif
83
84
85 #if defined __GNUC__ && __GNUC__ >= 2
86 /* Swap bytes in 64 bit value. */
87 #define __bswap_constant_64(x) \
88 ((((x) & 0xff00000000000000ull) >> 56) \
89 | (((x) & 0x00ff000000000000ull) >> 40) \
90 | (((x) & 0x0000ff0000000000ull) >> 24) \
91 | (((x) & 0x000000ff00000000ull) >> 8) \
92 | (((x) & 0x00000000ff000000ull) << 8) \
93 | (((x) & 0x0000000000ff0000ull) << 24) \
94 | (((x) & 0x000000000000ff00ull) << 40) \
95 | (((x) & 0x00000000000000ffull) << 56))
96
97 # define __bswap_64(x) \
98 (__extension__ \
99 ({ union { __extension__ unsigned long long int __ll; \
100 unsigned long int __l[2]; } __w, __r; \
101 if (__builtin_constant_p (x)) \
102 __r.__ll = __bswap_constant_64 (x); \
103 else \
104 { \
105 __w.__ll = (x); \
106 __r.__l[0] = __bswap_32 (__w.__l[1]); \
107 __r.__l[1] = __bswap_32 (__w.__l[0]); \
108 } \
109 __r.__ll; }))
110 #endif
111