Prevent double inclusion.
[kopensolaris-gnu/glibc.git] / sysdeps / s390 / s390-32 / bits / byteswap.h
1 /* Macros to swap the order of bytes in integer values.  s390 version.
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
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
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
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
22 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
23 #endif
24
25 #ifndef _BITS_BYTESWAP_H
26 #define _BITS_BYTESWAP_H 1
27
28 #define __bswap_constant_16(x) \
29      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
30
31 /* Swap bytes in 16 bit value. */
32 #if defined __GNUC__ && __GNUC__ >= 2
33 # define __bswap_16(x) \
34      (__extension__                                                           \
35       ({ unsigned short int __v;                                              \
36          if (__builtin_constant_p (x))                                        \
37            __v = __bswap_constant_16 (x);                                     \
38          else {                                                               \
39            unsigned short int __tmp = (unsigned short int) (x);               \
40            __asm__ __volatile__ (                                             \
41               "sr   %0,%0\n"                                                  \
42               "la   1,%1\n"                                                   \
43               "icm  %0,2,1(1)\n"                                              \
44               "ic   %0,0(1)"                                                  \
45               : "=&d" (__v) : "m" (__tmp) : "1");                             \
46          }                                                                    \
47          __v; }))
48 #else
49 /* This is better than nothing.  */
50 #define __bswap_16(x) __bswap_constant_16 (x)
51 #endif
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       ({ unsigned int __v;                                                    \
62          if (__builtin_constant_p (x))                                        \
63            __v = __bswap_constant_32 (x);                                     \
64          else {                                                               \
65            unsigned int __tmp = (unsigned int) (x);                           \
66            __asm__ __volatile__ (                                             \
67               "la    1,%1\n"                                                  \
68               "icm   %0,8,3(1)\n"                                             \
69               "icm   %0,4,2(1)\n"                                             \
70               "icm   %0,2,1(1)\n"                                             \
71               "ic    %0,0(1)"                                                 \
72               : "=&d" (__v) : "m" (__tmp) : "1");                             \
73          }                                                                    \
74          __v; }))
75 #else
76 # define __bswap_32(x) __bswap_constant_32 (x)
77 #endif
78
79 #if defined __GNUC__ && __GNUC__ >= 2
80 /* Swap bytes in 64 bit value.  */
81 # define __bswap_64(x) \
82   __extension__                                         \
83   ({ union { unsigned long long int __ll;               \
84              unsigned long int __l[2]; } __w, __r;      \
85      __w.__ll = (x);                                    \
86      __r.__l[0] = __bswap_32 (__w.__l[1]);              \
87      __r.__l[1] = __bswap_32 (__w.__l[0]);              \
88      __r.__ll; })
89 #endif
90
91 #endif /* _BITS_BYTESWAP_H */