Wheefun I/O Library  0.0.5
Useful I/O Primitives.
byteswap.h
Go to the documentation of this file.
1 /* WFIO - the Wheefun IO Library
2  Copyright (C) 2018 Phillip Kilgore
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program 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
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 
17  Additionally, you should have recieved a copy of the GNU Classpath
18  exception, which amends the license to generally permit linking against
19  the software provided herein.
20 */
21 
30 #ifndef WFIO_BYTESWAP_H
31 #define WFIO_BYTESWAP_H
32 
33 #include <wfio/type.h>
34 
35 #ifdef WFIO_COMPILER_GCC
36 #define WFIO_INTRINSIC_SWAP_16_
37 #define WFIO_INTRINSIC_SWAP_32_
38 #define WFIO_INTRINSIC_SWAP_64_
39 #endif
40 
41 /* 16-bit integers */
42 
54 WFIO_INLINE wfio_sint16_t wfio_swapSI16(wfio_sint16_t src) {
55  /* GCC has an intrinsic version of this, use it */
56  #ifdef WFIO_COMPILER_GCC
57  return __builtin_bswap16((wfio_sint16_t) src);
58  #else
59  register wfio_uint8_t tmp;
60  register union {
61  wfio_sint16_t w;
62  wfio_sint8_t b[2];
63  } pun;
64 
65  /* Now perform the swap */
66  pun.w = src;
67  tmp = pun.b[0];
68  pun.b[0] = pun.b[1];
69  pun.b[1] = tmp;
70 
71  return pun.w;
72  #endif
73 }
74 
94 WFIO_DLL void wfio_swapSI16V2(const wfio_sint16_t* src, wfio_sint16_t* dest,
95  size_t len);
96 
114 WFIO_ALWAYS_INLINE void wfio_swapSI16V(wfio_sint16_t* buf, size_t len) {
115  wfio_swapSI16V2(buf, buf, len);
116 }
117 
118 
130 WFIO_ALWAYS_INLINE wfio_sint16_t wfio_swapUI16(wfio_sint16_t src) {
131  return (wfio_sint16_t) wfio_swapSI16((wfio_sint16_t) src);
132 }
133 
153 WFIO_ALWAYS_INLINE void wfio_swapUI16V2(const wfio_sint16_t* src, wfio_sint16_t* dest,
154  size_t len) {
155  wfio_swapSI16V2((wfio_sint16_t*) src, (wfio_sint16_t*) dest, len);
156 }
157 
175 WFIO_ALWAYS_INLINE void wfio_swapUI16V(wfio_sint16_t* buf, size_t len) {
176  wfio_swapSI16V2((wfio_sint16_t*) buf, (wfio_sint16_t*) buf, len);
177 }
178 
179 /* 32-bit Integers */
180 
181 #ifdef WFIO_INTRINSIC_SWAP_32_
182 #define WFIO_MAYBE_INLINE WFIO_ALWAYS_INLINE
183 #elif !defined(WFIO_DOXYGEN)
184 #define WFIO_MAYBE_INLINE WFIO_DLL
185 #endif
186 
197 WFIO_MAYBE_INLINE wfio_sint32_t wfio_swapSI32(wfio_sint32_t src)
198 #ifdef WFIO_INTRINSIC_SWAP_32_
199 {
200  #ifdef WFIO_COMPILER_GCC
201  return __builtin_bswap32((wfio_sint32_t) src);
202  #else
203  return (wfio_swapSI16(src) << 16) |
204  (wfio_swapSI16(src >> 16));
205  #endif
206 }
207 #else
208 ;
209 #endif
210 
230 WFIO_DLL void wfio_swapSI32V2(const wfio_sint32_t* src, wfio_sint32_t* dest,
231  size_t len);
232 
250 WFIO_ALWAYS_INLINE void wfio_swapSI32V(wfio_sint32_t* buf, size_t len) {
251  wfio_swapSI32V2(buf, buf, len);
252 }
253 
265 WFIO_ALWAYS_INLINE wfio_sint32_t wfio_swapUI32(wfio_sint32_t src) {
266  return (wfio_sint32_t) wfio_swapSI32((wfio_sint32_t) src);
267 }
268 
288 WFIO_ALWAYS_INLINE void wfio_swapUI32V2(const wfio_sint32_t* src, wfio_sint32_t* dest,
289  size_t len) {
290  wfio_swapSI32V2((wfio_sint32_t*) src, (wfio_sint32_t*) dest, len);
291 }
292 
310 WFIO_ALWAYS_INLINE void wfio_swapUI32V(wfio_sint32_t* buf, size_t len) {
311  wfio_swapSI32V2((wfio_sint32_t*) buf, (wfio_sint32_t*) buf, len);
312 }
313 
314 /* 64-bit Integers */
315 
316 #ifdef WFIO_INTRINSIC_SWAP_64_
317 #define WFIO_MAYBE_INLINE WFIO_ALWAYS_INLINE
318 #elif !defined(WFIO_DOXYGEN)
319 #define WFIO_MAYBE_INLINE WFIO_DLL
320 #endif
321 
333 WFIO_MAYBE_INLINE wfio_sint64_t wfio_swapSI64(wfio_sint64_t src)
334 #ifdef WFIO_INTRINSIC_SWAP_64_
335 {
336  #ifdef WFIO_COMPILER_GCC
337  return __builtin_bswap64((wfio_sint64_t) src);
338  #else
339  return (wfio_swapSI32(src) << 32) |
340  (wfio_swapSI32(src >> 32));
341  #endif
342 }
343 #else
344 ;
345 #endif
346 
366 WFIO_DLL void wfio_swapSI64V2(const wfio_sint64_t* src,
367  wfio_sint64_t* dest, size_t len);
368 
386 WFIO_ALWAYS_INLINE void wfio_swapSI64V(wfio_sint64_t* buf, size_t len) {
387  wfio_swapSI64V2(buf, buf, len);
388 }
389 
401 WFIO_ALWAYS_INLINE wfio_sint64_t wfio_swapUI64(wfio_sint64_t src) {
402  return (wfio_sint64_t) wfio_swapSI64((wfio_sint64_t) src);
403 }
404 
424 WFIO_ALWAYS_INLINE void wfio_swapUI64V2(const wfio_sint64_t* src,
425  wfio_sint64_t* dest, size_t len) {
426  wfio_swapSI64V2((wfio_sint64_t*) src, (wfio_sint64_t*) dest, len);
427 }
428 
446 WFIO_ALWAYS_INLINE void wfio_swapUI64V(wfio_sint64_t* buf, size_t len) {
447  wfio_swapSI64V2((wfio_sint64_t*) buf, (wfio_sint64_t*) buf, len);
448 }
449 
450 #undef WFIO_MAYBE_INLINE
451 
452 #endif
WFIO_ALWAYS_INLINE void wfio_swapUI16V2(const wfio_sint16_t *src, wfio_sint16_t *dest, size_t len)
Perform a byte swap on 16-bit unsigned integers in src to be stored in dest.
Definition: byteswap.h:153
#define WFIO_MAYBE_INLINE
Potentially mark this symbol for inlining.
Definition: host.h:466
WFIO_ALWAYS_INLINE void wfio_swapUI64V(wfio_sint64_t *buf, size_t len)
Perform a byte swap on 64-bit unsigned integers in buf.
Definition: byteswap.h:446
WFIO_ALWAYS_INLINE void wfio_swapUI32V2(const wfio_sint32_t *src, wfio_sint32_t *dest, size_t len)
Perform a byte swap on 32-bit unsigned integers in src to be stored in dest.
Definition: byteswap.h:288
WFIO_ALWAYS_INLINE wfio_sint16_t wfio_swapUI16(wfio_sint16_t src)
Perform a byte swap on a 16-bit unsigned integer.
Definition: byteswap.h:130
#define WFIO_DLL
Mark the symbol as an element of the library.
Definition: host.h:397
WFIO_ALWAYS_INLINE wfio_sint64_t wfio_swapUI64(wfio_sint64_t src)
Perform a byte swap on a 64-bit unsigned integer.
Definition: byteswap.h:401
#define WFIO_ALWAYS_INLINE
Mark this symbol to be forced inline.
Definition: host.h:447
WFIO_ALWAYS_INLINE void wfio_swapUI32V(wfio_sint32_t *buf, size_t len)
Perform a byte swap on 32-bit unsigned integers in buf.
Definition: byteswap.h:310
WFIO_MAYBE_INLINE wfio_sint64_t wfio_swapSI64(wfio_sint64_t src)
Perform a byte swap on a 64-bit signed integer.
WFIO_ALWAYS_INLINE void wfio_swapSI32V(wfio_sint32_t *buf, size_t len)
Perform a byte swap on 32-bit signed integers in buf.
Definition: byteswap.h:250
WFIO_ALWAYS_INLINE void wfio_swapSI16V(wfio_sint16_t *buf, size_t len)
Perform a byte swap on 16-bit signed integers in buf.
Definition: byteswap.h:114
WFIO_DLL void wfio_swapSI32V2(const wfio_sint32_t *src, wfio_sint32_t *dest, size_t len)
Perform a byte swap on 32-bit signed integers in src to be stored in dest.
#define WFIO_INLINE
Mark this symbol for inlining.
Definition: host.h:377
WFIO_ALWAYS_INLINE void wfio_swapUI16V(wfio_sint16_t *buf, size_t len)
Perform a byte swap on 16-bit unsigned integers in buf.
Definition: byteswap.h:175
WFIO_ALWAYS_INLINE void wfio_swapSI64V(wfio_sint64_t *buf, size_t len)
Perform a byte swap on 64-bit signed integers in buf.
Definition: byteswap.h:386
WFIO_ALWAYS_INLINE void wfio_swapUI64V2(const wfio_sint64_t *src, wfio_sint64_t *dest, size_t len)
Perform a byte swap on 64-bit unsigned integers in src to be stored in dest.
Definition: byteswap.h:424
WFIO_DLL void wfio_swapSI64V2(const wfio_sint64_t *src, wfio_sint64_t *dest, size_t len)
Perform a byte swap on 64-bit signed integers in src to be stored in dest.
WFIO_MAYBE_INLINE wfio_sint32_t wfio_swapSI32(wfio_sint32_t src)
Perform a byte swap on a 32-bit signed integer.
WFIO_INLINE wfio_sint16_t wfio_swapSI16(wfio_sint16_t src)
Perform a byte swap on a 16-bit signed integer.
Definition: byteswap.h:54
WFIO_ALWAYS_INLINE wfio_sint32_t wfio_swapUI32(wfio_sint32_t src)
Perform a byte swap on a 32-bit signed integer.
Definition: byteswap.h:265
WFIO_DLL void wfio_swapSI16V2(const wfio_sint16_t *src, wfio_sint16_t *dest, size_t len)
Perform a byte swap on 16-bit signed integers in src to be stored in dest.
Shared type definitions.