* Properly register submodules of ulab This is related to * https://github.com/adafruit/circuitpython/issues/6066 in which, after the merge of 1.18 into CircuitPython, we lost the ability to import submodules of built-in modules. While reconstructing the changes we had made locally to enable this, I discovered that there was an easier way: simply register the dotted module names via MP_REGISTER_MODULE. * Fix finding processor count when no `python` executable is installed debian likes to install only `python3`, and not `python` (which was, for many decades, python2). This was previously done for `build.sh` but not for `build-cp.sh`. * Only use this submodule feature in CircuitPython .. as it does not work properly in MicroPython. Also, modules to be const. This saves a small amount of RAM * Fix -Werror=undef diagnostic Most CircuitPython ports build with -Werror=undef, so that use of an undefined preprocessor flag is an error. Also, CircuitPython's micropython version is old enough that MICROPY_VERSION is not (ever) defined. Defensively check for this macro being defined, and use the older style of MP_REGISTER_MODULE when it is not. * Fix -Werror=discarded-qualifiers diagnostics Most CircuitPython ports build with -Werror=discarded-qualifiers. This detected a problem where string constants were passed to functions with non-constant parameter types. * bump version number * Use MicroPython-compatible registration of submodules * straggler * Remove spurious casts these were build errors for micropython * Run tests for both nanbox and regular variant during CI
102 lines
3.5 KiB
C
102 lines
3.5 KiB
C
|
|
/*
|
|
* This file is part of the micropython-ulab project,
|
|
*
|
|
* https://github.com/v923z/micropython-ulab
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2020-2021 Zoltán Vörös
|
|
*/
|
|
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "py/obj.h"
|
|
#include "py/runtime.h"
|
|
#include "py/misc.h"
|
|
#include "user.h"
|
|
|
|
#if ULAB_HAS_USER_MODULE
|
|
|
|
//| """This module should hold arbitrary user-defined functions."""
|
|
//|
|
|
|
|
static mp_obj_t user_square(mp_obj_t arg) {
|
|
// the function takes a single dense ndarray, and calculates the
|
|
// element-wise square of its entries
|
|
|
|
// raise a TypeError exception, if the input is not an ndarray
|
|
if(!mp_obj_is_type(arg, &ulab_ndarray_type)) {
|
|
mp_raise_TypeError(translate("input must be an ndarray"));
|
|
}
|
|
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(arg);
|
|
|
|
// make sure that the input is a dense array
|
|
if(!ndarray_is_dense(ndarray)) {
|
|
mp_raise_TypeError(translate("input must be a dense ndarray"));
|
|
}
|
|
|
|
// if the input is a dense array, create `results` with the same number of
|
|
// dimensions, shape, and dtype
|
|
ndarray_obj_t *results = ndarray_new_dense_ndarray(ndarray->ndim, ndarray->shape, ndarray->dtype);
|
|
|
|
// since in a dense array the iteration over the elements is trivial, we
|
|
// can cast the data arrays ndarray->array and results->array to the actual type
|
|
if(ndarray->dtype == NDARRAY_UINT8) {
|
|
uint8_t *array = (uint8_t *)ndarray->array;
|
|
uint8_t *rarray = (uint8_t *)results->array;
|
|
for(size_t i=0; i < ndarray->len; i++, array++) {
|
|
*rarray++ = (*array) * (*array);
|
|
}
|
|
} else if(ndarray->dtype == NDARRAY_INT8) {
|
|
int8_t *array = (int8_t *)ndarray->array;
|
|
int8_t *rarray = (int8_t *)results->array;
|
|
for(size_t i=0; i < ndarray->len; i++, array++) {
|
|
*rarray++ = (*array) * (*array);
|
|
}
|
|
} else if(ndarray->dtype == NDARRAY_UINT16) {
|
|
uint16_t *array = (uint16_t *)ndarray->array;
|
|
uint16_t *rarray = (uint16_t *)results->array;
|
|
for(size_t i=0; i < ndarray->len; i++, array++) {
|
|
*rarray++ = (*array) * (*array);
|
|
}
|
|
} else if(ndarray->dtype == NDARRAY_INT16) {
|
|
int16_t *array = (int16_t *)ndarray->array;
|
|
int16_t *rarray = (int16_t *)results->array;
|
|
for(size_t i=0; i < ndarray->len; i++, array++) {
|
|
*rarray++ = (*array) * (*array);
|
|
}
|
|
} else { // if we end up here, the dtype is NDARRAY_FLOAT
|
|
mp_float_t *array = (mp_float_t *)ndarray->array;
|
|
mp_float_t *rarray = (mp_float_t *)results->array;
|
|
for(size_t i=0; i < ndarray->len; i++, array++) {
|
|
*rarray++ = (*array) * (*array);
|
|
}
|
|
}
|
|
// at the end, return a micrppython object
|
|
return MP_OBJ_FROM_PTR(results);
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(user_square_obj, user_square);
|
|
|
|
static const mp_rom_map_elem_t ulab_user_globals_table[] = {
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_user) },
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_square), (mp_obj_t)&user_square_obj },
|
|
};
|
|
|
|
static MP_DEFINE_CONST_DICT(mp_module_ulab_user_globals, ulab_user_globals_table);
|
|
|
|
const mp_obj_module_t ulab_user_module = {
|
|
.base = { &mp_type_module },
|
|
.globals = (mp_obj_dict_t*)&mp_module_ulab_user_globals,
|
|
};
|
|
#if CIRCUITPY_ULAB
|
|
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
|
|
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module, ULAB_HAS_USER_MODULE);
|
|
#else
|
|
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module);
|
|
#endif
|
|
#endif
|
|
|
|
#endif
|