]>
Commit | Line | Data |
---|---|---|
d5efd131 | 1 | /* Macros to swap the order of bytes in integer values. |
b168057a | 2 | Copyright (C) 1997-2015 Free Software Foundation, Inc. |
d5efd131 MF |
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 | |
75efb018 MF |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
d5efd131 MF |
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 | /* Swap bytes in 16 bit value. */ | |
27 | #define __bswap_constant_16(x) \ | |
0c41943a | 28 | ((unsigned short int)((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) |
d5efd131 | 29 | |
756b23ec MF |
30 | /* Get __bswap_16. */ |
31 | #include <bits/byteswap-16.h> | |
d5efd131 MF |
32 | |
33 | /* Swap bytes in 32 bit value. */ | |
34 | #define __bswap_constant_32(x) \ | |
35 | ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ | |
36 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) | |
37 | ||
38 | #if defined __GNUC__ && __GNUC__ >= 2 | |
39 | # define __bswap_32(x) \ | |
40 | (__extension__ \ | |
2e09a79a | 41 | ({ unsigned int __v, __x = (x); \ |
d5efd131 MF |
42 | if (__builtin_constant_p (x)) \ |
43 | __v = __bswap_constant_32 (__x); \ | |
44 | else \ | |
45 | __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \ | |
46 | "mux1 %0 = %0, @rev ;;" \ | |
47 | : "=r" (__v) \ | |
48 | : "r" ((unsigned int) (__x))); \ | |
49 | __v; })) | |
50 | #else | |
51 | static __inline unsigned int | |
52 | __bswap_32 (unsigned int __bsx) | |
53 | { | |
54 | return __bswap_constant_32 (__bsx); | |
55 | } | |
56 | #endif | |
57 | ||
58 | ||
59 | /* Swap bytes in 64 bit value. */ | |
60 | #if defined __GNUC__ && __GNUC__ >= 2 | |
61 | # define __bswap_constant_64(x) \ | |
62 | (__extension__ ((((x) & 0xff00000000000000ul) >> 56) \ | |
63 | | (((x) & 0x00ff000000000000ul) >> 40) \ | |
64 | | (((x) & 0x0000ff0000000000ul) >> 24) \ | |
65 | | (((x) & 0x000000ff00000000ul) >> 8) \ | |
66 | | (((x) & 0x00000000ff000000ul) << 8) \ | |
67 | | (((x) & 0x0000000000ff0000ul) << 24) \ | |
68 | | (((x) & 0x000000000000ff00ul) << 40) \ | |
69 | | (((x) & 0x00000000000000fful) << 56))) | |
70 | ||
71 | # define __bswap_64(x) \ | |
72 | (__extension__ \ | |
2e09a79a | 73 | ({ unsigned long int __v, __x = (x); \ |
d5efd131 MF |
74 | if (__builtin_constant_p (x)) \ |
75 | __v = __bswap_constant_64 (__x); \ | |
76 | else \ | |
77 | __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \ | |
78 | : "=r" (__v) \ | |
79 | : "r" ((unsigned long int) (__x))); \ | |
80 | __v; })) | |
81 | ||
82 | #else | |
83 | # define __bswap_constant_64(x) \ | |
84 | ((((x) & 0xff00000000000000ul) >> 56) \ | |
85 | | (((x) & 0x00ff000000000000ul) >> 40) \ | |
86 | | (((x) & 0x0000ff0000000000ul) >> 24) \ | |
87 | | (((x) & 0x000000ff00000000ul) >> 8) \ | |
88 | | (((x) & 0x00000000ff000000ul) << 8) \ | |
89 | | (((x) & 0x0000000000ff0000ul) << 24) \ | |
90 | | (((x) & 0x000000000000ff00ul) << 40) \ | |
91 | | (((x) & 0x00000000000000fful) << 56)) | |
92 | ||
93 | static __inline unsigned long int | |
94 | __bswap_64 (unsigned long int __bsx) | |
95 | { | |
96 | return __bswap_constant_64 (__bsx); | |
97 | } | |
98 | #endif | |
99 | ||
100 | #endif /* _BITS_BYTESWAP_H */ |