Fix some QJSValue test failures.
authorGlenn Watson <glenn.watson@nokia.com>
Fri, 3 Feb 2012 01:21:24 +0000 (11:21 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 6 Feb 2012 03:10:41 +0000 (04:10 +0100)
Fix precision issues causing test failures on platforms where
qreal is defined as single precision. Also use explicit casts
to ensure well defined behaviour when converting a negative
double value to an unsigned integer.

Change-Id: Ia0048bf83169d3b617f70828f86368c23f4f3786
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/declarative/qml/v8/qjsvalue_impl_p.h

index 69ec70b..cd33859 100644 (file)
@@ -304,7 +304,11 @@ double QJSValuePrivate::toInteger() const
         return 0;
     if (qIsInf(result))
         return result;
-    return (result > 0) ? qFloor(result) : -1 * qFloor(-result);
+
+    // Must use floor explicitly rather than qFloor here. On some
+    // platforms qFloor will cast the value to a single precision float and use
+    // floorf() which results in test failures.
+    return (result > 0) ? floor(result) : -1 * floor(-result);
 }
 
 qint32 QJSValuePrivate::toInt32() const
@@ -324,7 +328,11 @@ quint32 QJSValuePrivate::toUInt32() const
     // some of these operation are invoked in toInteger subcall.
     if (qIsInf(result))
         return 0;
-    return result;
+
+    // The explicit casts are required to avoid undefined behaviour. For example, casting
+    // a negative double directly to an unsigned int on ARM NEON FPU results in the value
+    // being set to zero. Casting to a signed int first ensures well defined behaviour.
+    return (quint32) (qint32) result;
 }
 
 quint16 QJSValuePrivate::toUInt16() const