From 48b825007fe45d84cea1ae6f526103ccedef87c3 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 13 Dec 2012 14:48:20 +0100 Subject: [PATCH] inline some code in toInt32 and toUInt32 Speeds up crypto.js by ~5% Change-Id: I707bd6e7dc0f13b70889955e1b90f0c436db1848 Reviewed-by: Simon Hausmann --- qmljs_value.cpp | 16 ---------------- qmljs_value.h | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/qmljs_value.cpp b/qmljs_value.cpp index 38e1f5d..32c0ca6 100644 --- a/qmljs_value.cpp +++ b/qmljs_value.cpp @@ -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); diff --git a/qmljs_value.h b/qmljs_value.h index 7f095ca..9446c79 100644 --- a/qmljs_value.h +++ b/qmljs_value.h @@ -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(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(dbl); + return toUInt32(d); +} + } // namespace VM } // namespace QQmlJS -- 2.7.4