Bug 5812 – Power expression optimisation: constant fold (x^^0) = 1

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-03T18:06:00Z
Last change time
2011-04-19T02:21:06Z
Keywords
patch, performance, wrong-code
Assigned to
nobody
Creator
ibuclaw

Attachments

IDFilenameSummaryContent-TypeSize
937foldpow.patchimplement noted foldable code in this reporttext/plain1346
938foldpow2.patchjust noticed previous patch allows to bypass an error - fixedtext/plain1256

Comments

Comment #0 by ibuclaw — 2011-04-03T18:06:47Z
CTFE testcases (that currently fail) const x = 3; static assert((7 ^^ 0) == 1); static assert((7 ^^ 0.0) == 1); static assert((x ^^ 0) == 1); static assert((x ^^ 0.0) == 1); Patch: --- dmd.orig/expression.c 2011-02-18 01:15:38.000000000 +0000 +++ dmd/expression.c 2011-04-04 02:05:38.631620650 +0100 @@ -10302,6 +10302,15 @@ Expression *PowExp::semantic(Scope *sc) e = e->semantic(sc); return e; } + // Replace x ^^ 0 or x^^0.0 by (x, 1) + if ((e2->op == TOKint64 && e2->toInteger() == 0) || + (e2->op == TOKfloat64 && e2->toReal() == 0.0)) + { + typeCombine(sc); + e = new CommaExp(loc, e1, new IntegerExp(loc, 1, Type::tint32)); + e = e->semantic(sc); + return e; + } // Replace -1 ^^ x by (x&1) ? -1 : 1, where x is integral if (e2->type->isintegral() && e1->op == TOKint64 && (sinteger_t)e1->toInteger() == -1L) { Feel free to improve it. Regards
Comment #1 by ibuclaw — 2011-04-04T11:32:56Z
You can also optimize: x^^1 => (x) x^^-1 => (1/x) And that will remove two other scenarios from requiring std.math to compute the result. Regards
Comment #2 by ibuclaw — 2011-04-05T16:00:43Z
Created attachment 937 implement noted foldable code in this report
Comment #3 by ibuclaw — 2011-04-05T16:08:49Z
Created attachment 938 just noticed previous patch allows to bypass an error - fixed
Comment #4 by bugzilla — 2011-04-19T02:21:06Z