2.9
[glibc/nacl-glibc.git] / sysdeps / ia64 / bits / byteswap.h
blobd64914f36ef40a1fe8a587a388b8eca66d7720c1
1 /* Macros to swap the order of bytes in integer values.
2 Copyright (C) 1997,1998,2000,2002,2003,2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 #ifndef _BITS_BYTESWAP_H
25 #define _BITS_BYTESWAP_H 1
27 /* Swap bytes in 16 bit value. */
28 #define __bswap_constant_16(x) \
29 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
31 #if defined __GNUC__ && __GNUC__ >= 2
32 # define __bswap_16(x) \
33 (__extension__ \
34 ({ register unsigned short int __v, __x = (x); \
35 if (__builtin_constant_p (x)) \
36 __v = __bswap_constant_16 (__x); \
37 else \
38 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
39 "mux1 %0 = %0, @rev ;;" \
40 : "=r" (__v) \
41 : "r" ((unsigned short int) (__x))); \
42 __v; }))
43 #else
44 /* This is better than nothing. */
45 static __inline unsigned short int
46 __bswap_16 (unsigned short int __bsx)
48 return __bswap_constant_16 (__bsx);
50 #endif
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))
58 #if defined __GNUC__ && __GNUC__ >= 2
59 # define __bswap_32(x) \
60 (__extension__ \
61 ({ register unsigned int __v, __x = (x); \
62 if (__builtin_constant_p (x)) \
63 __v = __bswap_constant_32 (__x); \
64 else \
65 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
66 "mux1 %0 = %0, @rev ;;" \
67 : "=r" (__v) \
68 : "r" ((unsigned int) (__x))); \
69 __v; }))
70 #else
71 static __inline unsigned int
72 __bswap_32 (unsigned int __bsx)
74 return __bswap_constant_32 (__bsx);
76 #endif
79 /* Swap bytes in 64 bit value. */
80 #define __bswap_constant_64(x) \
81 ((((x) & 0xff00000000000000ul) >> 56) \
82 | (((x) & 0x00ff000000000000ul) >> 40) \
83 | (((x) & 0x0000ff0000000000ul) >> 24) \
84 | (((x) & 0x000000ff00000000ul) >> 8) \
85 | (((x) & 0x00000000ff000000ul) << 8) \
86 | (((x) & 0x0000000000ff0000ul) << 24) \
87 | (((x) & 0x000000000000ff00ul) << 40) \
88 | (((x) & 0x00000000000000fful) << 56))
90 #if defined __GNUC__ && __GNUC__ >= 2
91 # define __bswap_64(x) \
92 (__extension__ \
93 ({ register unsigned long int __v, __x = (x); \
94 if (__builtin_constant_p (x)) \
95 __v = __bswap_constant_64 (__x); \
96 else \
97 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
98 : "=r" (__v) \
99 : "r" ((unsigned long int) (__x))); \
100 __v; }))
102 #else
103 static __inline unsigned long int
104 __bswap_64 (unsigned long int __bsx)
106 return __bswap_constant_64 (__bsx);
108 #endif
110 #endif /* _BITS_BYTESWAP_H */