summaryrefslogtreecommitdiffstats
path: root/le.h
blob: 953db220fc81495e87fa96f944403e15f7ce9c53 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2008 rPath, Inc. - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * le.h
 *
 * Write unaligned littleendian data
 */

#ifndef LE_H
#define LE_H

#include <inttypes.h>

#if defined(__386__) || defined(__i386__) || defined(__x86_64__)

/* Littleendian architecture, unaligned accesses permitted */

static inline uint16_t rdle16(const uint16_t *p)
{
	return *p;
}

static inline uint32_t rdle32(const uint32_t *p)
{
	return *p;
}

static inline uint64_t rdle64(const uint64_t *p)
{
	return *p;
}

static inline void wrle16(uint16_t v, uint16_t *p)
{
	*p = v;
}

static inline void wrle32(uint32_t v, uint32_t *p)
{
	*p = v;
}

static inline void wrle64(uint64_t v, uint64_t *p)
{
	*p = v;
}

#else

static inline uint16_t rdle16(const uint16_t *p)
{
	const uint8_t *_p = (const uint8_t *)p;
	uint16_t _v;

	_v  = (uint16_t)_p[0];
	_v += (uint16_t)_p[1] << 8;

	return _v;
}

static inline uint32_t rdle32(const uint32_t *p)
{
	const uint8_t *_p = (const uint8_t *)p;
	uint32_t _v;

	_v  = (uint32_t)_p[0];
	_v += (uint32_t)_p[1] << 8;
	_v += (uint32_t)_p[2] << 16;
	_v += (uint32_t)_p[3] << 24;

	return _v;
}

static inline uint64_t rdle64(const uint64_t *p)
{
	const uint8_t *_p = (const uint8_t *)p;
	uint64_t _v;

	_v  = (uint64_t)_p[0];
	_v += (uint64_t)_p[1] << 8;
	_v += (uint64_t)_p[2] << 16;
	_v += (uint64_t)_p[3] << 24;
	_v += (uint64_t)_p[4] << 32;
	_v += (uint64_t)_p[5] << 40;
	_v += (uint64_t)_p[6] << 48;
	_v += (uint64_t)_p[7] << 56;

	return _v;
}

static inline void wrle16(uint16_t v, uint16_t *p)
{
	uint8_t *_p = (uint8_t *)p;

	_p[0] = v;
	_p[1] = v >> 8;
}

static inline void wrle32(uint32_t v, uint32_t *p)
{
	uint8_t *_p = (uint8_t *)p;

	_p[0] = v;
	_p[1] = v >> 8;
	_p[2] = v >> 16;
	_p[3] = v >> 24;
}

static inline void wrle64(uint64_t v, uint64_t *p)
{
	uint8_t *_p = (uint8_t *)p;

	_p[0] = v;
	_p[1] = v >> 8;
	_p[2] = v >> 16;
	_p[3] = v >> 24;
	_p[4] = v >> 32;
	_p[5] = v >> 40;
	_p[6] = v >> 48;
	_p[7] = v >> 56;
}

#endif

#endif /* LE_H */