1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-28 22:30:12 +00:00

Better code for > compares

git-svn-id: svn://svn.cc65.org/cc65/trunk@823 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2001-07-26 06:10:10 +00:00
parent d604ed5e3f
commit 58592116ac

View File

@ -3559,31 +3559,31 @@ void g_le (unsigned flags, unsigned long val)
*/ */
if (flags & CF_CONST) { if (flags & CF_CONST) {
/* <= is not very effective on the 6502, so try to convert /* <= is not very effective on the 6502, so try to convert
* it into < if the value is in a valid range. * it into < if the value is in a valid range.
*/ */
/* Look at the type */ /* Look at the type */
switch (flags & CF_TYPE) { switch (flags & CF_TYPE) {
case CF_CHAR: case CF_CHAR:
if (flags & CF_FORCECHAR) { if (flags & CF_FORCECHAR) {
if (flags & CF_UNSIGNED) { if (flags & CF_UNSIGNED) {
if (val < 255) { if (val < 255) {
AddCodeLine ("cmp #$%02X", (unsigned char)val+1); AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
AddCodeLine ("jsr boolult"); AddCodeLine ("jsr boolult");
} else { } else {
AddCodeLine ("cmp #$%02X", (unsigned char)val); AddCodeLine ("cmp #$%02X", (unsigned char)val);
AddCodeLine ("jsr boolule"); AddCodeLine ("jsr boolule");
} }
} else { } else {
if (val < 127) { if (val < 127) {
AddCodeLine ("cmp #$%02X", (unsigned char)val+1); AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
AddCodeLine ("jsr boollt"); AddCodeLine ("jsr boollt");
} else { } else {
AddCodeLine ("cmp #$%02X", (unsigned char)val); AddCodeLine ("cmp #$%02X", (unsigned char)val);
AddCodeLine ("jsr boolle"); AddCodeLine ("jsr boolle");
} }
} }
return; return;
} }
@ -3595,7 +3595,7 @@ void g_le (unsigned flags, unsigned long val)
const char* Name = "boolule"; const char* Name = "boolule";
if (val < 65535) { if (val < 65535) {
++val; ++val;
Name = "boolult"; Name = "boolult";
} }
AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("bne %s", LocalLabelName (L)); AddCodeLine ("bne %s", LocalLabelName (L));
@ -3630,10 +3630,10 @@ void g_gt (unsigned flags, unsigned long val)
/* Test for greater than */ /* Test for greater than */
{ {
static char* ops [12] = { static char* ops [12] = {
"tosgt00", "tosgta0", "tosgtax", "tosgt00", "tosgta0", "tosgtax",
"tosugt00", "tosugta0", "tosugtax", "tosugt00", "tosugta0", "tosugtax",
0, 0, "tosgteax", 0, 0, "tosgteax",
0, 0, "tosugteax", 0, 0, "tosugteax",
}; };
@ -3642,46 +3642,65 @@ void g_gt (unsigned flags, unsigned long val)
*/ */
if (flags & CF_CONST) { if (flags & CF_CONST) {
/* > is not very effective on the 6502, so try to convert
* it into >= if the value is in a valid range.
*/
/* Look at the type */ /* Look at the type */
switch (flags & CF_TYPE) { switch (flags & CF_TYPE) {
case CF_CHAR: case CF_CHAR:
if (flags & CF_FORCECHAR) { if (flags & CF_FORCECHAR) {
AddCodeLine ("cmp #$%02X", (unsigned char)val); if (flags & CF_UNSIGNED) {
if (flags & CF_UNSIGNED) { /* If we have a compare > 0, we will replace it by
/* If we have a compare > 0, we will replace it by * != 0 here, since both are identical but the latter
* != 0 here, since both are identical but the latter * is easier to optimize.
* is easier to optimize. */
*/ if (val == 0) {
if (val & 0xFF) { AddCodeLine ("cmp #$%02X", (unsigned char)val);
AddCodeLine ("jsr boolugt"); AddCodeLine ("jsr boolne");
} else { } else if (val < 255) {
AddCodeLine ("jsr boolne"); AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
} AddCodeLine ("jsr booluge");
} else { } else {
AddCodeLine ("jsr boolgt"); AddCodeLine ("cmp #$%02X", (unsigned char)val);
} AddCodeLine ("jsr boolugt");
return; }
} } else {
/* FALLTHROUGH */ if (val < 127) {
AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
AddCodeLine ("jsr boolge");
} else {
AddCodeLine ("cmp #$%02X", (unsigned char)val);
AddCodeLine ("jsr boolgt");
}
}
return;
}
/* FALLTHROUGH */
case CF_INT: case CF_INT:
if (flags & CF_UNSIGNED) { if (flags & CF_UNSIGNED) {
/* If we have a compare > 0, we will replace it by /* If we have a compare > 0, we will replace it by
* != 0 here, since both are identical but the latter * != 0 here, since both are identical but the latter
* is easier to optimize. * is easier to optimize.
*/ */
if ((val & 0xFFFF) == 0) { if (val == 0) {
AddCodeLine ("stx tmp1"); AddCodeLine ("stx tmp1");
AddCodeLine ("ora tmp1"); AddCodeLine ("ora tmp1");
AddCodeLine ("jsr boolne"); AddCodeLine ("jsr boolne");
} else { } else {
unsigned L = GetLocalLabel(); unsigned L = GetLocalLabel();
const char* Name = "boolugt";
if (val < 65535) {
++val;
Name = "booluge";
}
AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("bne %s", LocalLabelName (L)); AddCodeLine ("bne %s", LocalLabelName (L));
AddCodeLine ("cmp #$%02X", (unsigned char)val); AddCodeLine ("cmp #$%02X", (unsigned char)val);
g_defcodelabel (L); g_defcodelabel (L);
AddCodeLine ("jsr boolugt"); AddCodeLine ("jsr %s", Name);
} }
return; return;
} }