Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / s390 / bits / byteswap.h
blobaf6eb45dc0c8b2e33a1eb90688dbd8dfbcac11c4
1 /* Macros to swap the order of bytes in integer values. s390 version.
2 Copyright (C) 2000-2015 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."
22 #endif
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)))
32 /* Get __bswap_16. */
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
41 # if __WORDSIZE == 64
42 # define __bswap_32(x) \
43 (__extension__ \
44 ({ unsigned int __v, __x = (x); \
45 if (__builtin_constant_p (x)) \
46 __v = __bswap_constant_32 (__x); \
47 else { \
48 unsigned int __tmp = (unsigned int) (__x); \
49 __asm__ __volatile__ ( \
50 "lrv %0,%1" \
51 : "=&d" (__v) : "m" (__tmp)); \
52 } \
53 __v; }))
54 # else
55 # define __bswap_32(x) \
56 (__extension__ \
57 ({ unsigned int __v, __x = (x); \
58 if (__builtin_constant_p (x)) \
59 __v = __bswap_constant_32 (__x); \
60 else { \
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 } \
70 __v; }))
71 # endif
72 #else
73 static __inline unsigned int
74 __bswap_32 (unsigned int __bsx)
76 return __bswap_constant_32 (__bsx);
78 #endif
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)))
92 # if __WORDSIZE == 64
93 # define __bswap_64(x) \
94 (__extension__ \
95 ({ unsigned long __w, __x = (x); \
96 if (__builtin_constant_p (x)) \
97 __w = __bswap_constant_64 (__x); \
98 else { \
99 unsigned long __tmp = (unsigned long) (__x); \
100 __asm__ __volatile__ ( \
101 "lrvg %0,%1" \
102 : "=&d" (__w) : "m" (__tmp)); \
104 __w; }))
105 # else
106 # define __bswap_64(x) \
107 __extension__ \
108 ({ union { unsigned long long int __ll; \
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; })
114 # endif
115 #else
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))
126 __extension__
127 static __inline unsigned long long int
128 __bswap_64 (unsigned long long int __bsx)
130 return __bswap_constant_64 (__bsx);
132 #endif
134 #endif /* _BITS_BYTESWAP_H */