Debian Codegraph @

share/libc/byteswap.h

raw
1 /**************************************************************************/
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /**************************************************************************/
22
23 /* Copyright (C) 1997 Free Software Foundation, Inc.
24 This file is part of the GNU C Library.
25
26 The GNU C Library is free software; you can redistribute it and/or
27 modify it under the terms of the GNU Lesser General Public
28 License as published by the Free Software Foundation; either
29 version 2.1 of the License, or (at your option) any later version.
30
31 The GNU C Library is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 Lesser General Public License for more details.
35
36 You should have received a copy of the GNU Lesser General Public
37 License along with the GNU C Library; if not, write to the Free
38 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
39 02111-1307 USA. */
40
41 #ifndef _BYTESWAP_H
42 #define _BYTESWAP_H 1
43
44
45
46 /* Get the machine specific, optimized definitions. */
47
48 //#include <bits/wordsize.h>
49
50 /* Swap bytes in 16 bit value. */
51 #define __bswap_constant_16(x) \
52 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
53
54 /* This is better than nothing. */
55 # define __bswap_16(x) \
56 (__extension__ \
57 ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
58
59
60 /* Swap bytes in 32 bit value. */
61 #define __bswap_constant_32(x) \
62 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
63 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
64
65 # define __bswap_32(x) \
66 (__extension__ \
67 ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
68
69
70 /* Swap bytes in 64 bit value. */
71 # define __bswap_constant_64(x) \
72 ((((x) & 0xff00000000000000ull) >> 56) \
73 | (((x) & 0x00ff000000000000ull) >> 40) \
74 | (((x) & 0x0000ff0000000000ull) >> 24) \
75 | (((x) & 0x000000ff00000000ull) >> 8) \
76 | (((x) & 0x00000000ff000000ull) << 8) \
77 | (((x) & 0x0000000000ff0000ull) << 24) \
78 | (((x) & 0x000000000000ff00ull) << 40) \
79 | (((x) & 0x00000000000000ffull) << 56))
80
81 # define __bswap_64(x) \
82 (__extension__ \
83 ({ union __fc_bswap { __extension__ unsigned long long int __ll; \
84 unsigned int __l[2]; } __w, __r; \
85 if (__builtin_constant_p (x)) \
86 __r.__ll = __bswap_constant_64 (x); \
87 else \
88 { \
89 __w.__ll = (x); \
90 __r.__l[0] = __bswap_32 (__w.__l[1]); \
91 __r.__l[1] = __bswap_32 (__w.__l[0]); \
92 } \
93 __r.__ll; }))
94
95
96 /* The following definitions must all be macros since otherwise some
97 of the possible optimizations are not possible. */
98
99 /* Return a value with all bytes in the 16 bit argument swapped. */
100 #define bswap_16(x) __bswap_16 (x)
101
102 /* Return a value with all bytes in the 32 bit argument swapped. */
103 #define bswap_32(x) __bswap_32 (x)
104
105 #if defined __GNUC__ && __GNUC__ >= 2
106 /* Return a value with all bytes in the 64 bit argument swapped. */
107 # define bswap_64(x) __bswap_64 (x)
108 #endif
109
110 #endif /* byteswap.h */
111
112