8000 py: Add mp_binary_set_val_array_from_int, to store an int directly. · errordeveloper/micropython@71e9bfa · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 71e9bfa

Browse files
committed
py: Add mp_binary_set_val_array_from_int, to store an int directly.
1 parent b11b85a commit 71e9bfa

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

py/binary.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,21 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
151151
}
152152

153153
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
154-
machine_int_t val = 0;
155-
if (MP_OBJ_IS_INT(val_in)) {
156-
val = mp_obj_int_get(val_in);
154+
switch (typecode) {
155+
#if MICROPY_ENABLE_FLOAT
156+
case 'f':
157+
((float*)p)[index] = mp_obj_float_get(val_in);
158+
break;
159+
case 'd':
160+
((double*)p)[index] = mp_obj_float_get(val_in);
161+
break;
162+
#endif
163+
default:
164+
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
157165
}
166+
}
158167

168+
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val) {
159169
switch (typecode) {
160170
case 'b':
161171
((int8_t*)p)[index] = val;
@@ -187,10 +197,10 @@ void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in)
187197
#endif
188198
#if MICROPY_ENABLE_FLOAT
189199
case 'f':
190-
((float*)p)[index] = mp_obj_float_get(val_in);
200+
((float*)p)[index] = val;
191201
break;
192202
case 'd':
193-
((double*)p)[index] = mp_obj_float_get(val_in);
203+
((double*)p)[index] = val;
194204
break;
195205
#endif
196206
}

py/binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ int mp_binary_get_size(char typecode);
66
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index);
77
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
88
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in);
9+
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val);

0 commit comments

Comments
 (0)
0