-
Notifications
You must be signed in to change notification settings - Fork 34
/
common.h
65 lines (53 loc) · 1.72 KB
/
common.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Copyright 2015 Kakaroto
This software is distributed under the terms of the GNU General Public
License ("GPL") version 3, as published by the Free Software Foundation.
*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdio.h>
#include <inttypes.h>
#ifdef WIN32
#define MKDIR(x,y) mkdir(x)
#define U64_FORMAT PRIu64
#else
#define MKDIR(x,y) mkdir(x,y)
#define U64_FORMAT "lu"
#endif
#define noop16(x) (x)
#define noop32(x) (x)
#define noop64(x) (x)
#define swap16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
(((uint16_t)(x) & 0x00ff) << 8))
#define swap32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
(((uint32_t)(x) & 0x00ff0000) >> 8) | \
(((uint32_t)(x) & 0x0000ff00) << 8) | \
(((uint32_t)(x) & 0x000000ff) << 24))
#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))
#ifdef __BIG_ENDIAN__
#define TO_BE(b, x) noop##b (x)
#define TO_LE(b, x) swap##b (x)
#define FROM_BE(b, x) noop##b (x)
#define FROM_LE(b, x) swap##b (x)
#else
#define TO_BE(b, x) swap##b (x)
#define TO_LE(b, x) noop##b (x)
#define FROM_BE(b, x) swap##b (x)
#define FROM_LE(b, x) noop##b (x)
#endif
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
#define DBG(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
#else
#define DBG(...)
#endif
#endif /* __COMMON_H__ */