scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 0935ae49a658bedc97d1bf9082049498e148f6df
parent a8dae969ea1800fd5f3f247cc3cc7c91f10be260
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Mon, 23 Mar 2026 11:54:51 +0100

cc1: Reduce unsigned constant division to shift

Diffstat:
Msrc/cmd/scc-cc/cc1/fold.c | 5+++++
Atests/cc/execute/0250-div.c | 14++++++++++++++
Mtests/cc/execute/scc-tests.lst | 1+
3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/fold.c b/src/cmd/scc-cc/cc1/fold.c @@ -803,6 +803,11 @@ reduce(Node *np) return; switch (op) { + case ODIV: + /* i / 2^n => i >> n */ + np->op = OSHR; + rp->sym->u.u = log; + break; case OMOD: /* i % 2^n => i & n-1 */ np->op = OBAND; diff --git a/tests/cc/execute/0250-div.c b/tests/cc/execute/0250-div.c @@ -0,0 +1,14 @@ +int +main(void) +{ + int a = -2; + unsigned b = 2, c = 16; + + if (!(a / 2 == -1)) + return 1; + if (!(b / 2 == 1)) + return 2; + if (!(c / 8 == 2)) + return 3; + return 0; +} diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst @@ -240,3 +240,4 @@ 0247-float.c 0248-enum.c 0249-mod.c +0250-div.c