summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Brazdil <dbrazdil@google.com>2015-03-27 10:31:38 +0000
committerDavid Brazdil <dbrazdil@google.com>2015-03-27 11:02:20 +0000
commitf6468109a49aff6e4a1d28787de625bfb214a761 (patch)
treea662906fca20ace8fb42ebe4872822d00015596c
parent03910065cd025ecb07781b85c2240be69c202d75 (diff)
downloadart-f6468109a49aff6e4a1d28787de625bfb214a761.zip
art-f6468109a49aff6e4a1d28787de625bfb214a761.tar.gz
art-f6468109a49aff6e4a1d28787de625bfb214a761.tar.bz2
ART: Fix IsInt when N==32, add tests
Implicit type conversion caused IsInt to always return true for N==32 on 32-bit platforms. This patch templetizes the function to avoid the conversion and adds tests of this and similar functions. Change-Id: Ie526b68b7c3e7cb7b658253d51840794224785fe
-rw-r--r--runtime/utils.h17
-rw-r--r--runtime/utils_test.cc75
2 files changed, 86 insertions, 6 deletions
diff --git a/runtime/utils.h b/runtime/utils.h
index 1a7fdfb..e6a6b1d 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -109,12 +109,17 @@ static inline bool IsAlignedParam(T x, int n) {
DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
// Check whether an N-bit two's-complement representation can hold value.
-static inline bool IsInt(int N, intptr_t value) {
- if (N == kBitsPerIntPtrT) return true;
- CHECK_LT(0, N);
- CHECK_LT(N, kBitsPerIntPtrT);
- intptr_t limit = static_cast<intptr_t>(1) << (N - 1);
- return (-limit <= value) && (value < limit);
+template <typename T>
+static inline bool IsInt(int N, T value) {
+ int bitsPerT = sizeof(T) * kBitsPerByte;
+ if (N == bitsPerT) {
+ return true;
+ } else {
+ CHECK_LT(0, N);
+ CHECK_LT(N, bitsPerT);
+ T limit = static_cast<T>(1) << (N - 1);
+ return (-limit <= value) && (value < limit);
+ }
}
template <typename T>
diff --git a/runtime/utils_test.cc b/runtime/utils_test.cc
index 6b36c19..833427e 100644
--- a/runtime/utils_test.cc
+++ b/runtime/utils_test.cc
@@ -432,4 +432,79 @@ TEST_F(UtilsTest, MinimumBitsToStore) {
EXPECT_EQ(32u, MinimumBitsToStore(~static_cast<uint32_t>(0)));
}
+static constexpr int64_t INT_MIN_minus1 = static_cast<int64_t>(INT_MIN) - 1;
+static constexpr int64_t INT_MAX_plus1 = static_cast<int64_t>(INT_MAX) + 1;
+static constexpr int64_t UINT_MAX_plus1 = static_cast<int64_t>(UINT_MAX) + 1;
+
+TEST_F(UtilsTest, IsInt) {
+ EXPECT_FALSE(IsInt(1, -2));
+ EXPECT_TRUE(IsInt(1, -1));
+ EXPECT_TRUE(IsInt(1, 0));
+ EXPECT_FALSE(IsInt(1, 1));
+
+ EXPECT_FALSE(IsInt(4, -9));
+ EXPECT_TRUE(IsInt(4, -8));
+ EXPECT_TRUE(IsInt(4, 7));
+ EXPECT_FALSE(IsInt(4, 8));
+
+ EXPECT_FALSE(IsInt(32, INT_MIN_minus1));
+ EXPECT_TRUE(IsInt(32, INT_MIN));
+ EXPECT_TRUE(IsInt(32, INT_MAX));
+ EXPECT_FALSE(IsInt(32, INT_MAX_plus1));
+}
+
+TEST_F(UtilsTest, IsInt_Static) {
+ EXPECT_FALSE(IsInt<1>(-2));
+ EXPECT_TRUE(IsInt<1>(-1));
+ EXPECT_TRUE(IsInt<1>(0));
+ EXPECT_FALSE(IsInt<1>(1));
+
+ EXPECT_FALSE(IsInt<4>(-9));
+ EXPECT_TRUE(IsInt<4>(-8));
+ EXPECT_TRUE(IsInt<4>(7));
+ EXPECT_FALSE(IsInt<4>(8));
+
+ EXPECT_FALSE(IsInt<32>(INT_MIN_minus1));
+ EXPECT_TRUE(IsInt<32>(INT_MIN));
+ EXPECT_TRUE(IsInt<32>(INT_MAX));
+ EXPECT_FALSE(IsInt<32>(INT_MAX_plus1));
+}
+
+TEST_F(UtilsTest, IsUint) {
+ EXPECT_FALSE(IsUint<1>(-1));
+ EXPECT_TRUE(IsUint<1>(0));
+ EXPECT_TRUE(IsUint<1>(1));
+ EXPECT_FALSE(IsUint<1>(2));
+
+ EXPECT_FALSE(IsUint<4>(-1));
+ EXPECT_TRUE(IsUint<4>(0));
+ EXPECT_TRUE(IsUint<4>(15));
+ EXPECT_FALSE(IsUint<4>(16));
+
+ EXPECT_FALSE(IsUint<32>(-1));
+ EXPECT_TRUE(IsUint<32>(0));
+ EXPECT_TRUE(IsUint<32>(UINT_MAX));
+ EXPECT_FALSE(IsUint<32>(UINT_MAX_plus1));
+}
+
+TEST_F(UtilsTest, IsAbsoluteUint) {
+ EXPECT_FALSE(IsAbsoluteUint<1>(-2));
+ EXPECT_TRUE(IsAbsoluteUint<1>(-1));
+ EXPECT_TRUE(IsAbsoluteUint<32>(0));
+ EXPECT_TRUE(IsAbsoluteUint<1>(1));
+ EXPECT_FALSE(IsAbsoluteUint<1>(2));
+
+ EXPECT_FALSE(IsAbsoluteUint<4>(-16));
+ EXPECT_TRUE(IsAbsoluteUint<4>(-15));
+ EXPECT_TRUE(IsAbsoluteUint<32>(0));
+ EXPECT_TRUE(IsAbsoluteUint<4>(15));
+ EXPECT_FALSE(IsAbsoluteUint<4>(16));
+
+ EXPECT_FALSE(IsAbsoluteUint<32>(-UINT_MAX_plus1));
+ EXPECT_TRUE(IsAbsoluteUint<32>(-UINT_MAX));
+ EXPECT_TRUE(IsAbsoluteUint<32>(0));
+ EXPECT_TRUE(IsAbsoluteUint<32>(UINT_MAX));
+ EXPECT_FALSE(IsAbsoluteUint<32>(UINT_MAX_plus1));
+}
+
} // namespace art