From 056b49e1d1bc12fa9a4958be7d373e8fe6f23cd2 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 4 Oct 2024 20:06:56 +0200 Subject: [PATCH] Optimize 8 by 16 bit multiply --- include/crt.c | 50 +++++++++++++++++++++++++++++++++ oscar64/NativeCodeGenerator.cpp | 8 ++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/include/crt.c b/include/crt.c index 8750d5c..08a6535 100644 --- a/include/crt.c +++ b/include/crt.c @@ -839,6 +839,55 @@ W1: asl accu #endif } +#if 1 +__asm mul16by8 +{ + lsr + beq zero + more: + ldx #0 + ldy #0 + bcc skip + odd: + ldy accu + ldx accu + 1 + bcs skip + + loop: + sta tmpy + + clc + tya + adc accu + tay + txa + adc accu + 1 + tax + + lda tmpy + skip: + asl accu + rol accu + 1 + lsr + bcc skip + bne loop + done: + clc + tya + adc accu + sta accu + txa + adc accu + 1 + sta accu + 1 + rts + zero: + bcs one + sta accu + sta accu + 1 + one: + rts +} +#else __asm mul16by8 { ldy #0 @@ -866,6 +915,7 @@ L2: sty tmp + 2 rts } +#endif __asm divs16 { diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 512863d..e966b63 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -9390,6 +9390,8 @@ int NativeCodeBasicBlock::ShortMultiply(InterCodeProcedure* proc, NativeCodeProc NativeCodeGenerator::Runtime& rt(nproc->mGenerator->ResolveRuntime(Ident::Unique("mul16by8"))); mIns.Push(NativeCodeInstruction(ins, ASMIT_JSR, ASMIM_ABSOLUTE, rt.mOffset, rt.mLinkerObject, NCIF_RUNTIME | NCIF_LOWER | NCIF_UPPER | NCIF_USE_CPU_REG_A)); + + return BC_REG_ACCU; } else { @@ -9425,6 +9427,8 @@ int NativeCodeBasicBlock::ShortMultiply(InterCodeProcedure* proc, NativeCodeProc NativeCodeGenerator::Runtime& rt(nproc->mGenerator->ResolveRuntime(Ident::Unique("mul16by8"))); mIns.Push(NativeCodeInstruction(ins, ASMIT_JSR, ASMIM_ABSOLUTE, rt.mOffset, rt.mLinkerObject, NCIF_RUNTIME | NCIF_LOWER | NCIF_UPPER | NCIF_USE_CPU_REG_A)); + + return BC_REG_ACCU; } return BC_REG_WORK + 2; @@ -10995,7 +10999,7 @@ NativeCodeBasicBlock* NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* p NativeCodeGenerator::Runtime& frt(nproc->mGenerator->ResolveRuntime(Ident::Unique("mul16by8"))); mIns.Push(NativeCodeInstruction(ins, ASMIT_JSR, ASMIM_ABSOLUTE, frt.mOffset, frt.mLinkerObject, NCIF_RUNTIME | NCIF_LOWER | NCIF_UPPER | NCIF_USE_CPU_REG_A)); - reg = BC_REG_WORK + 2; + reg = BC_REG_ACCU; } else if (ins->mOperator == IA_MUL && ins->mSrc[1].IsUByte()) { @@ -11019,7 +11023,7 @@ NativeCodeBasicBlock* NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* p NativeCodeGenerator::Runtime& frt(nproc->mGenerator->ResolveRuntime(Ident::Unique("mul16by8"))); mIns.Push(NativeCodeInstruction(ins, ASMIT_JSR, ASMIM_ABSOLUTE, frt.mOffset, frt.mLinkerObject, NCIF_RUNTIME | NCIF_LOWER | NCIF_UPPER | NCIF_USE_CPU_REG_A)); - reg = BC_REG_WORK + 2; + reg = BC_REG_ACCU; } #if 1 else if (ins->mOperator == IA_DIVS && ins->mSrc[0].mTemp < 0 && (ins->mSrc[0].mIntConst == 2 || ins->mSrc[0].mIntConst == 4 || ins->mSrc[0].mIntConst == 8))