aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/include/cybozu/endian.hpp
blob: 3f1575c466151c96ea802962d975ced9f634f114 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once

/**
    @file
    @brief deal with big and little endian

    @author MITSUNARI Shigeo(@herumi)
*/
#include <cybozu/inttype.hpp>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

namespace cybozu {

#ifdef _MSC_VER
inline uint16_t byteSwap(uint16_t x) { return _byteswap_ushort(x); }
inline uint32_t byteSwap(uint32_t x) { return _byteswap_ulong(x); }
inline uint64_t byteSwap(uint64_t x) { return _byteswap_uint64(x); }
#else
#if (((__GNUC__) << 16) + (__GNUC_MINOR__)) >= ((4 << 16) + 8)
inline uint16_t byteSwap(uint16_t x) { return __builtin_bswap16(x); }
#else
inline uint16_t byteSwap(uint16_t x) { return (x >> 8) | (x << 8); }
#endif
inline uint32_t byteSwap(uint32_t x) { return __builtin_bswap32(x); }
inline uint64_t byteSwap(uint64_t x) { return __builtin_bswap64(x); }
#endif

/**
    get 16bit integer as little endian
    @param src [in] pointer
*/
inline uint16_t Get16bitAsLE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint16_t x;
    memcpy(&x, src, sizeof(x));
    return x;
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return p[0] | (p[1] << 8);
#endif
}

/**
    get 32bit integer as little endian
    @param src [in] pointer
*/
inline uint32_t Get32bitAsLE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint32_t x;
    memcpy(&x, src, sizeof(x));
    return x;
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return Get16bitAsLE(p) | (static_cast<uint32_t>(Get16bitAsLE(p + 2)) << 16);
#endif
}

/**
    get 64bit integer as little endian
    @param src [in] pointer
*/
inline uint64_t Get64bitAsLE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint64_t x;
    memcpy(&x, src, sizeof(x));
    return x;
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return Get32bitAsLE(p) | (static_cast<uint64_t>(Get32bitAsLE(p + 4)) << 32);
#endif
}

/**
    get 16bit integer as bit endian
    @param src [in] pointer
*/
inline uint16_t Get16bitAsBE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint16_t x;
    memcpy(&x, src, sizeof(x));
    return byteSwap(x);
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return p[1] | (p[0] << 8);
#endif
}

/**
    get 32bit integer as bit endian
    @param src [in] pointer
*/
inline uint32_t Get32bitAsBE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint32_t x;
    memcpy(&x, src, sizeof(x));
    return byteSwap(x);
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return Get16bitAsBE(p + 2) | (static_cast<uint32_t>(Get16bitAsBE(p)) << 16);
#endif
}

/**
    get 64bit integer as big endian
    @param src [in] pointer
*/
inline uint64_t Get64bitAsBE(const void *src)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    uint64_t x;
    memcpy(&x, src, sizeof(x));
    return byteSwap(x);
#else
    const uint8_t *p = static_cast<const uint8_t *>(src);
    return Get32bitAsBE(p + 4) | (static_cast<uint64_t>(Get32bitAsBE(p)) << 32);
#endif
}

/**
    set 16bit integer as little endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set16bitAsLE(void *src, uint16_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    p[0] = static_cast<uint8_t>(x);
    p[1] = static_cast<uint8_t>(x >> 8);
#endif
}
/**
    set 32bit integer as little endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set32bitAsLE(void *src, uint32_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    p[0] = static_cast<uint8_t>(x);
    p[1] = static_cast<uint8_t>(x >> 8);
    p[2] = static_cast<uint8_t>(x >> 16);
    p[3] = static_cast<uint8_t>(x >> 24);
#endif
}
/**
    set 64bit integer as little endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set64bitAsLE(void *src, uint64_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    Set32bitAsLE(p, static_cast<uint32_t>(x));
    Set32bitAsLE(p + 4, static_cast<uint32_t>(x >> 32));
#endif
}
/**
    set 16bit integer as big endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set16bitAsBE(void *src, uint16_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    x = byteSwap(x);
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    p[0] = static_cast<uint8_t>(x >> 8);
    p[1] = static_cast<uint8_t>(x);
#endif
}
/**
    set 32bit integer as big endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set32bitAsBE(void *src, uint32_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    x = byteSwap(x);
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    p[0] = static_cast<uint8_t>(x >> 24);
    p[1] = static_cast<uint8_t>(x >> 16);
    p[2] = static_cast<uint8_t>(x >> 8);
    p[3] = static_cast<uint8_t>(x);
#endif
}
/**
    set 64bit integer as big endian
    @param src [out] pointer
    @param x [in] integer
*/
inline void Set64bitAsBE(void *src, uint64_t x)
{
#if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
    x = byteSwap(x);
    memcpy(src, &x, sizeof(x));
#else
    uint8_t *p = static_cast<uint8_t *>(src);
    Set32bitAsBE(p, static_cast<uint32_t>(x >> 32));
    Set32bitAsBE(p + 4, static_cast<uint32_t>(x));
#endif
}

} // cybozu