]>
Commit | Line | Data |
---|---|---|
7bf75551 | 1 | /* Macros to swap the order of bytes in integer values. s390 version. |
f7a9f785 | 2 | Copyright (C) 2000-2016 Free Software Foundation, Inc. |
847b055c AJ |
3 | Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com). |
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 | |
41bdb6e2 AJ |
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. | |
847b055c AJ |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
847b055c | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
847b055c | 19 | |
2f9a1be8 | 20 | #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H |
847b055c AJ |
21 | # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead." |
22 | #endif | |
23 | ||
7bf75551 UD |
24 | #include <bits/wordsize.h> |
25 | ||
5c26fecb AJ |
26 | #ifndef _BITS_BYTESWAP_H |
27 | #define _BITS_BYTESWAP_H 1 | |
28 | ||
847b055c | 29 | #define __bswap_constant_16(x) \ |
2174c6dd | 30 | ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) |
847b055c | 31 | |
f8887d0a L |
32 | /* Get __bswap_16. */ |
33 | #include <bits/byteswap-16.h> | |
847b055c AJ |
34 | |
35 | /* Swap bytes in 32 bit value. */ | |
36 | #define __bswap_constant_32(x) \ | |
37 | ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ | |
38 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) | |
39 | ||
40 | #if defined __GNUC__ && __GNUC__ >= 2 | |
7bf75551 | 41 | # if __WORDSIZE == 64 |
847b055c AJ |
42 | # define __bswap_32(x) \ |
43 | (__extension__ \ | |
67371b56 | 44 | ({ unsigned int __v, __x = (x); \ |
847b055c | 45 | if (__builtin_constant_p (x)) \ |
b5facfda | 46 | __v = __bswap_constant_32 (__x); \ |
847b055c | 47 | else { \ |
67371b56 UD |
48 | unsigned int __tmp = (unsigned int) (__x); \ |
49 | __asm__ __volatile__ ( \ | |
50 | "lrv %0,%1" \ | |
51 | : "=&d" (__v) : "m" (__tmp)); \ | |
52 | } \ | |
847b055c | 53 | __v; })) |
7bf75551 UD |
54 | # else |
55 | # define __bswap_32(x) \ | |
56 | (__extension__ \ | |
67371b56 | 57 | ({ unsigned int __v, __x = (x); \ |
7bf75551 | 58 | if (__builtin_constant_p (x)) \ |
b5facfda | 59 | __v = __bswap_constant_32 (__x); \ |
7bf75551 | 60 | else { \ |
67371b56 UD |
61 | unsigned int __tmp = (unsigned int) (__x); \ |
62 | __asm__ __volatile__ ( \ | |
63 | "la 1,%1\n" \ | |
64 | "icm %0,8,3(1)\n" \ | |
65 | "icm %0,4,2(1)\n" \ | |
66 | "icm %0,2,1(1)\n" \ | |
67 | "ic %0,0(1)" \ | |
68 | : "=&d" (__v) : "m" (__tmp) : "1"); \ | |
69 | } \ | |
7bf75551 UD |
70 | __v; })) |
71 | # endif | |
847b055c | 72 | #else |
b5facfda UD |
73 | static __inline unsigned int |
74 | __bswap_32 (unsigned int __bsx) | |
75 | { | |
76 | return __bswap_constant_32 (__bsx); | |
77 | } | |
847b055c AJ |
78 | #endif |
79 | ||
847b055c | 80 | /* Swap bytes in 64 bit value. */ |
ffeac417 | 81 | #if defined __GNUC__ && __GNUC__ >= 2 |
67371b56 UD |
82 | # define __bswap_constant_64(x) \ |
83 | (__extension__ ((((x) & 0xff00000000000000ul) >> 56) \ | |
84 | | (((x) & 0x00ff000000000000ul) >> 40) \ | |
85 | | (((x) & 0x0000ff0000000000ul) >> 24) \ | |
86 | | (((x) & 0x000000ff00000000ul) >> 8) \ | |
87 | | (((x) & 0x00000000ff000000ul) << 8) \ | |
88 | | (((x) & 0x0000000000ff0000ul) << 24) \ | |
89 | | (((x) & 0x000000000000ff00ul) << 40) \ | |
90 | | (((x) & 0x00000000000000fful) << 56))) | |
91 | ||
7bf75551 | 92 | # if __WORDSIZE == 64 |
ffeac417 UD |
93 | # define __bswap_64(x) \ |
94 | (__extension__ \ | |
67371b56 | 95 | ({ unsigned long __w, __x = (x); \ |
ffeac417 | 96 | if (__builtin_constant_p (x)) \ |
b5facfda | 97 | __w = __bswap_constant_64 (__x); \ |
ffeac417 | 98 | else { \ |
67371b56 UD |
99 | unsigned long __tmp = (unsigned long) (__x); \ |
100 | __asm__ __volatile__ ( \ | |
101 | "lrvg %0,%1" \ | |
102 | : "=&d" (__w) : "m" (__tmp)); \ | |
103 | } \ | |
ffeac417 | 104 | __w; })) |
7bf75551 UD |
105 | # else |
106 | # define __bswap_64(x) \ | |
107 | __extension__ \ | |
108 | ({ union { unsigned long long int __ll; \ | |
67371b56 UD |
109 | unsigned long int __l[2]; } __w, __r; \ |
110 | __w.__ll = (x); \ | |
111 | __r.__l[0] = __bswap_32 (__w.__l[1]); \ | |
112 | __r.__l[1] = __bswap_32 (__w.__l[0]); \ | |
113 | __r.__ll; }) | |
7bf75551 | 114 | # endif |
6a57d931 | 115 | #else |
67371b56 | 116 | # define __bswap_constant_64(x) \ |
b1aa60f3 AJ |
117 | ((((x) & 0xff00000000000000ull) >> 56) \ |
118 | | (((x) & 0x00ff000000000000ull) >> 40) \ | |
119 | | (((x) & 0x0000ff0000000000ull) >> 24) \ | |
120 | | (((x) & 0x000000ff00000000ull) >> 8) \ | |
121 | | (((x) & 0x00000000ff000000ull) << 8) \ | |
122 | | (((x) & 0x0000000000ff0000ull) << 24) \ | |
123 | | (((x) & 0x000000000000ff00ull) << 40) \ | |
124 | | (((x) & 0x00000000000000ffull) << 56)) | |
67371b56 | 125 | |
828beb13 | 126 | __extension__ |
b5facfda UD |
127 | static __inline unsigned long long int |
128 | __bswap_64 (unsigned long long int __bsx) | |
129 | { | |
130 | return __bswap_constant_64 (__bsx); | |
131 | } | |
847b055c | 132 | #endif |
ffeac417 | 133 | |
5c26fecb | 134 | #endif /* _BITS_BYTESWAP_H */ |