Fix d2i, f2i runtime support regression.

(double)(int32_t)(0x80000000ul) is not equal to
static_cast<double>(0x80000000ul).  We should use
static_cast<double>(static_cast<int32_t>(0x80000000))
instead.

Change-Id: Idfc39ec7c990056bd157689fe4cdb899e62c0f10
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 6640430..99aa167 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -363,6 +363,7 @@
 	src/oat_test.cc \
 	src/object_test.cc \
 	src/reference_table_test.cc \
+	src/runtime_support_test.cc \
 	src/runtime_test.cc \
 	src/space_test.cc \
 	src/utils_test.cc \
diff --git a/src/runtime_support.cc b/src/runtime_support.cc
index 37be5f7..c067765 100644
--- a/src/runtime_support.cc
+++ b/src/runtime_support.cc
@@ -35,9 +35,9 @@
   static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
   static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
   if (d >= kMaxLong) {
-    return 0x7fffffffffffffffULL;
+    return static_cast<int64_t>(0x7fffffffffffffffULL);
   } else if (d <= kMinLong) {
-    return 0x8000000000000000ULL;
+    return static_cast<int64_t>(0x8000000000000000ULL);
   } else if (d != d)  { // NaN case
     return 0;
   } else {
@@ -49,9 +49,9 @@
   static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
   static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
   if (f >= kMaxLong) {
-    return 0x7fffffffffffffffULL;
+    return static_cast<int64_t>(0x7fffffffffffffffULL);
   } else if (f <= kMinLong) {
-    return 0x8000000000000000ULL;
+    return static_cast<int64_t>(0x8000000000000000ULL);
   } else if (f != f) { // NaN case
     return 0;
   } else {
@@ -60,12 +60,12 @@
 }
 
 int32_t art_d2i(double d) {
-  static const double kMaxInt = static_cast<double>(0x7fffffffUL);
-  static const double kMinInt = static_cast<double>(0x80000000UL);
+  static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
+  static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
   if (d >= kMaxInt) {
-    return 0x7fffffffUL;
+    return static_cast<int32_t>(0x7fffffffUL);
   } else if (d <= kMinInt) {
-    return 0x80000000UL;
+    return static_cast<int32_t>(0x80000000UL);
   } else if (d != d)  { // NaN case
     return 0;
   } else {
@@ -74,12 +74,12 @@
 }
 
 int32_t art_f2i(float f) {
-  static const float kMaxInt = static_cast<float>(0x7fffffffUL);
-  static const float kMinInt = static_cast<float>(0x80000000UL);
+  static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
+  static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
   if (f >= kMaxInt) {
-    return 0x7fffffffUL;
+    return static_cast<int32_t>(0x7fffffffUL);
   } else if (f <= kMinInt) {
-    return 0x80000000UL;
+    return static_cast<int32_t>(0x80000000UL);
   } else if (f != f) { // NaN case
     return 0;
   } else {
diff --git a/src/runtime_support_test.cc b/src/runtime_support_test.cc
new file mode 100644
index 0000000..b827813
--- /dev/null
+++ b/src/runtime_support_test.cc
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "runtime_support.h"
+
+#include "common_test.h"
+#include <limits>
+
+namespace art {
+
+class RuntimeSupportTest : public CommonTest {};
+
+TEST_F(RuntimeSupportTest, DoubleToLong) {
+  EXPECT_EQ(std::numeric_limits<int64_t>::max(), art_d2l(1.85e19));
+  EXPECT_EQ(std::numeric_limits<int64_t>::min(), art_d2l(-1.85e19));
+  EXPECT_EQ(0LL, art_d2l(0));
+  EXPECT_EQ(1LL, art_d2l(1.0));
+  EXPECT_EQ(10LL, art_d2l(10.0));
+  EXPECT_EQ(100LL, art_d2l(100.0));
+  EXPECT_EQ(-1LL, art_d2l(-1.0));
+  EXPECT_EQ(-10LL, art_d2l(-10.0));
+  EXPECT_EQ(-100LL, art_d2l(-100.0));
+}
+
+TEST_F(RuntimeSupportTest, FloatToLong) {
+  EXPECT_EQ(std::numeric_limits<int64_t>::max(), art_f2l(1.85e19));
+  EXPECT_EQ(std::numeric_limits<int64_t>::min(), art_f2l(-1.85e19));
+  EXPECT_EQ(0LL, art_f2l(0));
+  EXPECT_EQ(1LL, art_f2l(1.0));
+  EXPECT_EQ(10LL, art_f2l(10.0));
+  EXPECT_EQ(100LL, art_f2l(100.0));
+  EXPECT_EQ(-1LL, art_f2l(-1.0));
+  EXPECT_EQ(-10LL, art_f2l(-10.0));
+  EXPECT_EQ(-100LL, art_f2l(-100.0));
+}
+
+TEST_F(RuntimeSupportTest, DoubleToInt) {
+  EXPECT_EQ(std::numeric_limits<int32_t>::max(), art_d2i(4.3e9));
+  EXPECT_EQ(std::numeric_limits<int32_t>::min(), art_d2i(-4.3e9));
+  EXPECT_EQ(0L, art_d2i(0));
+  EXPECT_EQ(1L, art_d2i(1.0));
+  EXPECT_EQ(10L, art_d2i(10.0));
+  EXPECT_EQ(100L, art_d2i(100.0));
+  EXPECT_EQ(-1L, art_d2i(-1.0));
+  EXPECT_EQ(-10L, art_d2i(-10.0));
+  EXPECT_EQ(-100L, art_d2i(-100.0));
+}
+
+TEST_F(RuntimeSupportTest, FloatToInt) {
+  EXPECT_EQ(std::numeric_limits<int32_t>::max(), art_f2i(4.3e9));
+  EXPECT_EQ(std::numeric_limits<int32_t>::min(), art_f2i(-4.3e9));
+  EXPECT_EQ(0L, art_f2i(0));
+  EXPECT_EQ(1L, art_f2i(1.0));
+  EXPECT_EQ(10L, art_f2i(10.0));
+  EXPECT_EQ(100L, art_f2i(100.0));
+  EXPECT_EQ(-1L, art_f2i(-1.0));
+  EXPECT_EQ(-10L, art_f2i(-10.0));
+  EXPECT_EQ(-100L, art_f2i(-100.0));
+}
+
+}  // namespace art