Skip to content

Commit 32f0b79

Browse files
committed
py: Implement sdiv/udiv for inline Thumb assembler.
1 parent 0d967b8 commit 32f0b79

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

py/emitinlinethumb.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,17 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
490490
src_b = get_arg_i(emit, op_str, pn_args[2], 0x7);
491491
}
492492
asm_thumb_format_2(emit->as, op_code, rlo_dest, rlo_src, src_b);
493+
} else if (strcmp(op_str, "sdiv") == 0) {
494+
op_code = 0xfb90; // sdiv high part
495+
mp_uint_t rd, rn, rm;
496+
op_sdiv_udiv:
497+
rd = get_arg_reg(emit, op_str, pn_args[0], 15);
498+
rn = get_arg_reg(emit, op_str, pn_args[1], 15);
499+
rm = get_arg_reg(emit, op_str, pn_args[2], 15);
500+
asm_thumb_op32(emit->as, op_code | rn, 0xf0f0 | (rd << 8) | rm);
501+
} else if (strcmp(op_str, "udiv") == 0) {
502+
op_code = 0xfbb0; // udiv high part
503+
goto op_sdiv_udiv;
493504
} else if (strcmp(op_str, "sub") == 0) {
494505
op_code = ASM_THUMB_FORMAT_2_SUB;
495506
goto op_format_2;

py/objfun.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
431431
return 0;
432432
} else if (obj == mp_const_true) {
433433
return 1;
434+
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
435+
return mp_obj_int_get_truncated(obj);
434436
} else if (MP_OBJ_IS_STR(obj)) {
435437
// pointer to the string (it's probably constant though!)
436438
mp_uint_t l;

tests/inlineasm/asmdiv.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@micropython.asm_thumb
2+
def sdiv(r0, r1):
3+
sdiv(r0, r0, r1)
4+
5+
@micropython.asm_thumb
6+
def udiv(r0, r1):
7+
udiv(r0, r0, r1)
8+
9+
print(sdiv(1234, 3))
10+
print(sdiv(-1234, 3))
11+
print(sdiv(1234, -3))
12+
print(sdiv(-1234, -3))
13+
14+
print(udiv(1234, 3))
15+
print(udiv(0xffffffff, 0x7fffffff))
16+
print(udiv(0xffffffff, 0xffffffff))

tests/inlineasm/asmdiv.py.exp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
411
2+
-411
3+
-411
4+
411
5+
411
6+
2
7+
1

0 commit comments

Comments
 (0)