forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbytearray.cpp
339 lines (276 loc) · 7.44 KB
/
vbytearray.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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, either version 3 of the License, or
(at your option) any later version.
VESC Tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vbytearray.h"
#include <cmath>
#include <stdint.h>
namespace {
inline double roundDouble(double x) {
return x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
}
}
VByteArray::VByteArray()
{
}
VByteArray::VByteArray(const QByteArray &data) : QByteArray(data)
{
}
void VByteArray::vbAppendInt64(qint64 number)
{
QByteArray data;
data.append((char)((number >> 56) & 0xFF));
data.append((char)((number >> 48) & 0xFF));
data.append((char)((number >> 40) & 0xFF));
data.append((char)((number >> 32) & 0xFF));
data.append((char)((number >> 24) & 0xFF));
data.append((char)((number >> 16) & 0xFF));
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendUint64(quint64 number)
{
QByteArray data;
data.append((char)((number >> 56) & 0xFF));
data.append((char)((number >> 48) & 0xFF));
data.append((char)((number >> 40) & 0xFF));
data.append((char)((number >> 32) & 0xFF));
data.append((char)((number >> 24) & 0xFF));
data.append((char)((number >> 16) & 0xFF));
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendInt32(qint32 number)
{
QByteArray data;
data.append((char)((number >> 24) & 0xFF));
data.append((char)((number >> 16) & 0xFF));
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendUint32(quint32 number)
{
QByteArray data;
data.append((char)((number >> 24) & 0xFF));
data.append((char)((number >> 16) & 0xFF));
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendInt16(qint16 number)
{
QByteArray data;
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendUint16(quint16 number)
{
QByteArray data;
data.append((char)((number >> 8) & 0xFF));
data.append((char)(number & 0xFF));
append(data);
}
void VByteArray::vbAppendInt8(qint8 number)
{
append((char)number);
}
void VByteArray::vbAppendUint8(quint8 number)
{
append((char)number);
}
void VByteArray::vbAppendDouble64(double number, double scale)
{
vbAppendInt64((qint64)roundDouble(number * scale));
}
void VByteArray::vbAppendDouble32(double number, double scale)
{
vbAppendInt32((qint32)roundDouble(number * scale));
}
void VByteArray::vbAppendDouble16(double number, double scale)
{
vbAppendInt16((qint16)roundDouble(number * scale));
}
void VByteArray::vbAppendDouble32Auto(double number)
{
// Set subnormal numbers to 0 as they are not handled properly
// using this method.
if (fabs(number) < 1.5e-38) {
number = 0.0;
}
int e = 0;
float fr = frexpf(number, &e);
float fr_abs = fabsf(fr);
uint32_t fr_s = 0;
if (fr_abs >= 0.5) {
fr_s = (uint32_t)((fr_abs - 0.5f) * 2.0f * 8388608.0f);
e += 126;
}
uint32_t res = ((e & 0xFF) << 23) | (fr_s & 0x7FFFFF);
if (fr < 0) {
res |= 1 << 31;
}
vbAppendUint32(res);
}
void VByteArray::vbAppendDouble64Auto(double number)
{
float n = number;
float err = float(number - double(n));
vbAppendDouble32Auto(n);
vbAppendDouble32Auto(err);
}
void VByteArray::vbAppendString(QString str)
{
append(str.toLocal8Bit());
append((char)0);
}
qint64 VByteArray::vbPopFrontInt64()
{
if (size() < 8) {
return 0;
}
qint64 res = (quint64)((quint8) at(0)) << 56 |
(quint64)((quint8) at(1)) << 48 |
(quint64)((quint8) at(2)) << 40 |
(quint64)((quint8) at(3)) << 32 |
(quint64)((quint8) at(4)) << 24 |
(quint64)((quint8) at(5)) << 16 |
(quint64)((quint8) at(6)) << 8 |
(quint64)((quint8) at(7));
remove(0, 8);
return res;
}
quint64 VByteArray::vbPopFrontUint64()
{
if (size() < 8) {
return 0;
}
quint64 res = (quint64)((quint8) at(0)) << 56 |
(quint64)((quint8) at(1)) << 48 |
(quint64)((quint8) at(2)) << 40 |
(quint64)((quint8) at(3)) << 32 |
(quint64)((quint8) at(4)) << 24 |
(quint64)((quint8) at(5)) << 16 |
(quint64)((quint8) at(6)) << 8 |
(quint64)((quint8) at(7));
remove(0, 8);
return res;
}
qint32 VByteArray::vbPopFrontInt32()
{
if (size() < 4) {
return 0;
}
qint32 res = ((quint8) at(0)) << 24 |
((quint8) at(1)) << 16 |
((quint8) at(2)) << 8 |
((quint8) at(3));
remove(0, 4);
return res;
}
quint32 VByteArray::vbPopFrontUint32()
{
if (size() < 4) {
return 0;
}
quint32 res = ((quint8) at(0)) << 24 |
((quint8) at(1)) << 16 |
((quint8) at(2)) << 8 |
((quint8) at(3));
remove(0, 4);
return res;
}
qint16 VByteArray::vbPopFrontInt16()
{
if (size() < 2) {
return 0;
}
qint16 res = ((quint8) at(0)) << 8 |
((quint8) at(1));
remove(0, 2);
return res;
}
quint16 VByteArray::vbPopFrontUint16()
{
if (size() < 2) {
return 0;
}
quint16 res = ((quint8) at(0)) << 8 |
((quint8) at(1));
remove(0, 2);
return res;
}
qint8 VByteArray::vbPopFrontInt8()
{
if (size() < 1) {
return 0;
}
qint8 res = (quint8)at(0);
remove(0, 1);
return res;
}
quint8 VByteArray::vbPopFrontUint8()
{
if (size() < 1) {
return 0;
}
quint8 res = (quint8)at(0);
remove(0, 1);
return res;
}
double VByteArray::vbPopFrontDouble64(double scale)
{
return (double)vbPopFrontInt64() / scale;
}
double VByteArray::vbPopFrontDouble32(double scale)
{
return (double)vbPopFrontInt32() / scale;
}
double VByteArray::vbPopFrontDouble16(double scale)
{
return (double)vbPopFrontInt16() / scale;
}
double VByteArray::vbPopFrontDouble32Auto()
{
uint32_t res = vbPopFrontUint32();
int e = (res >> 23) & 0xFF;
int fr = res & 0x7FFFFF;
bool negative = res & (1 << 31);
float f = 0.0;
if (e != 0 || fr != 0) {
f = (float)fr / (8388608.0 * 2.0) + 0.5;
e -= 126;
}
if (negative) {
f = -f;
}
return ldexpf(f, e);
}
double VByteArray::vbPopFrontDouble64Auto()
{
double n = vbPopFrontDouble32Auto();
double err = vbPopFrontDouble32Auto();
return n + err;
}
QString VByteArray::vbPopFrontString()
{
if (size() < 1) {
return QString();
}
QString str(data());
remove(0, str.size() + 1);
return str;
}