]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/m68k/bits/byteswap.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / m68k / bits / byteswap.h
CommitLineData
c093785f 1/* Macros to swap the order of bytes in integer values. m68k version.
b168057a 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
c093785f
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
3214b89b
AJ
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.
c093785f
UD
9
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
3214b89b 13 Lesser General Public License for more details.
c093785f 14
3214b89b 15 You should have received a copy of the GNU Lesser General Public
ab84e3ff
PE
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
c093785f 18
d0a39e1d 19#if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
90571408
UD
20# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
21#endif
c093785f 22
5374babe
AJ
23#ifndef _BITS_BYTESWAP_H
24#define _BITS_BYTESWAP_H 1
25
c093785f
UD
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. */
d0a39e1d 29#define __bswap_constant_16(x) \
8a70b2dc 30 ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
d0a39e1d 31
d0a39e1d
AS
32static __inline unsigned short int
33__bswap_16 (unsigned short int __bsx)
34{
35 return __bswap_constant_16 (__bsx);
36}
c093785f
UD
37
38/* Swap bytes in 32 bit value. */
39#define __bswap_constant_32(x) \
8a70b2dc
AS
40 ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
41 (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
c093785f 42
fb3ed187
AS
43#if !defined(__mcoldfire__)
44static __inline unsigned int
45__bswap_32 (unsigned int __bsx)
46{
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;
54}
c093785f 55#else
d0a39e1d
AS
56static __inline unsigned int
57__bswap_32 (unsigned int __bsx)
58{
59 return __bswap_constant_32 (__bsx);
60}
c093785f
UD
61#endif
62
63#if defined __GNUC__ && __GNUC__ >= 2
d0a39e1d
AS
64/* Swap bytes in 64 bit value. */
65# define __bswap_constant_64(x) \
8a70b2dc
AS
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))
d0a39e1d 75
c093785f 76/* Swap bytes in 64 bit value. */
828beb13 77__extension__
fb3ed187
AS
78static __inline unsigned long long
79__bswap_64 (unsigned long long __bsx)
80{
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));
85}
c093785f 86#endif
5374babe
AJ
87
88#endif /* _BITS_BYTESWAP_H */