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
|
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>
#include <stdint.h>
void
integer_unpack_u8(Py_buffer *in_view, Py_buffer *out_view)
{
Py_ssize_t in_size = in_view->shape[0];
Py_ssize_t in_index = 0;
Py_ssize_t out_index = 0;
uint8_t *in_data = in_view->buf;
uint32_t *out_data = out_view->buf;
while (in_index < in_size) {
uint32_t sum = in_data[in_index];
if (sum == UINT8_MAX) {
while (in_index + 1 < in_size) {
in_index += 1;
sum += in_data[in_index];
if (in_data[in_index] != UINT8_MAX) {
break;
}
}
}
out_data[out_index] = sum;
in_index += 1;
out_index += 1;
}
}
void
integer_unpack_u16(Py_buffer *in_view, Py_buffer *out_view)
{
Py_ssize_t in_size = in_view->shape[0];
Py_ssize_t in_index = 0;
Py_ssize_t out_index = 0;
uint16_t *in_data = in_view->buf;
uint32_t *out_data = out_view->buf;
while (in_index < in_size) {
uint32_t sum = in_data[in_index];
if (sum == UINT16_MAX) {
while (in_index + 1 < in_size) {
in_index += 1;
sum += in_data[in_index];
if (in_data[in_index] != UINT16_MAX) {
break;
}
}
}
out_data[out_index] = sum;
in_index += 1;
out_index += 1;
}
}
void
integer_unpack_i8(Py_buffer *in_view, Py_buffer *out_view)
{
Py_ssize_t in_size = in_view->shape[0];
Py_ssize_t in_index = 0;
Py_ssize_t out_index = 0;
int8_t *in_data = in_view->buf;
int32_t *out_data = out_view->buf;
while (in_index < in_size) {
int32_t sum = in_data[in_index];
if (sum == INT8_MAX || sum == INT8_MIN) {
while (in_index + 1 < in_size) {
in_index += 1;
sum += in_data[in_index];
if (in_data[in_index] != INT8_MAX && in_data[in_index] != INT8_MIN) {
break;
}
}
}
out_data[out_index] = sum;
in_index += 1;
out_index += 1;
}
}
void
integer_unpack_i16(Py_buffer *in_view, Py_buffer *out_view)
{
Py_ssize_t in_size = in_view->shape[0];
Py_ssize_t in_index = 0;
Py_ssize_t out_index = 0;
int16_t *in_data = in_view->buf;
int32_t *out_data = out_view->buf;
while (in_index < in_size) {
int32_t sum = in_data[in_index];
if (sum == INT16_MAX || sum == INT16_MIN) {
while (in_index + 1 < in_size) {
in_index += 1;
sum += in_data[in_index];
if (in_data[in_index] != INT16_MAX && in_data[in_index] != INT16_MIN) {
break;
}
}
}
out_data[out_index] = sum;
in_index += 1;
out_index += 1;
}
}
static PyObject *
integer_unpack(PyObject *self, PyObject *args)
{
PyObject *in = NULL;
PyObject *out = NULL;
if (!PyArg_ParseTuple(args, "OO", &in, &out)) {
return NULL;
}
Py_buffer in_view, out_view;
const int flags = PyBUF_ND | PyBUF_FORMAT;
if (PyObject_GetBuffer(in, &in_view, flags) != 0) {
return NULL;
}
if (PyObject_GetBuffer(out, &out_view, flags | PyBUF_WRITABLE) != 0) {
PyBuffer_Release(&in_view);
return NULL;
}
if (in_view.ndim != 1) {
PyErr_SetString(PyExc_ValueError, "First argument should be one-dimensional.");
goto exit;
}
if (out_view.ndim != 1) {
PyErr_SetString(PyExc_ValueError, "Second argument should be one-dimensional.");
goto exit;
}
const char format = in_view.format[0];
if (format == 'B') {
integer_unpack_u8(&in_view, &out_view);
}
else if (format == 'H') {
integer_unpack_u16(&in_view, &out_view);
}
else if (format == 'b') {
integer_unpack_i8(&in_view, &out_view);
}
else if (format == 'h') {
integer_unpack_i16(&in_view, &out_view);
}
else {
PyErr_Format(PyExc_ValueError,
"Unexpected buffer format: %s",
in_view.format);
}
exit:
PyBuffer_Release(&in_view);
PyBuffer_Release(&out_view);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef IntegerUnpackMethods[] = {
{"integer_unpack", integer_unpack, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_bcif_helper",
NULL,
-1,
IntegerUnpackMethods
};
PyMODINIT_FUNC
PyInit__bcif_helper(void)
{
PyObject *m;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
}
return m;
}
|