GnuCOBOL  2.0
A free COBOL compiler
byteswap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, 51 Franklin Street, Fifth Floor
17  * Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Extracted from glib/gtypes.h in GLIB-2.2.2.
22  * Modified by Roger While
23  */
24 
25 #ifndef VB_BYTESWAP_H
26 #define VB_BYTESWAP_H
27 
28 #include <sys/types.h>
29 
30 /* Basic bit swapping functions
31  */
32 
33 #define VB_BSWAP_16_CONSTANT(val) ((unsigned short) ( \
34  (unsigned short) ((unsigned short) (val) >> 8) | \
35  (unsigned short) ((unsigned short) (val) << 8)))
36 
37 #define VB_BSWAP_32_CONSTANT(val) ((unsigned int) ( \
38  (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
39  (((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \
40  (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \
41  (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
42 
43 #define VB_BSWAP_64_CONSTANT(val) ((unsigned long long) ( \
44  (((unsigned long long) (val) & \
45  (unsigned long long) 0x00000000000000ffULL) << 56) | \
46  (((unsigned long long) (val) & \
47  (unsigned long long) 0x000000000000ff00ULL) << 40) | \
48  (((unsigned long long) (val) & \
49  (unsigned long long) 0x0000000000ff0000ULL) << 24) | \
50  (((unsigned long long) (val) & \
51  (unsigned long long) 0x00000000ff000000ULL) << 8) | \
52  (((unsigned long long) (val) & \
53  (unsigned long long) 0x000000ff00000000ULL) >> 8) | \
54  (((unsigned long long) (val) & \
55  (unsigned long long) 0x0000ff0000000000ULL) >> 24) | \
56  (((unsigned long long) (val) & \
57  (unsigned long long) 0x00ff000000000000ULL) >> 40) | \
58  (((unsigned long long) (val) & \
59  (unsigned long long) 0xff00000000000000ULL) >> 56)))
60 
61 /* Arch specific stuff for speed */
62 
63 #if defined (__GNUC__) && (__GNUC__ >= 2)
64 # if defined (__i386__)
65 # define VB_BSWAP_16_IA32(val) \
66  (__extension__ \
67  ({ register unsigned short int __v, __x = ((unsigned short) (val)); \
68  if (__builtin_constant_p (__x)) \
69  __v = VB_BSWAP_16_CONSTANT (__x); \
70  else \
71  __asm__ ("rorw $8, %w0" \
72  : "=r" (__v) \
73  : "0" (__x) \
74  : "cc"); \
75  __v; }))
76 # define VB_BSWAP_32_IA32(val) \
77  (__extension__ \
78  ({ register unsigned int __v, __x = ((unsigned int) (val)); \
79  if (__builtin_constant_p (__x)) \
80  __v = VB_BSWAP_32_CONSTANT (__x); \
81  else \
82  __asm__ ("bswap %0" \
83  : "=r" (__v) \
84  : "0" (__x)); \
85  __v; }))
86 # define VB_BSWAP_64_IA32(val) \
87  (__extension__ \
88  ({ union { unsigned long long __ll; \
89  unsigned int __l[2]; } __w, __r; \
90  __w.__ll = ((unsigned long long) (val)); \
91  if (__builtin_constant_p (__w.__ll)) \
92  __r.__ll = VB_BSWAP_64_CONSTANT (__w.__ll); \
93  else \
94  { \
95  __r.__l[0] = VB_BSWAP_32 (__w.__l[1]); \
96  __r.__l[1] = VB_BSWAP_32 (__w.__l[0]); \
97  } \
98  __r.__ll; }))
99 # define VB_BSWAP_16(val) (VB_BSWAP_16_IA32 (val))
100 # define VB_BSWAP_32(val) (VB_BSWAP_32_IA32 (val))
101 # define VB_BSWAP_64(val) (VB_BSWAP_64_IA32 (val))
102 # elif defined (__ia64__)
103 # define VB_BSWAP_16_IA64(val) \
104  (__extension__ \
105  ({ register unsigned short __v, __x = ((unsigned short) (val)); \
106  if (__builtin_constant_p (__x)) \
107  __v = VB_BSWAP_16_CONSTANT (__x); \
108  else \
109  __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
110  "mux1 %0 = %0, @rev ;;" \
111  : "=r" (__v) \
112  : "r" (__x)); \
113  __v; }))
114 # define VB_BSWAP_32_IA64(val) \
115  (__extension__ \
116  ({ register unsigned int __v, __x = ((unsigned int) (val)); \
117  if (__builtin_constant_p (__x)) \
118  __v = VB_BSWAP_32_CONSTANT (__x); \
119  else \
120  __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
121  "mux1 %0 = %0, @rev ;;" \
122  : "=r" (__v) \
123  : "r" (__x)); \
124  __v; }))
125 # define VB_BSWAP_64_IA64(val) \
126  (__extension__ \
127  ({ register unsigned long long __v, \
128  __x = ((unsigned long long) (val)); \
129  if (__builtin_constant_p (__x)) \
130  __v = VB_BSWAP_64_CONSTANT (__x); \
131  else \
132  __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
133  : "=r" (__v) \
134  : "r" (__x)); \
135  __v; }))
136 # define VB_BSWAP_16(val) (VB_BSWAP_16_IA64 (val))
137 # define VB_BSWAP_32(val) (VB_BSWAP_32_IA64 (val))
138 # define VB_BSWAP_64(val) (VB_BSWAP_64_IA64 (val))
139 # elif defined (__x86_64__)
140 # define VB_BSWAP_16_X86_64(val) \
141  (__extension__ \
142  ({ register unsigned short int __v, __x = ((unsigned short) (val)); \
143  if (__builtin_constant_p (__x)) \
144  __v = VB_BSWAP_16_CONSTANT (__x); \
145  else \
146  __asm__ ("rorw $8, %w0" \
147  : "=r" (__v) \
148  : "0" (__x) \
149  : "cc"); \
150  __v; }))
151 # define VB_BSWAP_32_X86_64(val) \
152  (__extension__ \
153  ({ register unsigned int __v, __x = ((unsigned int) (val)); \
154  if (__builtin_constant_p (__x)) \
155  __v = VB_BSWAP_32_CONSTANT (__x); \
156  else \
157  __asm__ ("bswapl %0" \
158  : "=r" (__v) \
159  : "0" (__x)); \
160  __v; }))
161 # define VB_BSWAP_64_X86_64(val) \
162  (__extension__ \
163  ({ register unsigned long long __v, __x = ((unsigned long long) (val)); \
164  if (__builtin_constant_p (__x)) \
165  __v = VB_BSWAP_64_CONSTANT (__x); \
166  else \
167  __asm__ ("bswapq %0" \
168  : "=r" (__v) \
169  : "0" (__x)); \
170  __v; }))
171 # define VB_BSWAP_16(val) (VB_BSWAP_16_X86_64 (val))
172 # define VB_BSWAP_32(val) (VB_BSWAP_32_X86_64 (val))
173 # define VB_BSWAP_64(val) (VB_BSWAP_64_X86_64 (val))
174 # else /* generic gcc */
175 # define VB_BSWAP_16(val) (VB_BSWAP_16_CONSTANT (val))
176 # define VB_BSWAP_32(val) (VB_BSWAP_32_CONSTANT (val))
177 # define VB_BSWAP_64(val) (VB_BSWAP_64_CONSTANT (val))
178 # endif
179 # elif defined(_MSC_VER)
180 # define VB_BSWAP_16(val) (_byteswap_ushort (val))
181 # define VB_BSWAP_32(val) (_byteswap_ulong (val))
182 # define VB_BSWAP_64(val) (_byteswap_uint64 (val))
183 #else /* generic */
184 # define VB_BSWAP_16(val) (VB_BSWAP_16_CONSTANT (val))
185 # define VB_BSWAP_32(val) (VB_BSWAP_32_CONSTANT (val))
186 # define VB_BSWAP_64(val) (VB_BSWAP_64_CONSTANT (val))
187 #endif /* generic */
188 
189 #endif /* VB_BYTESWAP_H */