Search
[Issue 5812] New: Power expression optimisation: constant fold (x^^0) = 1
Apr 04, 2011
Iain Buclaw
Apr 04, 2011
Iain Buclaw
Apr 05, 2011
Iain Buclaw
Apr 05, 2011
Iain Buclaw
Apr 19, 2011
Walter Bright
```http://d.puremagic.com/issues/show_bug.cgi?id=5812

Summary: Power expression optimisation: constant fold (x^^0) =
1
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody@puremagic.com
ReportedBy: ibuclaw@ubuntu.com

--- Comment #0 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-03 18:06:47 PDT ---
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

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
```http://d.puremagic.com/issues/show_bug.cgi?id=5812

--- Comment #1 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-04 11:32:56 PDT ---
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

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
```http://d.puremagic.com/issues/show_bug.cgi?id=5812

--- Comment #2 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-05 16:00:43 PDT ---
Created an attachment (id=937)
implement noted foldable code in this report

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
```http://d.puremagic.com/issues/show_bug.cgi?id=5812

Iain Buclaw <ibuclaw@ubuntu.com> changed:

----------------------------------------------------------------------------
Attachment #937 is|0                           |1
obsolete|                            |

--- Comment #3 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-05 16:08:49 PDT ---
Created an attachment (id=938)
just noticed previous patch allows to bypass an error - fixed

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
```http://d.puremagic.com/issues/show_bug.cgi?id=5812

Walter Bright <bugzilla@digitalmars.com> changed: