Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / m68k / bits / byteswap.h
blob9e7fd876157295b4a6cba159a02e2e5d0c33f4d5
1 /* Macros to swap the order of bytes in integer values. m68k version.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
19 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
20 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
21 #endif
23 #ifndef _BITS_BYTESWAP_H
24 #define _BITS_BYTESWAP_H 1
26 /* Swap bytes in 16 bit value. We don't provide an assembler version
27 because GCC is smart enough to generate optimal assembler output, and
28 this allows for better cse. */
29 #define __bswap_constant_16(x) \
30 ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
32 static __inline unsigned short int
33 __bswap_16 (unsigned short int __bsx)
35 return __bswap_constant_16 (__bsx);
38 /* Swap bytes in 32 bit value. */
39 #define __bswap_constant_32(x) \
40 ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
41 (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
43 #if !defined(__mcoldfire__)
44 static __inline unsigned int
45 __bswap_32 (unsigned int __bsx)
47 if (__builtin_constant_p (__bsx))
48 return __bswap_constant_32 (__bsx);
49 __asm__ __volatile__ ("ror%.w %#8, %0;"
50 "swap %0;"
51 "ror%.w %#8, %0"
52 : "+d" (__bsx));
53 return __bsx;
55 #else
56 static __inline unsigned int
57 __bswap_32 (unsigned int __bsx)
59 return __bswap_constant_32 (__bsx);
61 #endif
63 #if defined __GNUC__ && __GNUC__ >= 2
64 /* Swap bytes in 64 bit value. */
65 # define __bswap_constant_64(x) \
66 __extension__ \
67 ((((x) & 0xff00000000000000ull) >> 56) \
68 | (((x) & 0x00ff000000000000ull) >> 40) \
69 | (((x) & 0x0000ff0000000000ull) >> 24) \
70 | (((x) & 0x000000ff00000000ull) >> 8) \
71 | (((x) & 0x00000000ff000000ull) << 8) \
72 | (((x) & 0x0000000000ff0000ull) << 24) \
73 | (((x) & 0x000000000000ff00ull) << 40) \
74 | (((x) & 0x00000000000000ffull) << 56))
76 /* Swap bytes in 64 bit value. */
77 __extension__
78 static __inline unsigned long long
79 __bswap_64 (unsigned long long __bsx)
81 if (__builtin_constant_p (__bsx))
82 return __bswap_constant_64 (__bsx);
83 return (__bswap_32 (__bsx >> 32)
84 | ((unsigned long long) __bswap_32 (__bsx) << 32));
86 #endif
88 #endif /* _BITS_BYTESWAP_H */