-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathbyteswriter_extra_ops.h
More file actions
291 lines (250 loc) · 9.24 KB
/
byteswriter_extra_ops.h
File metadata and controls
291 lines (250 loc) · 9.24 KB
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
#ifndef BYTESWRITER_EXTRA_OPS_H
#define BYTESWRITER_EXTRA_OPS_H
#ifdef MYPYC_EXPERIMENTAL
#include <stdbool.h>
#include <stdint.h>
#include <Python.h>
#include "mypyc_util.h"
#include "strings/librt_strings_api.h"
#include "strings/librt_strings_common.h"
// BytesWriter: Length and capacity
static inline CPyTagged
CPyBytesWriter_Len(PyObject *obj) {
return (CPyTagged)((BytesWriterObject *)obj)->len << 1;
}
static inline bool
CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) {
if (likely(data->capacity - data->len >= n)) {
return true;
} else {
return LibRTStrings_ByteWriter_grow_buffer_internal(data, n);
}
}
// BytesWriter: Basic write operations
static inline char
CPyBytesWriter_Append(PyObject *obj, uint8_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
// Store length in a local variable to enable additional optimizations
Py_ssize_t len = self->len;
if (!CPyBytesWriter_EnsureSize(self, 1))
return CPY_NONE_ERROR;
self->buf[len] = value;
self->len = len + 1;
return CPY_NONE;
}
char CPyBytesWriter_Write(PyObject *obj, PyObject *value);
// BytesWriter: Indexing operations
// If index is negative, convert to non-negative index (no range checking)
static inline int64_t CPyBytesWriter_AdjustIndex(PyObject *obj, int64_t index) {
if (index < 0) {
return index + ((BytesWriterObject *)obj)->len;
}
return index;
}
static inline bool CPyBytesWriter_RangeCheck(PyObject *obj, int64_t index) {
return index >= 0 && index < ((BytesWriterObject *)obj)->len;
}
static inline uint8_t CPyBytesWriter_GetItem(PyObject *obj, int64_t index) {
return (((BytesWriterObject *)obj)->buf)[index];
}
static inline void CPyBytesWriter_SetItem(PyObject *obj, int64_t index, uint8_t x) {
(((BytesWriterObject *)obj)->buf)[index] = x;
}
// BytesWriter: Write integer operations (little-endian)
static inline char
CPyBytesWriter_WriteI16LE(PyObject *obj, int16_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 2))
return CPY_NONE_ERROR;
BytesWriter_WriteI16LEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteI16BE(PyObject *obj, int16_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 2))
return CPY_NONE_ERROR;
BytesWriter_WriteI16BEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteI32LE(PyObject *obj, int32_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 4))
return CPY_NONE_ERROR;
BytesWriter_WriteI32LEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteI32BE(PyObject *obj, int32_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 4))
return CPY_NONE_ERROR;
BytesWriter_WriteI32BEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteI64LE(PyObject *obj, int64_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 8))
return CPY_NONE_ERROR;
BytesWriter_WriteI64LEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteI64BE(PyObject *obj, int64_t value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 8))
return CPY_NONE_ERROR;
BytesWriter_WriteI64BEUnsafe(self, value);
return CPY_NONE;
}
// BytesWriter: Write float operations
static inline char
CPyBytesWriter_WriteF32LE(PyObject *obj, double value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 4))
return CPY_NONE_ERROR;
BytesWriter_WriteF32LEUnsafe(self, (float)value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteF32BE(PyObject *obj, double value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 4))
return CPY_NONE_ERROR;
BytesWriter_WriteF32BEUnsafe(self, (float)value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteF64LE(PyObject *obj, double value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 8))
return CPY_NONE_ERROR;
BytesWriter_WriteF64LEUnsafe(self, value);
return CPY_NONE;
}
static inline char
CPyBytesWriter_WriteF64BE(PyObject *obj, double value) {
BytesWriterObject *self = (BytesWriterObject *)obj;
if (!CPyBytesWriter_EnsureSize(self, 8))
return CPY_NONE_ERROR;
BytesWriter_WriteF64BEUnsafe(self, value);
return CPY_NONE;
}
// Bytes: Read integer operations
// Helper function for bytes read error handling (negative index or out of range)
void CPyBytes_ReadError(int64_t index, Py_ssize_t size);
static inline int16_t
CPyBytes_ReadI16LE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 2)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI16LEUnsafe(data + index);
}
static inline int16_t
CPyBytes_ReadI16BE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 2)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI16BEUnsafe(data + index);
}
static inline int32_t
CPyBytes_ReadI32BE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 4)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI32BEUnsafe(data + index);
}
static inline int32_t
CPyBytes_ReadI32LE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 4)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI32LEUnsafe(data + index);
}
static inline int64_t
CPyBytes_ReadI64LE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 8)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI64LEUnsafe(data + index);
}
static inline int64_t
CPyBytes_ReadI64BE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 8)) {
CPyBytes_ReadError(index, size);
return CPY_LL_INT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadI64BEUnsafe(data + index);
}
// Bytes: Read float operations
static inline double
CPyBytes_ReadF32LE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 4)) {
CPyBytes_ReadError(index, size);
return CPY_FLOAT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return (double)CPyBytes_ReadF32LEUnsafe(data + index);
}
static inline double
CPyBytes_ReadF32BE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 4)) {
CPyBytes_ReadError(index, size);
return CPY_FLOAT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return (double)CPyBytes_ReadF32BEUnsafe(data + index);
}
static inline double
CPyBytes_ReadF64LE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 8)) {
CPyBytes_ReadError(index, size);
return CPY_FLOAT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadF64LEUnsafe(data + index);
}
static inline double
CPyBytes_ReadF64BE(PyObject *bytes_obj, int64_t index) {
// bytes_obj type is enforced by mypyc
Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
if (unlikely(index < 0 || index > size - 8)) {
CPyBytes_ReadError(index, size);
return CPY_FLOAT_ERROR;
}
const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
return CPyBytes_ReadF64BEUnsafe(data + index);
}
#endif // MYPYC_EXPERIMENTAL
#endif