Skip to content

Commit

Permalink
inline sqrtf function call
Browse files Browse the repository at this point in the history
  • Loading branch information
lurk101 committed Sep 13, 2024
1 parent 4b1c395 commit 6a2fcaa
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 502 deletions.
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,10 @@ add_executable(${PSHELL}
main.c
)

if ("${PICO_BOARD}" STREQUAL "pico2")
target_compile_definitions(${PSHELL} PUBLIC
PICO_MALLOC_PANIC=0
PSHELL_GIT_TAG=\"${PSHELL_GIT_TAG}\"
PICO2350=1
)
else()
target_compile_definitions(${PSHELL} PUBLIC
PICO_MALLOC_PANIC=0
PSHELL_GIT_TAG=\"${PSHELL_GIT_TAG}\"
PICO2350=0
)
endif()


if (FORCE_TESTS)
target_compile_definitions(${PSHELL} PUBLIC PSHELL_TESTS)
Expand Down
446 changes: 223 additions & 223 deletions cc/2040/cc_extrns.h

Large diffs are not rendered by default.

444 changes: 222 additions & 222 deletions cc/2350/cc_extrns.h

Large diffs are not rendered by default.

92 changes: 56 additions & 36 deletions cc/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ extern void get_screen_xy(int* x, int* y); // retrieve screem dimensio
extern void cc_exit(int rc); // C exit function
extern char __StackLimit[TEXT_BYTES + DATA_BYTES]; // start of code segment

// rp2350 sqrt float operation
static void wrap_sqrtf() {
asm volatile(" vmov s15,r0\n"
" vsqrt.f32 s15,s15\n"
" vmov r0, s15");
}

#if !PICO2350
#if !PICO_RP2350
// SDK floating point functions
void __wrap___aeabi_idiv();
void __wrap___aeabi_i2f();
Expand Down Expand Up @@ -105,7 +98,7 @@ void __wrap_asinhf();
void __wrap_acoshf();
void __wrap_atanhf();

#if !PICO2350
#if !PICO_RP2350
enum {
aeabi_idiv = 1,
aeabi_i2f,
Expand Down Expand Up @@ -393,6 +386,10 @@ static int wrap_popcount(int n) { return __builtin_popcount(n); };
static int wrap_printf(void){};
static int wrap_sprintf(void){};

#define IS_PRINTF(a) (!strcmp((a)->name, "printf"))
#define IS_SPRINTF(a) (!strcmp((a)->name, "sprintf"))
#define IS_SQRTF(a) (!strcmp((a)->name, "sqrtf"))

static int wrap_remove(char* name) { return fs_remove(full_path(name)); };

static int wrap_rename(char* old, char* new) {
Expand Down Expand Up @@ -430,8 +427,6 @@ struct externs_s {
const struct define_grp* grp; // help group
const void* extrn; // function address
const int ret_float : 1; // returns float
const int is_printf : 1; // printf function
const int is_sprintf : 1; // sprintf function
};

static const struct externs_s externs[] = {
Expand Down Expand Up @@ -1268,7 +1263,7 @@ static void expr(int lev) {
if (d->etype != tt) {
if (d->class == Func)
fatal("argument type mismatch");
else if (!externs[d->val].is_printf && !externs[d->val].is_sprintf)
else if ((!IS_PRINTF(&externs[d->val])) && (!IS_SPRINTF(&externs[d->val])))
fatal("argument type mismatch");
}
next();
Expand Down Expand Up @@ -2358,7 +2353,7 @@ static uint16_t pat12[] = {0x2000, 0x4438, 0x6800};
static uint16_t msk12[] = {0xff83, 0xffff, 0xffff};
static uint16_t rep12[] = {0x6838};

#if PICO2350
#if PICO_RP2350

// ldr r0,[r0,#0] vldr s15,[r0]
// pop {r1} vpop {s14}
Expand Down Expand Up @@ -2391,6 +2386,21 @@ static uint16_t pat16[] = {0xee17, 0x0a90, 0xee07, 0x0a90};
static uint16_t msk16[] = {0xff83, 0xffff, 0xffff, 0xffff};
static uint16_t rep16[] = {0xee17, 0x0a90};

// vmov r0,s15 vmov r0,s15
// push {r0} push {r0}
// vmov s15,r0

static uint16_t pat17[] = {0xee17, 0x0a90, 0xb401, 0xee07, 0x0a90};
static uint16_t msk17[] = {0xff83, 0xffff, 0xffff, 0xffff, 0xffff};
static uint16_t rep17[] = {0xee17, 0x0a90, 0xb401};

// push {r0} vmov s15,r0
// vpop {s15}

static uint16_t pat18[] = {0xb401, 0xecfd, 0x7a01};
static uint16_t msk18[] = {0xff83, 0xffff, 0xffff};
static uint16_t rep18[] = {0xee07, 0x0a90};

#endif

struct subs {
Expand Down Expand Up @@ -2421,11 +2431,13 @@ static const struct segs {
{NUMOF(pat10), NUMOF(rep10), 1, pat10, msk10, rep10, {{1, 1, 0}, {}}},
{NUMOF(pat11), NUMOF(rep11), 0, pat11, msk11, rep11, {{}, {}}},
{NUMOF(pat12), NUMOF(rep12), 1, pat12, msk12, rep12, {{0, 0, 4}, {}}},
#if PICO2350
#if PICO_RP2350
{NUMOF(pat13), NUMOF(rep13), 0, pat13, msk13, rep13, {{}, {}}},
{NUMOF(pat14), NUMOF(rep14), 0, pat14, msk14, rep14, {{}, {}}},
{NUMOF(pat15), NUMOF(rep15), 0, pat15, msk15, rep15, {{}, {}}},
{NUMOF(pat16), NUMOF(rep16), 0, pat16, msk16, rep16, {{}, {}}},
{NUMOF(pat17), NUMOF(rep17), 0, pat17, msk17, rep17, {{}, {}}},
{NUMOF(pat18), NUMOF(rep18), 0, pat18, msk18, rep18, {{}, {}}},
#endif
};

Expand Down Expand Up @@ -2643,7 +2655,7 @@ static void emit_branch(uint16_t* to) {
emit_call((int)(to + 2));
}

#if !PICO2350
#if !PICO_RP2350
static void emit_fop(int n) {
if (!ofn) // if exe output emit negative external function index
emit_load_long_imm(3, (int)fops[n], 0);
Expand Down Expand Up @@ -2783,7 +2795,7 @@ static void emit_oper(int op) {
case DIV:
emit(0x4601); // mov r1,r0
emit_pop(0); // pop {r0}
#if PICO2350
#if PICO_RP2350
emit(0xfb90);
emit(0xf0f1); // sdiv r0, r0, r1
#else
Expand All @@ -2794,7 +2806,7 @@ static void emit_oper(int op) {
case MOD:
emit(0x4601); // mov r1,r0
emit_pop(0); // pop {r0}
#if PICO2350
#if PICO_RP2350
emit(0xfb90);
emit(0xf3f1); // sdiv r3, r0, r1
emit(0xfb03);
Expand All @@ -2810,7 +2822,7 @@ static void emit_oper(int op) {
}
}

#if PICO2350
#if PICO_RP2350
static void emit_float_prefix(void) {
emit(0xee07);
emit(0x0a90); // vmov s15,r0
Expand All @@ -2831,7 +2843,7 @@ static void emit_float_oper(int op) {
switch (op) {
case ADDF:
emit_pop(1); // pop {r1}
#if PICO2350
#if PICO_RP2350
emit_float_prefix();
emit(0xee77);
emit(0x7a27); // vadd.f32 s15,s14,s15
Expand All @@ -2842,7 +2854,7 @@ static void emit_float_oper(int op) {
#endif
break;
case SUBF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_float_prefix();
emit(0xee77);
Expand All @@ -2857,7 +2869,7 @@ static void emit_float_oper(int op) {
break;
case MULF:
emit_pop(1);
#if PICO2350
#if PICO_RP2350
emit_float_prefix();
emit(0xee67);
emit(0x7a27); // vmul.f32 s15,s14,s15
Expand All @@ -2868,7 +2880,7 @@ static void emit_float_oper(int op) {
#endif
break;
case DIVF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_float_prefix();
emit(0xeec7);
Expand All @@ -2882,7 +2894,7 @@ static void emit_float_oper(int op) {
#endif
break;
case GEF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_cmp_prefix();
emit(0xbfac); // ite ge
Expand All @@ -2895,7 +2907,7 @@ static void emit_float_oper(int op) {
#endif
break;
case GTF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_cmp_prefix();
emit(0xbfcc); // ite gt
Expand All @@ -2908,7 +2920,7 @@ static void emit_float_oper(int op) {
#endif
break;
case LTF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_cmp_prefix();
emit(0xbf4c); // ite mi
Expand All @@ -2921,7 +2933,7 @@ static void emit_float_oper(int op) {
#endif
break;
case LEF:
#if PICO2350
#if PICO_RP2350
emit_pop(1);
emit_cmp_prefix();
emit(0xbf94); // ite ls
Expand All @@ -2948,7 +2960,7 @@ static void emit_float_oper(int op) {
static void emit_cast(int n) {
switch (n) {
case ITOF:
#if PICO2350
#if PICO_RP2350
emit(0xee07);
emit(0x0a90); // vmov s15,r0
emit(0xeef8);
Expand All @@ -2960,7 +2972,7 @@ static void emit_cast(int n) {
#endif
break;
case FTOI:
#if PICO2350
#if PICO_RP2350
emit(0xee07);
emit(0x0a90); // vmov s15,r0
emit(0xeefd);
Expand Down Expand Up @@ -3004,18 +3016,26 @@ static uint16_t* emit_call(int n) {

static void emit_syscall(int n, int np) {
const struct externs_s* p = externs + n;
if (p->is_printf) {
if (IS_PRINTF(p)) {
emit_load_immediate(0, np);
if (!ofn)
emit_load_long_imm(3, (int)x_printf, 0);
else
emit_load_long_imm(3, n, 1);
} else if (p->is_sprintf) {
} else if (IS_SPRINTF(p)) {
emit_load_immediate(0, np);
if (!ofn)
emit_load_long_imm(3, (int)x_sprintf, 0);
else
emit_load_long_imm(3, n, 1);
} else if (IS_SQRTF(p)) {
emit(0xecfd);
emit(0x7a01); // vpop {s15}
emit(0xeef1);
emit(0x7ae7); // vsqrt.f32 s15,s15
emit(0xee17);
emit(0x0a90); // vmov r0,s15
return;
} else {
int nparm = np & ADJ_MASK;
if (nparm > 4)
Expand All @@ -3029,7 +3049,7 @@ static void emit_syscall(int n, int np) {
}
emit(0x4798); // blx r3
int nparm = np & ADJ_MASK;
if (p->is_printf || p->is_sprintf)
if (IS_PRINTF(p) || IS_SPRINTF(p))
emit_adjust_stack(nparm);
else {
nparm = (nparm > 4) ? nparm - 4 : 0;
Expand Down Expand Up @@ -4294,7 +4314,7 @@ static void help(char* lib) {
if (!lib) {
printf("\n"
"usage: cc [-s] [-u] [-n]"
#if PICO2350
#if PICO_RP2350
" [-x]"
#endif
" [-h [lib]] [-D [symbol[ = value]]]\n"
Expand Down Expand Up @@ -4474,7 +4494,7 @@ int cc(int mode, int argc, char** argv) {
// optionally enable and add known symbols to disassembler tables
if (src_opt) {
disasm_init(&state, DISASM_ADDRESS | DISASM_INSTR | DISASM_COMMENT);
#if !PICO2350
#if !PICO_RP2350
disasm_symbol(&state, "idiv", (uint32_t)__wrap___aeabi_idiv, ARMMODE_THUMB);
disasm_symbol(&state, "i2f", (uint32_t)__wrap___aeabi_i2f, ARMMODE_THUMB);
disasm_symbol(&state, "f2i", (uint32_t)__wrap___aeabi_f2iz, ARMMODE_THUMB);
Expand Down Expand Up @@ -4665,15 +4685,15 @@ int cc(int mode, int argc, char** argv) {
}
int v = *((int*)addr);
if (v < 0)
#if !PICO2350
#if !PICO_RP2350
*((int*)addr) = (int)fops[-v];
#else
;
#endif
else {
if (externs[v].is_printf)
if (IS_PRINTF(&externs[v]))
*((int*)addr) = (int)x_printf;
else if (externs[v].is_sprintf)
else if (IS_SPRINTF(&externs[v]))
*((int*)addr) = (int)x_sprintf;
else
*((int*)addr) = (int)externs[v].extrn;
Expand Down
29 changes: 22 additions & 7 deletions disassembler/armdisasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ static bool thumb2_co_trans(ARMSTATE* state, uint32_t instr) {
return true;
}

#if PICO2350
#if PICO_RP2350
// FP decoders

static bool v_ldr(ARMSTATE* state, uint32_t instr) {
Expand All @@ -2045,6 +2045,14 @@ static bool v_pop(ARMSTATE* state, uint32_t instr) {
return true;
}

static bool v_pop15(ARMSTATE* state, uint32_t instr) {
state->size = 4;
strcpy(state->text, "vpop");
padinstr(state->text);
strcat(state->text, "{s15}");
return true;
}

static bool v_add_sub(ARMSTATE* state, uint32_t instr) {
state->size = 4;
switch (instr & 0xffff) {
Expand Down Expand Up @@ -2100,11 +2108,17 @@ static bool v_divf(ARMSTATE* state, uint32_t instr) {
return true;
}

static bool v_mrs(ARMSTATE* state, uint32_t instr) {
static bool v_mrs_sqrt(ARMSTATE* state, uint32_t instr) {
state->size = 4;
strcpy(state->text, "v_mrs");
padinstr(state->text);
strcat(state->text, "apsr_nzcv, fpscr");
if ((instr & 0xffff) == 0x7ae7) {
strcpy(state->text, "vsqrt");
padinstr(state->text);
strcat(state->text, "s15, s15");
} else {
strcpy(state->text, "v_mrs");
padinstr(state->text);
strcat(state->text, "apsr_nzcv, fpscr");
}
return true;
}

Expand Down Expand Up @@ -2149,11 +2163,11 @@ static bool v_mov_to(ARMSTATE* state, uint32_t instr) {
#endif

static const ENCODEMASK16 thumb_table[] = {
#if PICO2350
#if PICO_RP2350
// simple hack for floating point extensions
{0xffff, 0xee07, v_mov_from},
{0xffff, 0xee17, v_mov_to},
{0xffff, 0xeef1, v_mrs},
{0xffff, 0xeef1, v_mrs_sqrt},
{0xffff, 0xee77, v_add_sub},
{0xffff, 0xee67, v_mulf},
{0xffff, 0xeec7, v_divf},
Expand All @@ -2163,6 +2177,7 @@ static const ENCODEMASK16 thumb_table[] = {
{0xffff, 0xeeb0, v_mov},
{0xffff, 0xecbd, v_pop},
{0xfffe, 0xedd0, v_ldr},
{0xffff, 0xecfd, v_pop15},
// end of hack
#endif
{0xf800, 0x0000, thumb_lsl}, /* logical shift left by immediate, or MOV */
Expand Down
Loading

0 comments on commit 6a2fcaa

Please sign in to comment.