From 45500004dd165faec5ffa13f2e37b9fadde0eaad Mon Sep 17 00:00:00 2001 From: dschmenk Date: Thu, 1 Jun 2017 12:23:29 -0700 Subject: [PATCH] On the road to 1.0: optimize MUL/DIV by POW2 --- src/toolsrc/codegen.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/toolsrc/codegen.c b/src/toolsrc/codegen.c index 9d0cfb8..a6bb4d4 100755 --- a/src/toolsrc/codegen.c +++ b/src/toolsrc/codegen.c @@ -914,12 +914,13 @@ int crunch_seq(t_opseq *seq) t_opseq *op = seq; int crunched = 0; int freeops = 0; + int shiftcnt; + while (op && (opnext = op->nextop)) { switch (op->code) { case CONST_CODE: - //fprintf(stderr, "CONST -> $%04X", opnext->code); if (op->val == 1) { if (opnext->code == BINARY_CODE(ADD_TOKEN)) @@ -971,6 +972,28 @@ int crunch_seq(t_opseq *seq) op->code = SAW_CODE; freeops = 1; break; + case BINARY_CODE(MUL_TOKEN): + for (shiftcnt = 0; shiftcnt < 16; shiftcnt++) + { + if (op->val == (1 << shiftcnt)) + { + op->val = shiftcnt; + opnext->code = BINARY_CODE(SHL_TOKEN); + break; + } + } + break; + case BINARY_CODE(DIV_TOKEN): + for (shiftcnt = 0; shiftcnt < 16; shiftcnt++) + { + if (op->val == (1 << shiftcnt)) + { + op->val = shiftcnt; + opnext->code = BINARY_CODE(SHR_TOKEN); + break; + } + } + break; case CONST_CODE: // Collapse constant operation if ((opnextnext = opnext->nextop)) switch (opnextnext->code)