Reviewers: Sven Panne,

Description:
Make sure double to int conversion is correct.


[email protected]
BUG=v8:2260
TEST=test-utils/Utils1


Please review this at https://chromiumcodereview.appspot.com/10820047/

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

Affected files:
  M src/conversions.h
  M test/cctest/test-utils.cc


Index: src/conversions.h
diff --git a/src/conversions.h b/src/conversions.h
index 70559c9e9d7979f449110793bbdde122cd1d99a5..10c074d4273fbcd792ddb365cb9a61edd67c0a55 100644
--- a/src/conversions.h
+++ b/src/conversions.h
@@ -64,6 +64,8 @@ inline double SignedZero(bool negative) {
 inline int FastD2I(double x) {
   // The static_cast convertion from double to int used to be slow, but
   // as new benchmarks show, now it is much faster than lrint().
+  if (!(x >= INT_MIN)) return INT_MIN;  // Negation to catch NaNs.
+  if (x > INT_MAX) return INT_MAX;
   return static_cast<int>(x);
 }

Index: test/cctest/test-utils.cc
diff --git a/test/cctest/test-utils.cc b/test/cctest/test-utils.cc
index df8ff72e4f3eafed1e85aad150b2ce0d0a93f409..2ff4e6f440117aec2400aec6fb4ce52c80fc5af5 100644
--- a/test/cctest/test-utils.cc
+++ b/test/cctest/test-utils.cc
@@ -55,6 +55,9 @@ TEST(Utils1) {
   CHECK_EQ(-2, -8 >> 2);
   CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
   CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
+  CHECK_EQ(INT_MAX, FastD2I(1.0e100));
+  CHECK_EQ(INT_MIN, FastD2I(-1.0e100));
+  CHECK_EQ(INT_MIN, FastD2I(NAN));
 }




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

Reply via email to