xref: /onnv-gate/usr/src/lib/libparted/common/include/parted/endian.h (revision 9663:ace9a2ac3683)
1 /*
2     libparted - a library for manipulating disk partitions
3     Copyright (C) 1998-2002, 2007 Free Software Foundation, Inc.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program 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
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /* should only be #included by files in libparted */
20 
21 #ifndef PED_ENDIAN_H_INCLUDED
22 #define PED_ENDIAN_H_INCLUDED
23 
24 #include <stdint.h>
25 
26 /* returns the n'th least significant byte */
27 #define _GET_BYTE(x, n)		( ((x) >> (8 * (n))) & 0xff )
28 
29 #define _PED_SWAP16(x)		( (_GET_BYTE(x, 0) << 8)	\
30 				+ (_GET_BYTE(x, 1) << 0) )
31 
32 #define _PED_SWAP32(x)		( (_GET_BYTE(x, 0) << 24)	\
33 				+ (_GET_BYTE(x, 1) << 16)	\
34 				+ (_GET_BYTE(x, 2) << 8)	\
35 				+ (_GET_BYTE(x, 3) << 0) )
36 
37 #define _PED_SWAP64(x)		( (_GET_BYTE(x, 0) << 56)	\
38 				+ (_GET_BYTE(x, 1) << 48)	\
39 				+ (_GET_BYTE(x, 2) << 40)	\
40 				+ (_GET_BYTE(x, 3) << 32)	\
41 				+ (_GET_BYTE(x, 4) << 24)	\
42 				+ (_GET_BYTE(x, 5) << 16)	\
43 				+ (_GET_BYTE(x, 6) << 8)	\
44 				+ (_GET_BYTE(x, 7) << 0) )
45 
46 #define PED_SWAP16(x)		((uint16_t) _PED_SWAP16( (uint16_t) (x) ))
47 #define PED_SWAP32(x)		((uint32_t) _PED_SWAP32( (uint32_t) (x) ))
48 #define PED_SWAP64(x)		((uint64_t) _PED_SWAP64( (uint64_t) (x) ))
49 
50 #ifdef WORDS_BIGENDIAN
51 
52 #define PED_CPU_TO_LE16(x)	PED_SWAP16(x)
53 #define PED_CPU_TO_BE16(x)	(x)
54 #define PED_CPU_TO_LE32(x)	PED_SWAP32(x)
55 #define PED_CPU_TO_BE32(x)	(x)
56 #define PED_CPU_TO_LE64(x)	PED_SWAP64(x)
57 #define PED_CPU_TO_BE64(x)	(x)
58 
59 #define PED_LE16_TO_CPU(x)	PED_SWAP16(x)
60 #define PED_BE16_TO_CPU(x)	(x)
61 #define PED_LE32_TO_CPU(x)	PED_SWAP32(x)
62 #define PED_BE32_TO_CPU(x)	(x)
63 #define PED_LE64_TO_CPU(x)	PED_SWAP64(x)
64 #define PED_BE64_TO_CPU(x)	(x)
65 
66 #else /* !WORDS_BIGENDIAN */
67 
68 #define PED_CPU_TO_LE16(x)	(x)
69 #define PED_CPU_TO_BE16(x)	PED_SWAP16(x)
70 #define PED_CPU_TO_LE32(x)	(x)
71 #define PED_CPU_TO_BE32(x)	PED_SWAP32(x)
72 #define PED_CPU_TO_LE64(x)	(x)
73 #define PED_CPU_TO_BE64(x)	PED_SWAP64(x)
74 
75 #define PED_LE16_TO_CPU(x)	(x)
76 #define PED_BE16_TO_CPU(x)	PED_SWAP16(x)
77 #define PED_LE32_TO_CPU(x)	(x)
78 #define PED_BE32_TO_CPU(x)	PED_SWAP32(x)
79 #define PED_LE64_TO_CPU(x)	(x)
80 #define PED_BE64_TO_CPU(x)	PED_SWAP64(x)
81 
82 #endif /* !WORDS_BIGENDIAN */
83 
84 #endif /* PED_ENDIAN_H_INCLUDED */
85 
86