From ef5cb81a1865e7e86136a90adbce6911c5519743 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Wed, 29 Sep 2021 16:31:17 +0200 Subject: [PATCH] Optimized int shifts ge eight --- autotest/autotest.bat | 6 ++ autotest/bitshifttest.c | 102 ++++++++++++++++++++++++++++++++ oscar64/NativeCodeGenerator.cpp | 32 +++++++++- oscar64/Preprocessor.cpp | 2 + 4 files changed, 141 insertions(+), 1 deletion(-) diff --git a/autotest/autotest.bat b/autotest/autotest.bat index 7c98a8c..5a2de9e 100644 --- a/autotest/autotest.bat +++ b/autotest/autotest.bat @@ -108,6 +108,12 @@ if %errorlevel% neq 0 goto :error ..\release\oscar64 -e -n asmtest.c if %errorlevel% neq 0 goto :error +..\release\oscar64 -e bitshifttest.c +if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -n bitshifttest.c +if %errorlevel% neq 0 goto :error + exit /b 0 :error echo Failed with error #%errorlevel%. diff --git a/autotest/bitshifttest.c b/autotest/bitshifttest.c index 38f8a77..7d7af03 100644 --- a/autotest/bitshifttest.c +++ b/autotest/bitshifttest.c @@ -1,4 +1,5 @@ #include +#include unsigned shl1b(int n) { @@ -73,22 +74,123 @@ unsigned shr8n(int n) #pragma native(shr8n) +void shl16b(unsigned xu, int xi) +{ + unsigned ua[16]; + int ia[16]; +#assign s 0 +#repeat + ua[s] = xu << s; + ia[s] = xi << s; +#assign s s + 1 +#until s == 16 + + for(int i=0; i<16; i++) + { + assert(ua[i] == xu << i); + assert(ia[i] == xi << i); + } +} + +void shr16b(unsigned xu, int xi) +{ + unsigned ua[16]; + int ia[16]; +#assign s 0 +#repeat + ua[s] = xu >> s; + ia[s] = xi >> s; +#assign s s + 1 +#until s == 16 + + for(int i=0; i<16; i++) + { + assert(ua[i] == xu >> i); + assert(ia[i] == xi >> i); + } +} + +void shl16n(unsigned xu, int xi) +{ + unsigned ua[16]; + int ia[16]; +#assign s 0 +#repeat + ua[s] = xu << s; + ia[s] = xi << s; +#assign s s + 1 +#until s == 16 + + for(int i=0; i<16; i++) + { + assert(ua[i] == xu << i); + assert(ia[i] == xi << i); + } +} + +void shr16n(unsigned xu, int xi) +{ + unsigned ua[16]; + int ia[16]; +#assign s 0 +#repeat + ua[s] = xu >> s; + ia[s] = xi >> s; +#assign s s + 1 +#until s == 16 + + for(int i=0; i<16; i++) + { + assert(ua[i] == xu >> i); + assert(ia[i] == xi >> i); + } +} + +#pragma native(shl16n) +#pragma native(shr16n) + int main(void) { for(int i=0; i<32; i++) { printf("1: %.4x : %.4x | %.4x : %.4x\n", shl1b(i), shl1n(i), shr1b(i), shr1n(i)); + assert(shl1b(i) == shl1n(i)); + assert(shr1b(i) == shr1n(i)); } for(int i=0; i<32; i++) { printf("4: %.4x : %.4x | %.4x : %.4x\n", shl4b(i), shl4n(i), shr4b(i), shr4n(i)); + assert(shl4b(i) == shl4n(i)); + assert(shr4b(i) == shr4n(i)); } for(int i=0; i<32; i++) { printf("8: %.4x : %.4x | %.4x : %.4x\n", shl8b(i), shl8n(i), shr8b(i), shr8n(i)); + assert(shl8b(i) == shl8n(i)); + assert(shr8b(i) == shr8n(i)); } + shl16b(0x0000, 0x0000); + shl16b(0xffff, 0xffff); + shl16b(0x1234, 0x1234); + shl16b(0xfedc, 0xfedc); + + shr16b(0x0000, 0x0000); + shr16b(0xffff, 0xffff); + shr16b(0x1234, 0x1234); + shr16b(0xfedc, 0xfedc); + + shl16n(0x0000, 0x0000); + shl16n(0xffff, 0xffff); + shl16n(0x1234, 0x1234); + shl16n(0xfedc, 0xfedc); + + shr16n(0x0000, 0x0000); + shr16n(0xffff, 0xffff); + shr16n(0x1234, 0x1234); + shr16n(0xfedc, 0xfedc); + return 0; } diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 3aaa7fa..4e96ebe 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -3790,6 +3790,15 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_ZERO_PAGE, treg + 1)); } } + else if (shift >= 8) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + proc->mTempOffset[ins->mSTemp[1]])); + for (int i = 8; i < shift; i++) + mIns.Push(NativeCodeInstruction(ASMIT_ASL, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 1)); + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0x00)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 0)); + } else { if (ins->mSTemp[1] != ins->mTTemp) @@ -3900,6 +3909,15 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_ZERO_PAGE, treg)); } } + else if (shift >= 8) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + proc->mTempOffset[ins->mSTemp[1]] + 1)); + for (int i = 8; i < shift; i++) + mIns.Push(NativeCodeInstruction(ASMIT_LSR, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg)); + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0x00)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 1)); + } else { if (ins->mSTemp[1] != ins->mTTemp) @@ -3918,7 +3936,7 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr for (int i = 1; i < shift; i++) { mIns.Push(NativeCodeInstruction(ASMIT_LSR, ASMIM_ZERO_PAGE, treg + 1)); - mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_IMPLIED)); } mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg )); } @@ -3990,6 +4008,18 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 1)); } } + else if (shift >= 8) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + proc->mTempOffset[ins->mSTemp[1]] + 1)); + for (int i = 8; i < shift; i++) + mIns.Push(NativeCodeInstruction(ASMIT_LSR, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_EOR, ASMIM_IMMEDIATE, 0x80 >> (shift - 8))); + mIns.Push(NativeCodeInstruction(ASMIT_SEC, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_SBC, ASMIM_IMMEDIATE, 0x80 >> (shift - 8))); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg)); + mIns.Push(NativeCodeInstruction(ASMIT_SBC, ASMIM_ZERO_PAGE, treg)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 1)); + } else { if (ins->mSTemp[1] != ins->mTTemp) diff --git a/oscar64/Preprocessor.cpp b/oscar64/Preprocessor.cpp index 917f60b..d1e857f 100644 --- a/oscar64/Preprocessor.cpp +++ b/oscar64/Preprocessor.cpp @@ -207,11 +207,13 @@ bool Preprocessor::CloseSource(void) bool Preprocessor::PushSource(void) { + mSource->mLocation = mLocation; return mSource->PushSource(); } bool Preprocessor::PopSource(void) { + mLocation = mSource->mLocation; return mSource->PopSource(); }