]>
Commit | Line | Data |
---|---|---|
d5efd131 MF |
1 | /* Macros to swap the order of bytes in integer values. |
2 | Copyright (C) 1997,1998,2000,2002,2003,2008,2011 | |
3 | Free Software Foundation, Inc. | |
4 | This file is part of the GNU C Library. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
75efb018 MF |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
d5efd131 MF |
19 | |
20 | #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H | |
21 | # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead." | |
22 | #endif | |
23 | ||
24 | #ifndef _BITS_BYTESWAP_H | |
25 | #define _BITS_BYTESWAP_H 1 | |
26 | ||
27 | /* Swap bytes in 16 bit value. */ | |
28 | #define __bswap_constant_16(x) \ | |
29 | ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) | |
30 | ||
31 | #if defined __GNUC__ && __GNUC__ >= 2 | |
32 | # define __bswap_16(x) \ | |
33 | (__extension__ \ | |
34 | ({ register unsigned short int __v, __x = (x); \ | |
35 | if (__builtin_constant_p (x)) \ | |
36 | __v = __bswap_constant_16 (__x); \ | |
37 | else \ | |
38 | __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \ | |
39 | "mux1 %0 = %0, @rev ;;" \ | |
40 | : "=r" (__v) \ | |
41 | : "r" ((unsigned short int) (__x))); \ | |
42 | __v; })) | |
43 | #else | |
44 | /* This is better than nothing. */ | |
45 | static __inline unsigned short int | |
46 | __bswap_16 (unsigned short int __bsx) | |
47 | { | |
48 | return __bswap_constant_16 (__bsx); | |
49 | } | |
50 | #endif | |
51 | ||
52 | ||
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)) | |
57 | ||
58 | #if defined __GNUC__ && __GNUC__ >= 2 | |
59 | # define __bswap_32(x) \ | |
60 | (__extension__ \ | |
61 | ({ register unsigned int __v, __x = (x); \ | |
62 | if (__builtin_constant_p (x)) \ | |
63 | __v = __bswap_constant_32 (__x); \ | |
64 | else \ | |
65 | __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \ | |
66 | "mux1 %0 = %0, @rev ;;" \ | |
67 | : "=r" (__v) \ | |
68 | : "r" ((unsigned int) (__x))); \ | |
69 | __v; })) | |
70 | #else | |
71 | static __inline unsigned int | |
72 | __bswap_32 (unsigned int __bsx) | |
73 | { | |
74 | return __bswap_constant_32 (__bsx); | |
75 | } | |
76 | #endif | |
77 | ||
78 | ||
79 | /* Swap bytes in 64 bit value. */ | |
80 | #if defined __GNUC__ && __GNUC__ >= 2 | |
81 | # define __bswap_constant_64(x) \ | |
82 | (__extension__ ((((x) & 0xff00000000000000ul) >> 56) \ | |
83 | | (((x) & 0x00ff000000000000ul) >> 40) \ | |
84 | | (((x) & 0x0000ff0000000000ul) >> 24) \ | |
85 | | (((x) & 0x000000ff00000000ul) >> 8) \ | |
86 | | (((x) & 0x00000000ff000000ul) << 8) \ | |
87 | | (((x) & 0x0000000000ff0000ul) << 24) \ | |
88 | | (((x) & 0x000000000000ff00ul) << 40) \ | |
89 | | (((x) & 0x00000000000000fful) << 56))) | |
90 | ||
91 | # define __bswap_64(x) \ | |
92 | (__extension__ \ | |
93 | ({ register unsigned long int __v, __x = (x); \ | |
94 | if (__builtin_constant_p (x)) \ | |
95 | __v = __bswap_constant_64 (__x); \ | |
96 | else \ | |
97 | __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \ | |
98 | : "=r" (__v) \ | |
99 | : "r" ((unsigned long int) (__x))); \ | |
100 | __v; })) | |
101 | ||
102 | #else | |
103 | # define __bswap_constant_64(x) \ | |
104 | ((((x) & 0xff00000000000000ul) >> 56) \ | |
105 | | (((x) & 0x00ff000000000000ul) >> 40) \ | |
106 | | (((x) & 0x0000ff0000000000ul) >> 24) \ | |
107 | | (((x) & 0x000000ff00000000ul) >> 8) \ | |
108 | | (((x) & 0x00000000ff000000ul) << 8) \ | |
109 | | (((x) & 0x0000000000ff0000ul) << 24) \ | |
110 | | (((x) & 0x000000000000ff00ul) << 40) \ | |
111 | | (((x) & 0x00000000000000fful) << 56)) | |
112 | ||
113 | static __inline unsigned long int | |
114 | __bswap_64 (unsigned long int __bsx) | |
115 | { | |
116 | return __bswap_constant_64 (__bsx); | |
117 | } | |
118 | #endif | |
119 | ||
120 | #endif /* _BITS_BYTESWAP_H */ |