inline some code in toInt32 and toUInt32
authorLars Knoll <lars.knoll@digia.com>
Thu, 13 Dec 2012 13:48:20 +0000 (14:48 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Thu, 13 Dec 2012 15:16:50 +0000 (16:16 +0100)
Speeds up crypto.js by ~5%

Change-Id: I707bd6e7dc0f13b70889955e1b90f0c436db1848
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
qmljs_value.cpp
qmljs_value.h

index 38e1f5d..32c0ca6 100644 (file)
@@ -51,22 +51,6 @@ int Value::toUInt16(ExecutionContext *ctx)
     return __qmljs_to_uint16(*this, ctx);
 }
 
-int Value::toInt32(ExecutionContext *ctx)
-{
-    if (isConvertibleToInt())
-        return int_32;
-
-    return Value::toInt32(__qmljs_to_number(*this, ctx));
-}
-
-unsigned int Value::toUInt32(ExecutionContext *ctx)
-{
-    if (isConvertibleToInt())
-        return (unsigned) int_32;
-
-    return toUInt32(__qmljs_to_number(*this, ctx));
-}
-
 Bool Value::toBoolean(ExecutionContext *ctx) const
 {
     return __qmljs_to_boolean(*this, ctx);
index 7f095ca..9446c79 100644 (file)
@@ -64,6 +64,11 @@ struct ErrorObject;
 struct ArgumentsObject;
 struct ExecutionContext;
 struct ExecutionEngine;
+struct Value;
+
+extern "C" {
+double __qmljs_to_number(Value value, ExecutionContext *ctx);
+}
 
 typedef uint Bool;
 
@@ -197,6 +202,7 @@ struct Value
     int toUInt16(ExecutionContext *ctx);
     int toInt32(ExecutionContext *ctx);
     unsigned int toUInt32(ExecutionContext *ctx);
+
     Bool toBoolean(ExecutionContext *ctx) const;
     double toInteger(ExecutionContext *ctx) const;
     double toNumber(ExecutionContext *ctx) const;
@@ -324,6 +330,40 @@ inline Value Value::fromObject(Object *o)
     return v;
 }
 
+inline int Value::toInt32(ExecutionContext *ctx)
+{
+    if (isConvertibleToInt())
+        return int_32;
+    double d;
+    if (isDouble())
+        d = dbl;
+    else
+        d = __qmljs_to_number(*this, ctx);
+
+    const double D32 = 4294967296.0;
+    const double D31 = D32 / 2.0;
+
+    if ((d >= -D31 && d < D31))
+        return static_cast<int>(d);
+
+    return Value::toInt32(__qmljs_to_number(*this, ctx));
+}
+
+inline unsigned int Value::toUInt32(ExecutionContext *ctx) {
+    if (isConvertibleToInt())
+        return (unsigned) int_32;
+    double d;
+    if (isDouble())
+        d = dbl;
+    else
+        d = __qmljs_to_number(*this, ctx);
+
+    const double D32 = 4294967296.0;
+    if (dbl >= 0 && dbl < D32)
+        return static_cast<uint>(dbl);
+    return toUInt32(d);
+}
+
 } // namespace VM
 } // namespace QQmlJS