MPY C Extension - Cannot do division or modulo

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

MPY C Extension - Cannot do division or modulo

Post by hippy » Mon Sep 27, 2021 12:55 am

Code: Select all

#include "py/dynruntime.h"

STATIC mp_obj_t div(mp_obj_t lhs_obj, mp_obj_t rhs_obj) {
    mp_int_t lhs = mp_obj_get_int(lhs_obj);
    mp_int_t rhs = mp_obj_get_int(rhs_obj);
    mp_int_t res = lhs / rhs;                                      // <---
    return mp_obj_new_int(res);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(div_obj, div);

mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
    MP_DYNRUNTIME_INIT_ENTRY
    mp_store_global(MP_QSTR_div, MP_OBJ_FROM_PTR(&div_obj));
    MP_DYNRUNTIME_INIT_EXIT
}

Code: Select all

pi@Pi3B:~/pico/micropython/ports/rp2/modules-c/divbug $ make
GEN build/divbug.config.h
CC divbug.c
LINK build/divbug.o
LinkError: build/divbug.o: undefined symbol: __aeabi_idiv
make: *** [/home/pi/pico/micropython/py/dynruntime.mk:154: build/divbug.native.mpy] Error 1
Add, subtract and multiply work but what do I need to add to have division and modulo linking successfully ?

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: MPY C Extension - Cannot do division or modulo

Post by hippy » Mon Sep 27, 2021 5:56 pm

I have solved my immediate problem by including my own (poor) implementation of "__aeabi_idiv" in the module's source code, but it would be nice not to have to do it that way.

https://www.raspberrypi.org/forums/view ... 5#p1918255

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: MPY C Extension - Cannot do division or modulo

Post by dhylands » Mon Sep 27, 2021 5:59 pm

That helper function is normally implemented in libgcc.a that comes with the compiler.

Post Reply