Update.
[glibc.git] / sysdeps / ia64 / bits / byteswap.h
blobd728b64c2c756a3838ca8d6f40b0c0a98f55e5a8
1 /* Macros to swap the order of bytes in integer values.
2 Copyright (C) 1997, 1998, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
21 # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
22 #endif
24 /* Swap bytes in 16 bit value. */
25 #define __bswap_constant_16(x) \
26 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
28 #if defined __GNUC__ && __GNUC__ >= 2
29 # define __bswap_16(x) \
30 (__extension__ \
31 ({ register unsigned short int __v; \
32 if (__builtin_constant_p (x)) \
33 __v = __bswap_constant_16 (x); \
34 else \
35 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
36 "mux1 %0 = %2, @rev ;;" \
37 : "=r" (__v) \
38 : "r" ((unsigned short int) (x)), "0" (__v));\
39 __v; }))
40 #else
41 /* This is better than nothing. */
42 # define __bswap_16(x) __bswap_constant_16 (x)
43 #endif
46 /* Swap bytes in 32 bit value. */
47 #define __bswap_constant_32(x) \
48 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
49 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
51 #if defined __GNUC__ && __GNUC__ >= 2
52 # define __bswap_32(x) \
53 (__extension__ \
54 ({ register unsigned int __v; \
55 if (__builtin_constant_p (x)) \
56 __v = __bswap_constant_32 (x); \
57 else \
58 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
59 "mux1 %0 = %2, @rev ;;" \
60 : "=r" (__v) \
61 : "r" ((unsigned int) (x)), "0" (__v)); \
62 __v; }))
63 #else
64 # define __bswap_32(x) __bswap_constant_32 (x)
65 #endif
68 /* Swap bytes in 64 bit value. */
69 #define __bswap_constant_64(x) \
70 ((((x) & 0xff00000000000000ul) >> 56) \
71 | (((x) & 0x00ff000000000000ul) >> 40) \
72 | (((x) & 0x0000ff0000000000ul) >> 24) \
73 | (((x) & 0x000000ff00000000ul) >> 8) \
74 | (((x) & 0x00000000ff000000ul) << 8) \
75 | (((x) & 0x0000000000ff0000ul) << 24) \
76 | (((x) & 0x000000000000ff00ul) << 40) \
77 | (((x) & 0x00000000000000fful) << 56))
79 #if defined __GNUC__ && __GNUC__ >= 2
80 # define __bswap_64(x) \
81 (__extension__ \
82 ({ register unsigned long int __v; \
83 if (__builtin_constant_p (x)) \
84 __v = __bswap_constant_64 (x); \
85 else \
86 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
87 : "=r" (__v) \
88 : "r" ((unsigned long int) (x))); \
89 __v; }))
91 #else
92 # define __bswap_64(x) __bswap_constant_64 (x)
93 #endif