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