From 8c77a5d2562d9dd80966acb8c5efe05e65444b66 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 20 Mar 2022 22:53:51 +0100 Subject: [PATCH] Add "16bit * 8bit / 8bit" to fixed point math library --- include/fixmath.c | 89 +++++++++++++++++++++++++++++++++ include/fixmath.h | 5 ++ oscar64/NativeCodeGenerator.cpp | 7 ++- 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/include/fixmath.c b/include/fixmath.c index edaaac6..f0bbd6b 100644 --- a/include/fixmath.c +++ b/include/fixmath.c @@ -426,3 +426,92 @@ int lmuldiv16s(int a, int b, int c) E1: } } + + +unsigned lmuldiv16by8(unsigned a, char b, char c) +{ + __asm { + + lda #0 + sta accu + 0 + sta accu + 1 + sta accu + 2 + sta accu + 3 + + lda b + beq z1 + + lda c + l1: + asl + bcs e1 + cmp b + bcs e2 + + asl a + 0 + rol a + 1 + jmp l1 + e2: + clc + e1: + ror + sta c + + ldx #16 + l2: + lda b + sec + sbc c + bcc w1 + sta b + + clc + lda accu + 2 + adc a + sta accu + 2 + lda accu + 3 + adc a + 1 + sta accu + 3 + bcc * + 8 + inc accu + 0 + bne * + 4 + inc accu + 1 + + w1: + asl accu + 2 + rol accu + 3 + rol accu + 0 + rol accu + 1 + + asl b + bcc w2 + lda b + sbc c + sta b + + clc + lda accu + 2 + adc a + sta accu + 2 + lda accu + 3 + adc a + 1 + sta accu + 3 + bcc * + 8 + inc accu + 0 + bne * + 4 + inc accu + 1 + + w2: + dex + bne l2 + z1: + } +} + +int lmuldiv16sby8(int a, char b, char c) +{ + if (a < 0) + return -(int)lmuldiv16by8(-a, b, c); + else + return lmuldiv16by8(a, b, c); +} diff --git a/include/fixmath.h b/include/fixmath.h index e770547..277f005 100644 --- a/include/fixmath.h +++ b/include/fixmath.h @@ -37,6 +37,11 @@ __native unsigned lmuldiv16u(unsigned a, unsigned b, unsigned c) // Multiply two signed 16bit numbers and divide the result by another signed 16bit number a * b / c __native int lmuldiv16s(int a, int b, int c) + +__native unsigned lmuldiv16by8(unsigned a, char b, char c); + +inline int lmuldiv16sby8(int a, char b, char c); + #pragma compile("fixmath.c") #endif diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 6d69416..e48790b 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -15325,8 +15325,11 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass } else if (mIns[i].mType == ASMIT_STY && (mIns[i].mMode == ASMIM_ZERO_PAGE || mIns[i].mMode == ASMIM_ABSOLUTE)) { - if (MoveStoreYUp(i)) - changed = true; + if (i > 1 || mIns[0].mType != ASMIT_STX) + { + if (MoveStoreYUp(i)) + changed = true; + } } else if (mIns[i].mType == ASMIT_LDY && mIns[i].mMode == ASMIM_ZERO_PAGE && !(mIns[i].mLive & LIVE_MEM)) {