diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index f923ac8c9..d98fc85cb 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -4199,10 +4199,14 @@ void g_gt (unsigned flags, unsigned long val) */ g_ne (flags, val); } else if (val < 0xFFFF) { - /* Use >= instead of > because the former gives better - ** code on the 6502 than the latter. - */ - g_ge (flags, val+1); + if (val == 0xFF) { + AddCodeLine ("cpx #$00"); + } else { + /* Use >= instead of > because the former gives better + ** code on the 6502 than the latter. + */ + g_ge (flags, val+1); + } } else { /* Never true */ Warning ("Condition is never true"); @@ -4229,6 +4233,8 @@ void g_gt (unsigned flags, unsigned long val) ** is easier to optimize. */ g_ne (flags, val); + } else if (val == 0xFF) { + AddCodeLine ("cpx #$00"); } else if (val < 0xFFFFFFFF) { /* Use >= instead of > because the former gives better ** code on the 6502 than the latter. @@ -4241,7 +4247,9 @@ void g_gt (unsigned flags, unsigned long val) } } else { /* Signed compare */ - if ((long) val < 0x7FFFFFFF) { + if (val == 0xFF) { + AddCodeLine ("cpx #$00"); + } else if ((long) val < 0x7FFFFFFF) { g_ge (flags, val+1); } else { /* Never true */ diff --git a/test/val/compare8.c b/test/val/compare8.c index 2621dad1d..3004787ff 100644 --- a/test/val/compare8.c +++ b/test/val/compare8.c @@ -23,6 +23,8 @@ bit bit0 = 0; #endif int int0 = 0; int int1 = 0; +unsigned int uint0 = 0; +unsigned int uint1 = 0; unsigned char uchar0 = 0; unsigned char uchar1 = 0; signed char char0 = 0; @@ -296,12 +298,95 @@ void int_compare2(void) c_int_gt_lit2(0xff); } +void c_uint_gt_lit1(unsigned char expected_result) +{ + result = 0; + + if(uint0 > 0) + result |= 1; + + if(uint0 > 1) + result |= 2; + + if(uint0 > 0xff) + result |= 4; + + if(uint0 > 0x100) + result |= 8; + + if(uint0 > 0x0101) + result |= 0x10; + + if(uint0 > 0x01ff) + result |= 0x20; + + if(uint0 > 0x0200) + result |= 0x40; + + if(uint0 > 0x0201) + result |= 0x80; + + if(result != expected_result) + failures=1; +} + +void uint_compare1(void) +{ + uint0 = 0; + c_uint_gt_lit1(0x00); + + uint0 = 1; + c_uint_gt_lit1(0x01); + + uint0 = 2; + c_uint_gt_lit1(0x03); + + uint0 = 0xfe; + c_uint_gt_lit1(0x03); + + uint0 = 0xff; + c_uint_gt_lit1(0x03); + + uint0 = 0x100; + c_uint_gt_lit1(0x07); + + uint0 = 0x101; + c_uint_gt_lit1(0x0f); + + uint0 = 0x102; + c_uint_gt_lit1(0x1f); + + uint0 = 0x1fe; + c_uint_gt_lit1(0x1f); + + uint0 = 0x1ff; + c_uint_gt_lit1(0x1f); + + uint0 = 0x200; + c_uint_gt_lit1(0x3f); + + uint0 = 0x201; + c_uint_gt_lit1(0x7f); + + uint0 = 0x7f00; + c_uint_gt_lit1(0xff); + + /* now check contiguous ranges */ + + for(uint0 = 2; uint0 != 0xff; uint0++) + c_uint_gt_lit1(0x03); + + for(uint0 = 0x202; uint0 != 0xffff; uint0++) + c_uint_gt_lit1(0xff); +} + int main (void) { char_compare(); int_compare1(); int_compare2(); + uint_compare1(); success = failures; done ();