summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
blob: 95e82a688d86305a6ab3116780519378acef539d (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
#include <stdint.h>
#include <assert.h>

#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "binary.h"

// Helpers to work with binary-encoded data

int mp_binary_get_size(char typecode) {
    // This assumes that unsigned and signed types are of the same type,
    // which is invariant for [u]intN_t.
    switch (typecode) {
        case BYTEARRAY_TYPECODE:
        case 'b':
        case 'B':
            return sizeof(int8_t);
        case 'h':
        case 'H':
            return sizeof(int16_t);
        case 'i':
        case 'I':
            return sizeof(int32_t);
        case 'l':
        case 'L':
            return sizeof(int32_t);
        case 'q':
        case 'Q':
            return sizeof(long long);
#if MICROPY_ENABLE_FLOAT
        case 'f':
            return sizeof(float);
        case 'd':
            return sizeof(double);
#endif
    }
    return -1;
}

mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
    machine_int_t val = 0;
    switch (typecode) {
        case 'b':
            val = ((int8_t*)p)[index];
            break;
        case BYTEARRAY_TYPECODE:
        case 'B':
            val = ((uint8_t*)p)[index];
            break;
        case 'h':
            val = ((int16_t*)p)[index];
            break;
        case 'H':
            val = ((uint16_t*)p)[index];
            break;
        case 'i':
        case 'l':
            return mp_obj_new_int(((int32_t*)p)[index]);
        case 'I':
        case 'L':
            return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
        case 'q':
        case 'Q':
            // TODO: Explode API more to cover signedness
            return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif
#if MICROPY_ENABLE_FLOAT
        case 'f':
            return mp_obj_new_float(((float*)p)[index]);
        case 'd':
            return mp_obj_new_float(((double*)p)[index]);
#endif
    }
    return MP_OBJ_NEW_SMALL_INT(val);
}

mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr) {
    machine_int_t val = 0;
    byte *p = *ptr;
    switch (typecode) {
        case 'b':
            val = (int8_t)*p++;
            break;
        case BYTEARRAY_TYPECODE:
        case 'B':
            val = *p++;
            break;
        case 'h':
            val = (int16_t)((p[1] << 8) | p[0]);
            break;
        case 'H':
            val = (p[1] << 8) | p[0];
            break;
        case 'i':
        case 'l':
            val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
            *ptr = p + 4;
            return mp_obj_new_int(val);
        case 'I':
        case 'L':
            val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
            *ptr = p + 4;
            return mp_obj_new_int_from_uint(val);
#if 0 //TODO
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
        case 'q':
        case 'Q':
            // TODO: Explode API more to cover signedness
            return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif
#if MICROPY_ENABLE_FLOAT
        case 'f':
            return mp_obj_new_float(((float*)p)[index]);
        case 'd':
            return mp_obj_new_float(((double*)p)[index]);
#endif
#endif
    }
    *ptr = p;
    return MP_OBJ_NEW_SMALL_INT(val);
}

void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
    machine_int_t val = 0;
    if (MP_OBJ_IS_INT(val_in)) {
        val = mp_obj_int_get(val_in);
    }

    switch (typecode) {
        case 'b':
            ((int8_t*)p)[index] = val;
            break;
        case BYTEARRAY_TYPECODE:
        case 'B':
            val = ((uint8_t*)p)[index] = val;
            break;
        case 'h':
            val = ((int16_t*)p)[index] = val;
            break;
        case 'H':
            val = ((uint16_t*)p)[index] = val;
            break;
        case 'i':
        case 'l':
            ((int32_t*)p)[index] = val;
            break;
        case 'I':
        case 'L':
            ((uint32_t*)p)[index] = val;
            break;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
        case 'q':
        case 'Q':
            assert(0);
            ((long long*)p)[index] = val;
            break;
#endif
#if MICROPY_ENABLE_FLOAT
        case 'f':
            ((float*)p)[index] = mp_obj_float_get(val_in);
            break;
        case 'd':
            ((double*)p)[index] = mp_obj_float_get(val_in);
            break;
#endif
    }
}