]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/bits/byteswap.h
1 /* Macros to swap the order of bytes in integer values. s390 version.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
4 This file is part of the GNU C Library.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
21 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
24 #include <bits/wordsize.h>
26 #ifndef _BITS_BYTESWAP_H
27 #define _BITS_BYTESWAP_H 1
29 #define __bswap_constant_16(x) \
30 ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
33 #include <bits/byteswap-16.h>
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))
40 #if defined __GNUC__ && __GNUC__ >= 2
42 # define __bswap_32(x) \
44 ({ unsigned int __v, __x = (x); \
45 if (__builtin_constant_p (x)) \
46 __v = __bswap_constant_32 (__x); \
48 unsigned int __tmp = (unsigned int) (__x); \
49 __asm__ __volatile__ ( \
51 : "=&d" (__v) : "m" (__tmp)); \
55 # define __bswap_32(x) \
57 ({ unsigned int __v, __x = (x); \
58 if (__builtin_constant_p (x)) \
59 __v = __bswap_constant_32 (__x); \
61 unsigned int __tmp = (unsigned int) (__x); \
62 __asm__ __volatile__ ( \
68 : "=&d" (__v) : "m" (__tmp) : "1"); \
73 static __inline
unsigned int
74 __bswap_32 (unsigned int __bsx
)
76 return __bswap_constant_32 (__bsx
);
80 /* Swap bytes in 64 bit value. */
81 #if defined __GNUC__ && __GNUC__ >= 2
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)))
93 # define __bswap_64(x) \
95 ({ unsigned long __w, __x = (x); \
96 if (__builtin_constant_p (x)) \
97 __w = __bswap_constant_64 (__x); \
99 unsigned long __tmp = (unsigned long) (__x); \
100 __asm__ __volatile__ ( \
102 : "=&d" (__w) : "m" (__tmp)); \
106 # define __bswap_64(x) \
108 ({ union { unsigned long long int __ll; \
109 unsigned long int __l[2]; } __w, __r; \
111 __r.__l[0] = __bswap_32 (__w.__l[1]); \
112 __r.__l[1] = __bswap_32 (__w.__l[0]); \
116 # define __bswap_constant_64(x) \
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))
127 static __inline
unsigned long long int
128 __bswap_64 (unsigned long long int __bsx
)
130 return __bswap_constant_64 (__bsx
);
134 #endif /* _BITS_BYTESWAP_H */