libarmutils  1.4
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
endian_swap.h
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2010 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Author:
8 * name: Brian Ermold
9 * phone: (509) 375-2277
10 * email: brian.ermold@pnl.gov
11 *
12 ********************************************************************************
13 *
14 * REPOSITORY INFORMATION:
15 * $Revision: 6421 $
16 * $Author: ermold $
17 * $Date: 2011-04-26 01:23:44 +0000 (Tue, 26 Apr 2011) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file endian_swap.h
26  * Endian Swapping Functions
27  */
28 
29 #ifndef _ENDIAN_SWAP_H
30 #define _ENDIAN_SWAP_H
31 
32 /**
33  * @defgroup ARMUTILS_ENDIAN_SWAP Endian Swapping
34  */
35 /*@{*/
36 
37 /** Swap byes in 16 bit value. */
38 #define SWAP_BYTES_16(x) \
39  ( ( ((x) & 0xff00) >> 8) | \
40  ( ((x) & 0x00ff) << 8) )
41 
42 /** Swap byes in 32 bit value. */
43 #define SWAP_BYTES_32(x) \
44  ( ( ((x) & 0xff000000) >> 24) | \
45  ( ((x) & 0x00ff0000) >> 8) | \
46  ( ((x) & 0x0000ff00) << 8) | \
47  ( ((x) & 0x000000ff) << 24) )
48 
49 /** Swap byes in 64 bit value. */
50 #define SWAP_BYTES_64(x) \
51  ( ( ((x) & 0xff00000000000000LL) >> 56) | \
52  ( ((x) & 0x00ff000000000000LL) >> 40) | \
53  ( ((x) & 0x0000ff0000000000LL) >> 24) | \
54  ( ((x) & 0x000000ff00000000LL) >> 8) | \
55  ( ((x) & 0x00000000ff000000LL) << 8) | \
56  ( ((x) & 0x0000000000ff0000LL) << 24) | \
57  ( ((x) & 0x000000000000ff00LL) << 40) | \
58  ( ((x) & 0x00000000000000ffLL) << 56) )
59 
60 #ifdef _BIG_ENDIAN
61  #define BTON_16(x) (x)
62  #define BTON_32(x) (x)
63  #define BTON_64(x) (x)
64  #define LTON_16(x) SWAP_BYTES_16(x)
65  #define LTON_32(x) SWAP_BYTES_32(x)
66  #define LTON_64(x) SWAP_BYTES_64(x)
67 #else
68  #define BTON_16(x) SWAP_BYTES_16(x)
69  #define BTON_32(x) SWAP_BYTES_32(x)
70  #define BTON_64(x) SWAP_BYTES_64(x)
71  #define LTON_16(x) (x)
72  #define LTON_32(x) (x)
73  #define LTON_64(x) (x)
74 #endif
75 
76 void *bton_16(void *data, size_t nvals);
77 void *bton_32(void *data, size_t nvals);
78 void *bton_64(void *data, size_t nvals);
79 
80 void *lton_16(void *data, size_t nvals);
81 void *lton_32(void *data, size_t nvals);
82 void *lton_64(void *data, size_t nvals);
83 
84 int bton_read_16(int fd, void *data, size_t nvals);
85 int bton_read_32(int fd, void *data, size_t nvals);
86 int bton_read_64(int fd, void *data, size_t nvals);
87 
88 int lton_read_16(int fd, void *data, size_t nvals);
89 int lton_read_32(int fd, void *data, size_t nvals);
90 int lton_read_64(int fd, void *data, size_t nvals);
91 
92 /*@}*/
93 
94 #endif /* _ENDIAN_SWAP_H */