forked from sunflyer/vlmcsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endian.c
171 lines (143 loc) · 3.71 KB
/
endian.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef CONFIG
#define CONFIG "config.h"
#endif // CONFIG
#include CONFIG
#include "endian.h"
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN) \
&& defined(BS16) && defined(BS32) && defined(BS64)
#else // ! defined(__BYTE_ORDER)
void PUT_UAA64BE(void *p, unsigned long long v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
_p[ 0 ] = v >> 56;
_p[ 1 ] = v >> 48;
_p[ 2 ] = v >> 40;
_p[ 3 ] = v >> 32;
_p[ 4 ] = v >> 24;
_p[ 5 ] = v >> 16;
_p[ 6 ] = v >> 8;
_p[ 7 ] = v;
}
void PUT_UAA32BE(void *p, unsigned int v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
_p[ 0 ] = v >> 24;
_p[ 1 ] = v >> 16;
_p[ 2 ] = v >> 8;
_p[ 3 ] = v;
}
void PUT_UAA16BE(void *p, unsigned short v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
_p[ 0 ] = v >> 8;
_p[ 1 ] = v;
}
void PUT_UAA64LE(void *p, unsigned long long v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
_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;
}
void PUT_UAA32LE(void *p, unsigned int v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
_p[ 0 ] = v;
_p[ 1 ] = v >> 8;
_p[ 2 ] = v >> 16;
_p[ 3 ] = v >> 24;
}
void PUT_UAA16LE(void *p, unsigned short v, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
_p[ 0 ] = v;
_p[ 1 ] = v >> 8;
}
unsigned long long GET_UAA64BE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
return
(unsigned long long)_p[ 0 ] << 56 |
(unsigned long long)_p[ 1 ] << 48 |
(unsigned long long)_p[ 2 ] << 40 |
(unsigned long long)_p[ 3 ] << 32 |
(unsigned long long)_p[ 4 ] << 24 |
(unsigned long long)_p[ 5 ] << 16 |
(unsigned long long)_p[ 6 ] << 8 |
(unsigned long long)_p[ 7 ];
}
unsigned int GET_UAA32BE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
return
(unsigned int)_p[ 0 ] << 24 |
(unsigned int)_p[ 1 ] << 16 |
(unsigned int)_p[ 2 ] << 8 |
(unsigned int)_p[ 3 ];
}
unsigned short GET_UAA16BE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
return
(unsigned short)_p[ 0 ] << 8 |
(unsigned short)_p[ 1 ];
}
unsigned long long GET_UAA64LE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
return
(unsigned long long)_p[ 0 ] |
(unsigned long long)_p[ 1 ] << 8 |
(unsigned long long)_p[ 2 ] << 16 |
(unsigned long long)_p[ 3 ] << 24 |
(unsigned long long)_p[ 4 ] << 32 |
(unsigned long long)_p[ 5 ] << 40 |
(unsigned long long)_p[ 6 ] << 48 |
(unsigned long long)_p[ 7 ] << 56;
}
unsigned int GET_UAA32LE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
return
(unsigned int)_p[ 0 ] |
(unsigned int)_p[ 1 ] << 8 |
(unsigned int)_p[ 2 ] << 16 |
(unsigned int)_p[ 3 ] << 24;
}
unsigned short GET_UAA16LE(void *p, unsigned int i)
{
unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
return
(unsigned short)_p[ 0 ] |
(unsigned short)_p[ 1 ] << 8;
}
unsigned short BE16(unsigned short x)
{
return GET_UAA16BE(&x, 0);
}
unsigned short LE16(unsigned short x)
{
return GET_UAA16LE(&x, 0);
}
unsigned int BE32(unsigned int x)
{
return GET_UAA32BE(&x, 0);
}
unsigned int LE32(unsigned int x)
{
return GET_UAA32LE(&x, 0);
}
unsigned long long BE64(unsigned long long x)
{
return GET_UAA64BE(&x, 0);
}
inline unsigned long long LE64(unsigned long long x)
{
return GET_UAA64LE(&x, 0);
}
#endif // defined(__BYTE_ORDER)