www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5812] New: Power expression optimisation: constant fold (x^^0) = 1

reply d-bugmail puremagic.com writes:
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



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: -------
Apr 03 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5812




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: -------
Apr 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5812




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: -------
Apr 05 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5812


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------

           obsolete|                            |



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: -------
Apr 05 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5812


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



02:21:06 PDT ---
https://github.com/D-Programming-Language/dmd/commit/c64122e84c1ad304136a39acfd3912caec70fc1d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 19 2011