From f946abd2ee31bdae9e8f360b10f9a32c6007e51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20V=C3=B6r=C3=B6s?= Date: Wed, 9 Jun 2021 07:34:18 +0200 Subject: [PATCH] patch circuitpython --- code/ndarray.c | 83 ++++++++++++++++++++++++++++++++++++-- code/ndarray.h | 3 ++ code/numpy/vector/vector.c | 6 +-- code/ulab.c | 2 +- code/ulab_create.c | 6 +-- 5 files changed, 90 insertions(+), 10 deletions(-) diff --git a/code/ndarray.c b/code/ndarray.c index 6a4c7cf..5cc800e 100644 --- a/code/ndarray.c +++ b/code/ndarray.c @@ -20,6 +20,7 @@ #include "py/binary.h" #include "py/obj.h" #include "py/objtuple.h" +#include "py/objint.h" #include "ulab_tools.h" #include "ndarray.h" @@ -233,6 +234,82 @@ mp_uint_t ndarray_print_edgeitems = NDARRAY_PRINT_EDGEITEMS; //| """alternate constructor function for `ulab.ndarray`. Mirrors numpy.array""" //| ... + +#ifdef CIRCUITPY +void ndarray_set_value(char typecode, void *p, size_t index, mp_obj_t val_in) { + switch (typecode) { + #if MICROPY_PY_BUILTINS_FLOAT + case 'f': + ((float *)p)[index] = mp_obj_get_float_to_f(val_in); + break; + case 'd': + ((double *)p)[index] = mp_obj_get_float_to_d(val_in); + break; + #endif + // Extension to CPython: array of objects + case 'O': + ((mp_obj_t *)p)[index] = val_in; + break; + default: + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE + if (mp_obj_is_type(val_in, &mp_type_int)) { + size_t size = mp_binary_get_size('@', typecode, NULL); + mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG, + size, (uint8_t *)p + index * size); + return; + } + #endif + switch (typecode) { + case 'b': + ((signed char *)p)[index] = mp_obj_get_int(val_in); + break; + case BYTEARRAY_TYPECODE: + case 'B': + ((unsigned char *)p)[index] = mp_obj_get_int(val_in); + break; + case 'h': + ((short *)p)[index] = mp_obj_get_int(val_in); + break; + case 'H': + ((unsigned short *)p)[index] = mp_obj_get_int(val_in); + break; + case 'i': + ((int *)p)[index] = mp_obj_get_int(val_in); + break; + case 'I': + ((unsigned int *)p)[index] = mp_obj_get_int(val_in); + break; + case 'l': + ((long *)p)[index] = mp_obj_get_int(val_in); + break; + case 'L': + ((unsigned long *)p)[index] = mp_obj_get_int(val_in); + break; + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE + case 'q': + ((long long *)p)[index] = mp_obj_get_int(val_in); + break; + case 'Q': + ((unsigned long long *)p)[index] = mp_obj_get_int(val_in); + break; + #endif + #if MICROPY_PY_BUILTINS_FLOAT + case 'f': + ((float *)p)[index] = (float)mp_obj_get_int(val_in); + break; + case 'd': + ((double *)p)[index] = (double)mp_obj_get_int(val_in); + break; + #endif + // Extension to CPython: array of pointers + case 'P': + ((void **)p)[index] = (void *)(uintptr_t)mp_obj_get_int(val_in); + break; + } + } +} +#endif + #if defined(MICROPY_VERSION_MAJOR) && MICROPY_VERSION_MAJOR == 1 && MICROPY_VERSION_MINOR == 11 void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) { @@ -653,7 +730,7 @@ void ndarray_assign_elements(ndarray_obj_t *ndarray, mp_obj_t iterable, uint8_t } } else { while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - mp_binary_set_val_array(dtype, ndarray->array, (*idx)++, item); + ndarray_set_value(dtype, ndarray->array, (*idx)++, item); } } } @@ -1050,7 +1127,7 @@ STATIC mp_obj_t ndarray_make_new_core(const mp_obj_type_t *type, size_t n_args, } else { item = mp_binary_get_val_array(source->dtype, sarray, 0); } - mp_binary_set_val_array(dtype, tarray, 0, item); + ndarray_set_value(dtype, tarray, 0, item); tarray += target->itemsize; sarray += source->strides[ULAB_MAX_DIMS - 1]; l++; @@ -1734,7 +1811,7 @@ ndarray_obj_t *ndarray_from_mp_obj(mp_obj_t obj, uint8_t other_type) { } } ndarray = ndarray_new_linear_array(1, dtype); - mp_binary_set_val_array(dtype, ndarray->array, 0, obj); + ndarray_set_value(dtype, ndarray->array, 0, obj); } } else if(mp_obj_is_float(obj)) { ndarray = ndarray_new_linear_array(1, NDARRAY_FLOAT); diff --git a/code/ndarray.h b/code/ndarray.h index a7e2716..5123009 100644 --- a/code/ndarray.h +++ b/code/ndarray.h @@ -51,6 +51,9 @@ typedef struct _mp_obj_slice_t { #if !CIRCUITPY #define translate(x) MP_ERROR_TEXT(x) +#define ndarray_set_value(a, b, c, d) mp_binary_set_val_array(a, b, c, d) +#else +void ndarray_set_value(char , void *, size_t , mp_obj_t ); #endif #define NDARRAY_NUMERIC 0 diff --git a/code/numpy/vector/vector.c b/code/numpy/vector/vector.c index 6cca98c..d9d480f 100644 --- a/code/numpy/vector/vector.c +++ b/code/numpy/vector/vector.c @@ -550,7 +550,7 @@ static mp_obj_t vectorise_vectorized_function_call(mp_obj_t self_in, size_t n_ar for(size_t i=0; i < source->len; i++) { avalue[0] = mp_binary_get_val_array(source->dtype, source->array, i); fvalue = self->type->call(self->fun, 1, 0, avalue); - mp_binary_set_val_array(self->otypes, ndarray->array, i, fvalue); + ndarray_set_value(self->otypes, ndarray->array, i, fvalue); } return MP_OBJ_FROM_PTR(ndarray); } else if(mp_obj_is_type(args[0], &mp_type_tuple) || mp_obj_is_type(args[0], &mp_type_list) || @@ -562,14 +562,14 @@ static mp_obj_t vectorise_vectorized_function_call(mp_obj_t self_in, size_t n_ar size_t i=0; while ((avalue[0] = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { fvalue = self->type->call(self->fun, 1, 0, avalue); - mp_binary_set_val_array(self->otypes, ndarray->array, i, fvalue); + ndarray_set_value(self->otypes, ndarray->array, i, fvalue); i++; } return MP_OBJ_FROM_PTR(ndarray); } else if(mp_obj_is_int(args[0]) || mp_obj_is_float(args[0])) { ndarray_obj_t *ndarray = ndarray_new_linear_array(1, self->otypes); fvalue = self->type->call(self->fun, 1, 0, args); - mp_binary_set_val_array(self->otypes, ndarray->array, 0, fvalue); + ndarray_set_value(self->otypes, ndarray->array, 0, fvalue); return MP_OBJ_FROM_PTR(ndarray); } else { mp_raise_ValueError(translate("wrong input type")); diff --git a/code/ulab.c b/code/ulab.c index 2fa76c0..2890653 100644 --- a/code/ulab.c +++ b/code/ulab.c @@ -34,7 +34,7 @@ #include "user/user.h" #include "utils/utils.h" -#define ULAB_VERSION 2.10.0 +#define ULAB_VERSION 3.0.0 #define xstr(s) str(s) #define str(s) #s #define ULAB_VERSION_STRING xstr(ULAB_VERSION) xstr(-) xstr(ULAB_MAX_DIMS) xstr(D) diff --git a/code/ulab_create.c b/code/ulab_create.c index 63ce3cd..816db0a 100644 --- a/code/ulab_create.c +++ b/code/ulab_create.c @@ -55,7 +55,7 @@ static mp_obj_t create_zeros_ones_full(mp_obj_t oshape, uint8_t dtype, mp_obj_t } } for(size_t i=0; i < ndarray->len; i++) { - mp_binary_set_val_array(dtype, ndarray->array, i, value); + ndarray_set_value(dtype, ndarray->array, i, value); } } // if zeros calls the function, we don't have to do anything @@ -388,13 +388,13 @@ mp_obj_t create_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) size_t i = 0; if((args[2].u_int >= 0)) { while(k < m) { - mp_binary_set_val_array(dtype, ndarray->array, i*m+k, one); + ndarray_set_value(dtype, ndarray->array, i*m+k, one); k++; i++; } } else { while(k < n) { - mp_binary_set_val_array(dtype, ndarray->array, k*m+i, one); + ndarray_set_value(dtype, ndarray->array, k*m+i, one); k++; i++; }