Optimized int shifts ge eight

This commit is contained in:
drmortalwombat 2021-09-29 16:31:17 +02:00
parent f205ba1c49
commit ef5cb81a18
4 changed files with 141 additions and 1 deletions

View File

@ -108,6 +108,12 @@ if %errorlevel% neq 0 goto :error
..\release\oscar64 -e -n asmtest.c ..\release\oscar64 -e -n asmtest.c
if %errorlevel% neq 0 goto :error 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 exit /b 0
:error :error
echo Failed with error #%errorlevel%. echo Failed with error #%errorlevel%.

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h>
unsigned shl1b(int n) unsigned shl1b(int n)
{ {
@ -73,22 +74,123 @@ unsigned shr8n(int n)
#pragma native(shr8n) #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) int main(void)
{ {
for(int i=0; i<32; i++) for(int i=0; i<32; i++)
{ {
printf("1: %.4x : %.4x | %.4x : %.4x\n", shl1b(i), shl1n(i), shr1b(i), shr1n(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++) for(int i=0; i<32; i++)
{ {
printf("4: %.4x : %.4x | %.4x : %.4x\n", shl4b(i), shl4n(i), shr4b(i), shr4n(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++) for(int i=0; i<32; i++)
{ {
printf("8: %.4x : %.4x | %.4x : %.4x\n", shl8b(i), shl8n(i), shr8b(i), shr8n(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; return 0;
} }

View File

@ -3790,6 +3790,15 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_ZERO_PAGE, treg + 1)); 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 else
{ {
if (ins->mSTemp[1] != ins->mTTemp) 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)); 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 else
{ {
if (ins->mSTemp[1] != ins->mTTemp) if (ins->mSTemp[1] != ins->mTTemp)
@ -3918,7 +3936,7 @@ void NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* proc, NativeCodePr
for (int i = 1; i < shift; i++) for (int i = 1; i < shift; i++)
{ {
mIns.Push(NativeCodeInstruction(ASMIT_LSR, ASMIM_ZERO_PAGE, treg + 1)); 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 )); 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)); 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 else
{ {
if (ins->mSTemp[1] != ins->mTTemp) if (ins->mSTemp[1] != ins->mTTemp)

View File

@ -207,11 +207,13 @@ bool Preprocessor::CloseSource(void)
bool Preprocessor::PushSource(void) bool Preprocessor::PushSource(void)
{ {
mSource->mLocation = mLocation;
return mSource->PushSource(); return mSource->PushSource();
} }
bool Preprocessor::PopSource(void) bool Preprocessor::PopSource(void)
{ {
mLocation = mSource->mLocation;
return mSource->PopSource(); return mSource->PopSource();
} }