Skip to content

Commit 192d536

Browse files
committed
py: Implement clz and rbit for inline Thumb assembler.
1 parent 32f0b79 commit 192d536

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

py/emitinlinethumb.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
363363
} else if (n_args == 2) {
364364
if (MP_PARSE_NODE_IS_ID(pn_args[1])) {
365365
// second arg is a register (or should be)
366-
mp_uint_t op_code;
366+
mp_uint_t op_code, op_code_hi;
367367
if (strcmp(op_str, "mov") == 0) {
368368
mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
369369
mp_uint_t reg_src = get_arg_reg(emit, op_str, pn_args[1], 15);
@@ -391,6 +391,18 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
391391
} else if (strcmp(op_str, "mul") == 0) { op_code = ASM_THUMB_FORMAT_4_MUL; goto op_format_4;
392392
} else if (strcmp(op_str, "bic") == 0) { op_code = ASM_THUMB_FORMAT_4_BIC; goto op_format_4;
393393
} else if (strcmp(op_str, "mvn") == 0) { op_code = ASM_THUMB_FORMAT_4_MVN; goto op_format_4;
394+
} else if (strcmp(op_str, "clz") == 0) {
395+
op_code_hi = 0xfab0;
396+
op_code = 0xf080;
397+
mp_uint_t rd, rm;
398+
op_clz_rbit:
399+
rd = get_arg_reg(emit, op_str, pn_args[0], 15);
400+
rm = get_arg_reg(emit, op_str, pn_args[1], 15);
401+
asm_thumb_op32(emit->as, op_code_hi | rm, op_code | (rd << 8) | rm);
402+
} else if (strcmp(op_str, "rbit") == 0) {
403+
op_code_hi = 0xfa90;
404+
op_code = 0xf0a0;
405+
goto op_clz_rbit;
394406
} else {
395407
goto unknown_op;
396408
}

tests/inlineasm/asmbitops.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@micropython.asm_thumb
2+
def clz(r0):
3+
clz(r0, r0)
4+
5+
print(clz(0xf0))
6+
print(clz(0x8000))
7+
8+
@micropython.asm_thumb
9+
def rbit(r0):
10+
rbit(r0, r0)
11+
12+
print(hex(rbit(0xf0)))
13+
print(hex(rbit(0x8000)))

tests/inlineasm/asmbitops.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
24
2+
16
3+
0xf000000
4+
0x10000

0 commit comments

Comments
 (0)