aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/swap.c
blob: b6558234cdbb806b7273795979d2a404d0254c25 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

/*
 * big-endian short
 */
u16int
beswap2(u16int s)
{
	uchar *p;

	p = (uchar*)&s;
	return (p[0]<<8) | p[1];
}

/*
 * big-endian long
 */
u32int
beswap4(u32int l)
{
	uchar *p;

	p = (uchar*)&l;
	return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
}

/*
 * big-endian vlong
 */
u64int
beswap8(u64int v)
{
	uchar *p;

	p = (uchar*)&v;
	return ((u64int)p[0]<<56) | ((u64int)p[1]<<48) | ((u64int)p[2]<<40)
				 | ((u64int)p[3]<<32) | ((u64int)p[4]<<24)
				 | ((u64int)p[5]<<16) | ((u64int)p[6]<<8)
				 | (u64int)p[7];
}

/*
 * little-endian short
 */
u16int
leswap2(u16int s)
{
	uchar *p;

	p = (uchar*)&s;
	return (p[1]<<8) | p[0];
}

/*
 * little-endian long
 */
u32int
leswap4(u32int l)
{
	uchar *p;

	p = (uchar*)&l;
	return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
}

/*
 * little-endian vlong
 */
u64int
leswap8(u64int v)
{
	uchar *p;

	p = (uchar*)&v;
	return ((u64int)p[7]<<56) | ((u64int)p[6]<<48) | ((u64int)p[5]<<40)
				 | ((u64int)p[4]<<32) | ((u64int)p[3]<<24)
				 | ((u64int)p[2]<<16) | ((u64int)p[1]<<8)
				 | (u64int)p[0];
}

u16int
leload2(uchar *b)
{
	return b[0] | (b[1]<<8);
}

u32int
leload4(uchar *b)
{
	return b[0] | (b[1]<<8) | (b[2]<<16) | (b[3]<<24);
}

u64int
leload8(uchar *b)
{
	return leload4(b) | ((uvlong)leload4(b+4) << 32);
}

u16int
beload2(uchar *b)
{
	return (b[0]<<8) | b[1];
}

u32int
beload4(uchar *b)
{
	return (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3];
}

u64int
beload8(uchar *b)
{
	return ((uvlong)beload4(b) << 32) | beload4(b+4);
}