Debian Codegraph @

mypyc/lib-rt/byteswriter_extra_ops.h

raw
1 #ifndef BYTESWRITER_EXTRA_OPS_H
2 #define BYTESWRITER_EXTRA_OPS_H
3
4 #include <Python.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7
8 #include "mypyc_util.h"
9 #include "strings/librt_strings_api.h"
10 #include "strings/librt_strings_common.h"
11
12 // BytesWriter: Length and capacity
13
14 static inline CPyTagged
15 CPyBytesWriter_Len(PyObject *obj) {
16 return (CPyTagged)((BytesWriterObject *)obj)->len << 1;
17 }
18
19 static inline bool
20 CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) {
21 if (likely(data->capacity - data->len >= n)) {
22 return true;
23 } else {
24 return LibRTStrings_ByteWriter_grow_buffer_internal(data, n);
25 }
26 }
27
28 // BytesWriter: Basic write operations
29
30 static inline char
31 CPyBytesWriter_Append(PyObject *obj, uint8_t value) {
32 BytesWriterObject *self = (BytesWriterObject *)obj;
33 // Store length in a local variable to enable additional optimizations
34 Py_ssize_t len = self->len;
35 if (!CPyBytesWriter_EnsureSize(self, 1))
36 return CPY_NONE_ERROR;
37 self->buf[len] = value;
38 self->len = len + 1;
39 return CPY_NONE;
40 }
41
42 char CPyBytesWriter_Write(PyObject *obj, PyObject *value);
43
44 // BytesWriter: Indexing operations
45
46 // If index is negative, convert to non-negative index (no range checking)
47 static inline int64_t CPyBytesWriter_AdjustIndex(PyObject *obj, int64_t index) {
48 if (index < 0) {
49 return index + ((BytesWriterObject *)obj)->len;
50 }
51 return index;
52 }
53
54 static inline bool CPyBytesWriter_RangeCheck(PyObject *obj, int64_t index) {
55 return index >= 0 && index < ((BytesWriterObject *)obj)->len;
56 }
57
58 static inline uint8_t CPyBytesWriter_GetItem(PyObject *obj, int64_t index) {
59 return (((BytesWriterObject *)obj)->buf)[index];
60 }
61
62 static inline void CPyBytesWriter_SetItem(PyObject *obj, int64_t index, uint8_t x) {
63 (((BytesWriterObject *)obj)->buf)[index] = x;
64 }
65
66 // BytesWriter: Write integer operations (little-endian)
67
68 static inline char
69 CPyBytesWriter_WriteI16LE(PyObject *obj, int16_t value) {
70 BytesWriterObject *self = (BytesWriterObject *)obj;
71 if (!CPyBytesWriter_EnsureSize(self, 2))
72 return CPY_NONE_ERROR;
73 BytesWriter_WriteI16LEUnsafe(self, value);
74 return CPY_NONE;
75 }
76
77 static inline char
78 CPyBytesWriter_WriteI16BE(PyObject *obj, int16_t value) {
79 BytesWriterObject *self = (BytesWriterObject *)obj;
80 if (!CPyBytesWriter_EnsureSize(self, 2))
81 return CPY_NONE_ERROR;
82 BytesWriter_WriteI16BEUnsafe(self, value);
83 return CPY_NONE;
84 }
85
86 static inline char
87 CPyBytesWriter_WriteI32LE(PyObject *obj, int32_t value) {
88 BytesWriterObject *self = (BytesWriterObject *)obj;
89 if (!CPyBytesWriter_EnsureSize(self, 4))
90 return CPY_NONE_ERROR;
91 BytesWriter_WriteI32LEUnsafe(self, value);
92 return CPY_NONE;
93 }
94
95 static inline char
96 CPyBytesWriter_WriteI32BE(PyObject *obj, int32_t value) {
97 BytesWriterObject *self = (BytesWriterObject *)obj;
98 if (!CPyBytesWriter_EnsureSize(self, 4))
99 return CPY_NONE_ERROR;
100 BytesWriter_WriteI32BEUnsafe(self, value);
101 return CPY_NONE;
102 }
103
104 static inline char
105 CPyBytesWriter_WriteI64LE(PyObject *obj, int64_t value) {
106 BytesWriterObject *self = (BytesWriterObject *)obj;
107 if (!CPyBytesWriter_EnsureSize(self, 8))
108 return CPY_NONE_ERROR;
109 BytesWriter_WriteI64LEUnsafe(self, value);
110 return CPY_NONE;
111 }
112
113 static inline char
114 CPyBytesWriter_WriteI64BE(PyObject *obj, int64_t value) {
115 BytesWriterObject *self = (BytesWriterObject *)obj;
116 if (!CPyBytesWriter_EnsureSize(self, 8))
117 return CPY_NONE_ERROR;
118 BytesWriter_WriteI64BEUnsafe(self, value);
119 return CPY_NONE;
120 }
121
122 // BytesWriter: Write float operations
123
124 static inline char
125 CPyBytesWriter_WriteF32LE(PyObject *obj, double value) {
126 BytesWriterObject *self = (BytesWriterObject *)obj;
127 if (!CPyBytesWriter_EnsureSize(self, 4))
128 return CPY_NONE_ERROR;
129 BytesWriter_WriteF32LEUnsafe(self, (float)value);
130 return CPY_NONE;
131 }
132
133 static inline char
134 CPyBytesWriter_WriteF32BE(PyObject *obj, double value) {
135 BytesWriterObject *self = (BytesWriterObject *)obj;
136 if (!CPyBytesWriter_EnsureSize(self, 4))
137 return CPY_NONE_ERROR;
138 BytesWriter_WriteF32BEUnsafe(self, (float)value);
139 return CPY_NONE;
140 }
141
142 static inline char
143 CPyBytesWriter_WriteF64LE(PyObject *obj, double value) {
144 BytesWriterObject *self = (BytesWriterObject *)obj;
145 if (!CPyBytesWriter_EnsureSize(self, 8))
146 return CPY_NONE_ERROR;
147 BytesWriter_WriteF64LEUnsafe(self, value);
148 return CPY_NONE;
149 }
150
151 static inline char
152 CPyBytesWriter_WriteF64BE(PyObject *obj, double value) {
153 BytesWriterObject *self = (BytesWriterObject *)obj;
154 if (!CPyBytesWriter_EnsureSize(self, 8))
155 return CPY_NONE_ERROR;
156 BytesWriter_WriteF64BEUnsafe(self, value);
157 return CPY_NONE;
158 }
159
160 // Bytes: Read integer operations
161
162 // Helper function for bytes read error handling (negative index or out of range)
163 void CPyBytes_ReadError(int64_t index, Py_ssize_t size);
164
165 static inline int16_t
166 CPyBytes_ReadI16LE(PyObject *bytes_obj, int64_t index) {
167 // bytes_obj type is enforced by mypyc
168 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
169 if (unlikely(index < 0 || index > size - 2)) {
170 CPyBytes_ReadError(index, size);
171 return CPY_LL_INT_ERROR;
172 }
173 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
174 return CPyBytes_ReadI16LEUnsafe(data + index);
175 }
176
177 static inline int16_t
178 CPyBytes_ReadI16BE(PyObject *bytes_obj, int64_t index) {
179 // bytes_obj type is enforced by mypyc
180 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
181 if (unlikely(index < 0 || index > size - 2)) {
182 CPyBytes_ReadError(index, size);
183 return CPY_LL_INT_ERROR;
184 }
185 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
186 return CPyBytes_ReadI16BEUnsafe(data + index);
187 }
188
189 static inline int32_t
190 CPyBytes_ReadI32BE(PyObject *bytes_obj, int64_t index) {
191 // bytes_obj type is enforced by mypyc
192 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
193 if (unlikely(index < 0 || index > size - 4)) {
194 CPyBytes_ReadError(index, size);
195 return CPY_LL_INT_ERROR;
196 }
197 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
198 return CPyBytes_ReadI32BEUnsafe(data + index);
199 }
200
201 static inline int32_t
202 CPyBytes_ReadI32LE(PyObject *bytes_obj, int64_t index) {
203 // bytes_obj type is enforced by mypyc
204 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
205 if (unlikely(index < 0 || index > size - 4)) {
206 CPyBytes_ReadError(index, size);
207 return CPY_LL_INT_ERROR;
208 }
209 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
210 return CPyBytes_ReadI32LEUnsafe(data + index);
211 }
212
213 static inline int64_t
214 CPyBytes_ReadI64LE(PyObject *bytes_obj, int64_t index) {
215 // bytes_obj type is enforced by mypyc
216 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
217 if (unlikely(index < 0 || index > size - 8)) {
218 CPyBytes_ReadError(index, size);
219 return CPY_LL_INT_ERROR;
220 }
221 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
222 return CPyBytes_ReadI64LEUnsafe(data + index);
223 }
224
225 static inline int64_t
226 CPyBytes_ReadI64BE(PyObject *bytes_obj, int64_t index) {
227 // bytes_obj type is enforced by mypyc
228 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
229 if (unlikely(index < 0 || index > size - 8)) {
230 CPyBytes_ReadError(index, size);
231 return CPY_LL_INT_ERROR;
232 }
233 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
234 return CPyBytes_ReadI64BEUnsafe(data + index);
235 }
236
237 // Bytes: Read float operations
238
239 static inline double
240 CPyBytes_ReadF32LE(PyObject *bytes_obj, int64_t index) {
241 // bytes_obj type is enforced by mypyc
242 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
243 if (unlikely(index < 0 || index > size - 4)) {
244 CPyBytes_ReadError(index, size);
245 return CPY_FLOAT_ERROR;
246 }
247 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
248 return (double)CPyBytes_ReadF32LEUnsafe(data + index);
249 }
250
251 static inline double
252 CPyBytes_ReadF32BE(PyObject *bytes_obj, int64_t index) {
253 // bytes_obj type is enforced by mypyc
254 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
255 if (unlikely(index < 0 || index > size - 4)) {
256 CPyBytes_ReadError(index, size);
257 return CPY_FLOAT_ERROR;
258 }
259 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
260 return (double)CPyBytes_ReadF32BEUnsafe(data + index);
261 }
262
263 static inline double
264 CPyBytes_ReadF64LE(PyObject *bytes_obj, int64_t index) {
265 // bytes_obj type is enforced by mypyc
266 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
267 if (unlikely(index < 0 || index > size - 8)) {
268 CPyBytes_ReadError(index, size);
269 return CPY_FLOAT_ERROR;
270 }
271 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
272 return CPyBytes_ReadF64LEUnsafe(data + index);
273 }
274
275 static inline double
276 CPyBytes_ReadF64BE(PyObject *bytes_obj, int64_t index) {
277 // bytes_obj type is enforced by mypyc
278 Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj);
279 if (unlikely(index < 0 || index > size - 8)) {
280 CPyBytes_ReadError(index, size);
281 return CPY_FLOAT_ERROR;
282 }
283 const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj);
284 return CPyBytes_ReadF64BEUnsafe(data + index);
285 }
286
287 #endif