Skip to content

Commit

Permalink
Merge pull request cc65#2252 from colinleroy/optimize-multiply-by-zero
Browse files Browse the repository at this point in the history
Optimize multiplication by zero
  • Loading branch information
mrdudz authored Nov 12, 2023
2 parents be49751 + cff6117 commit d7d1d89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cc65/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2726,7 +2726,12 @@ void g_mul (unsigned flags, unsigned long val)
if (flags & CF_FORCECHAR) {
/* Handle some special cases */
switch (val) {

case 0:
AddCodeLine ("lda #$00");
return;
case 1:
/* Nothing to do */
return;
case 3:
AddCodeLine ("sta tmp1");
AddCodeLine ("asl a");
Expand Down Expand Up @@ -2764,6 +2769,13 @@ void g_mul (unsigned flags, unsigned long val)

case CF_INT:
switch (val) {
case 0:
AddCodeLine ("lda #$00");
AddCodeLine ("tax");
return;
case 1:
/* Nothing to do */
return;
case 3:
AddCodeLine ("jsr mulax3");
return;
Expand Down
1 change: 1 addition & 0 deletions test/val/mult1.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void m3(unsigned char uc)
/* testing literal multiply with same source and destination */
vuc = uc;
uc2 = 0;
uc1 = vuc; uc1 = uc1*0; if( uc1 != 0 ) failures++;
uc1 = vuc; uc1 = uc1*1; if( uc1 != (uc2+=TESTLIT) ) failures++;
uc1 = vuc; uc1 = uc1*2; if( uc1 != (uc2+=TESTLIT) ) failures++;
uc1 = vuc; uc1 = uc1*3; if( uc1 != (uc2+=TESTLIT) ) failures++;
Expand Down

0 comments on commit d7d1d89

Please sign in to comment.