More optimized codegen for the correct cases of the Issue #169 fix.

This commit is contained in:
acqn
2020-07-02 10:30:02 +08:00
committed by Oliver Schmidt
parent e98fe04cc2
commit 30835e3d9d

View File

@@ -1480,12 +1480,9 @@ void g_scale (unsigned flags, long val)
/* Scale down */ /* Scale down */
val = -val; val = -val;
/* g_div will use asr if feasible */ /* Use a division instead */
if (val != 1) { if (val != 1) {
/* Use a division instead */
g_div (flags | CF_CONST, val); g_div (flags | CF_CONST, val);
} }
} }
} }
@@ -2631,6 +2628,9 @@ void g_div (unsigned flags, unsigned long val)
unsigned DoShiftLabel, EndLabel; unsigned DoShiftLabel, EndLabel;
/* -Val truncated to the correct size */
unsigned long NegatedVal;
/* Do strength reduction if the value is constant and a power of two */ /* Do strength reduction if the value is constant and a power of two */
int p2; int p2;
if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) { if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
@@ -2641,9 +2641,11 @@ void g_div (unsigned flags, unsigned long val)
/* GitHub #169 - if abs(expr) < abs(val), the result is always 0 */ /* GitHub #169 - if abs(expr) < abs(val), the result is always 0 */
DoShiftLabel = GetLocalLabel (); DoShiftLabel = GetLocalLabel ();
EndLabel = GetLocalLabel (); EndLabel = GetLocalLabel ();
NegatedVal = (unsigned long)-(signed long)val;
switch (flags & CF_TYPEMASK) { switch (flags & CF_TYPEMASK) {
case CF_CHAR: case CF_CHAR:
if (flags & CF_FORCECHAR) { if (flags & CF_FORCECHAR) {
NegatedVal &= 0xFF;
AddCodeLine ("cmp #$00"); AddCodeLine ("cmp #$00");
AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel)); AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel));
break; break;
@@ -2651,11 +2653,13 @@ void g_div (unsigned flags, unsigned long val)
/* FALLTHROUGH */ /* FALLTHROUGH */
case CF_INT: case CF_INT:
NegatedVal &= 0xFFFF;
AddCodeLine ("cpx #$00"); AddCodeLine ("cpx #$00");
AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel)); AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel));
break; break;
case CF_LONG: case CF_LONG:
NegatedVal &= 0xFFFFFFFF;
AddCodeLine ("ldy sreg+1"); AddCodeLine ("ldy sreg+1");
AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel)); AddCodeLine ("bpl %s", LocalLabelName (DoShiftLabel));
break; break;
@@ -2665,7 +2669,7 @@ void g_div (unsigned flags, unsigned long val)
break; break;
} }
g_save (flags); g_save (flags);
g_le (flags, (unsigned long)-(signed long)val); g_le (flags | CF_UNSIGNED, NegatedVal);
AddCodeLine ("lsr a"); AddCodeLine ("lsr a");
g_restore (flags); g_restore (flags);
AddCodeLine ("bcs %s", LocalLabelName (DoShiftLabel)); AddCodeLine ("bcs %s", LocalLabelName (DoShiftLabel));