Reviewers: Kasper Lund,

Description:
Optimize constant divisions by powers of 2.

Please review this at http://codereview.chromium.org/126116

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
   M     src/parser.cc


Index: src/parser.cc
===================================================================
--- src/parser.cc       (revision 2162)
+++ src/parser.cc       (working copy)
@@ -2647,6 +2647,23 @@
          }
        }

+      // Convert constant divisions to multiplications for speed.
+      if (op == Token::DIV &&
+          y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) {
+        double y_val = y->AsLiteral()->handle()->Number();
+        int y_int = static_cast<int>(y_val);
+        // There are rounding issues with this optimization, but they don't
+        // apply if the number to be divided with has a reciprocal that can
+        // be precisely represented as a floating point number.  This is
+        // the case if the number is an integer power of 2.  You can test  
for
+        // an integer power of 2 by subtracting 1 and anding.
+        if (static_cast<double>(y_int) == y_val &&
+            ((y_int - 1) & y_int) == 0) {
+          y = NewNumberLiteral(1 / y_val);
+          op = Token::MUL;
+        }
+      }
+
        // For now we distinguish between comparisons and other binary
        // operations.  (We could combine the two and get rid of this
        // code an AST node eventually.)



--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to