-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.h
44 lines (34 loc) · 1.61 KB
/
swap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* (c) Copyright 1999, 2002 -- Anders Torger
*
* This program is open source. For license terms, see the LICENSE file.
*
*/
#ifndef _SWAP_H_
#define _SWAP_H_
#include <inttypes.h>
#ifdef __LITTLE_ENDIAN__
#include <netinet/in.h>
#define SWAP32(x) ntohl((uint32_t)(x))
#define SWAP16(x) ntohs((uint16_t)(x))
#endif
#ifdef __BIG_ENDIAN__
#define SWAP16(x) \
(((((uint16_t)(x)) & 0xff00U) >> 8) | \
((((uint16_t)(x)) & 0x00ffU) << 8))
#define SWAP32(x) \
(((((uint32_t)(x)) & 0xff000000UL) >> 24) | \
((((uint32_t)(x)) & 0x00ff0000UL) >> 8) | \
((((uint32_t)(x)) & 0x0000ff00UL) << 8) | \
((((uint32_t)(x)) & 0x000000ffUL) << 24))
#endif
#define SWAP64(x) \
(((((uint64_t)(x)) & 0xff00000000000000ULL) >> 56) | \
((((uint64_t)(x)) & 0x00ff000000000000ULL) >> 40) | \
((((uint64_t)(x)) & 0x0000ff0000000000ULL) >> 24) | \
((((uint64_t)(x)) & 0x000000ff00000000ULL) >> 8) | \
((((uint64_t)(x)) & 0x00000000ff000000ULL) << 8) | \
((((uint64_t)(x)) & 0x0000000000ff0000ULL) << 24) | \
((((uint64_t)(x)) & 0x000000000000ff00ULL) << 40) | \
((((uint64_t)(x)) & 0x00000000000000ffULL) << 56))
#endif