[turbofan] Fix HashCode/Equals for floating point operators.
Those floating point constant operators require bitwise handling of their parameters, otherwise 0.0 equals -0.0. This is solved in a general way by adding new base::bit_equal_to and base::bit_hash function objects. TEST=unittests R=svenpanne@chromium.org Review URL: https://codereview.chromium.org/636953002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24450 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
956591293c
commit
8617b3acf8
@ -6,6 +6,7 @@
|
||||
#define V8_BASE_FUNCTIONAL_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
@ -67,7 +68,17 @@ V8_INLINE size_t hash_combine(size_t seed) { return seed; }
|
||||
size_t hash_combine(size_t seed, size_t value);
|
||||
template <typename T, typename... Ts>
|
||||
V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) {
|
||||
return hash_combine(hash<T>()(v), hash_combine(vs...));
|
||||
return hash_combine(hash_combine(vs...), hash<T>()(v));
|
||||
}
|
||||
|
||||
|
||||
template <typename Iterator>
|
||||
V8_INLINE size_t hash_range(Iterator first, Iterator last) {
|
||||
size_t seed = 0;
|
||||
for (; first != last; ++first) {
|
||||
seed = hash_combine(seed, *first);
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
|
||||
@ -103,6 +114,16 @@ V8_INLINE size_t hash_value(double v) {
|
||||
return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0;
|
||||
}
|
||||
|
||||
template <typename T, size_t N>
|
||||
V8_INLINE size_t hash_value(const T (&v)[N]) {
|
||||
return hash_range(v, v + N);
|
||||
}
|
||||
|
||||
template <typename T, size_t N>
|
||||
V8_INLINE size_t hash_value(T (&v)[N]) {
|
||||
return hash_range(v, v + N);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
V8_INLINE size_t hash_value(T* const& v) {
|
||||
return hash_value(bit_cast<uintptr_t>(v));
|
||||
@ -148,13 +169,55 @@ struct hash<T*> : public std::unary_function<T*, size_t> {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T1, typename T2>
|
||||
struct hash<std::pair<T1, T2> >
|
||||
: public std::unary_function<std::pair<T1, T2>, size_t> {
|
||||
V8_INLINE size_t operator()(std::pair<T1, T2> const& v) const {
|
||||
return ::v8::base::hash_value(v);
|
||||
}
|
||||
};
|
||||
|
||||
// base::bit_equal_to is a function object class for bitwise equality
|
||||
// comparison, similar to std::equal_to, except that the comparison is performed
|
||||
// on the bit representation of the operands.
|
||||
//
|
||||
// base::bit_hash is a function object class for bitwise hashing, similar to
|
||||
// base::hash. It can be used together with base::bit_equal_to to implement a
|
||||
// hash data structure based on the bitwise representation of types.
|
||||
|
||||
template <typename T>
|
||||
struct bit_equal_to : public std::binary_function<T, T, bool> {};
|
||||
|
||||
template <typename T>
|
||||
struct bit_hash : public std::unary_function<T, size_t> {};
|
||||
|
||||
#define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type) \
|
||||
template <> \
|
||||
struct bit_equal_to<type> : public std::equal_to<type> {}; \
|
||||
template <> \
|
||||
struct bit_hash<type> : public hash<type> {};
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(signed char)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned char)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(short) // NOLINT(runtime/int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned short) // NOLINT(runtime/int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(long) // NOLINT(runtime/int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long) // NOLINT(runtime/int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(long long) // NOLINT(runtime/int)
|
||||
V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long long) // NOLINT(runtime/int)
|
||||
#undef V8_BASE_BIT_SPECIALIZE_TRIVIAL
|
||||
|
||||
#define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype) \
|
||||
template <> \
|
||||
struct bit_equal_to<type> : public std::binary_function<type, type, bool> { \
|
||||
V8_INLINE bool operator()(type lhs, type rhs) const { \
|
||||
return bit_cast<btype>(lhs) == bit_cast<btype>(rhs); \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct bit_hash<type> : public std::unary_function<type, size_t> { \
|
||||
V8_INLINE size_t operator()(type v) const { \
|
||||
hash<btype> h; \
|
||||
return h(bit_cast<btype>(v)); \
|
||||
} \
|
||||
};
|
||||
V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t)
|
||||
V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t)
|
||||
#undef V8_BASE_BIT_SPECIALIZE_BIT_CAST
|
||||
|
||||
} // namespace base
|
||||
} // namespace v8
|
||||
|
@ -150,15 +150,17 @@ const Operator* CommonOperatorBuilder::Int64Constant(int64_t value) {
|
||||
|
||||
const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
|
||||
return new (zone())
|
||||
Operator1<float>(IrOpcode::kFloat32Constant, Operator::kPure, 0, 1,
|
||||
"Float32Constant", value);
|
||||
Operator1<float, base::bit_equal_to<float>, base::bit_hash<float>>(
|
||||
IrOpcode::kFloat32Constant, Operator::kPure, 0, 1, "Float32Constant",
|
||||
value);
|
||||
}
|
||||
|
||||
|
||||
const Operator* CommonOperatorBuilder::Float64Constant(volatile double value) {
|
||||
return new (zone())
|
||||
Operator1<double>(IrOpcode::kFloat64Constant, Operator::kPure, 0, 1,
|
||||
"Float64Constant", value);
|
||||
Operator1<double, base::bit_equal_to<double>, base::bit_hash<double>>(
|
||||
IrOpcode::kFloat64Constant, Operator::kPure, 0, 1, "Float64Constant",
|
||||
value);
|
||||
}
|
||||
|
||||
|
||||
@ -172,8 +174,9 @@ const Operator* CommonOperatorBuilder::ExternalConstant(
|
||||
|
||||
const Operator* CommonOperatorBuilder::NumberConstant(volatile double value) {
|
||||
return new (zone())
|
||||
Operator1<double>(IrOpcode::kNumberConstant, Operator::kPure, 0, 1,
|
||||
"NumberConstant", value);
|
||||
Operator1<double, base::bit_equal_to<double>, base::bit_hash<double>>(
|
||||
IrOpcode::kNumberConstant, Operator::kPure, 0, 1, "NumberConstant",
|
||||
value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -97,6 +97,39 @@ TYPED_TEST(FunctionalTest, HashIsOkish) {
|
||||
}
|
||||
|
||||
|
||||
TYPED_TEST(FunctionalTest, HashValueArrayUsesHashRange) {
|
||||
TypeParam values[128];
|
||||
this->rng()->NextBytes(&values, sizeof(values));
|
||||
EXPECT_EQ(hash_range(values, values + arraysize(values)), hash_value(values));
|
||||
}
|
||||
|
||||
|
||||
TYPED_TEST(FunctionalTest, BitEqualTo) {
|
||||
bit_equal_to<TypeParam> pred;
|
||||
for (size_t i = 0; i < 128; ++i) {
|
||||
TypeParam v1, v2;
|
||||
this->rng()->NextBytes(&v1, sizeof(v1));
|
||||
this->rng()->NextBytes(&v2, sizeof(v2));
|
||||
EXPECT_PRED2(pred, v1, v1);
|
||||
EXPECT_PRED2(pred, v2, v2);
|
||||
EXPECT_EQ(memcmp(&v1, &v2, sizeof(TypeParam)) == 0, pred(v1, v2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TYPED_TEST(FunctionalTest, BitEqualToImpliesSameBitHash) {
|
||||
bit_hash<TypeParam> h;
|
||||
bit_equal_to<TypeParam> e;
|
||||
TypeParam values[32];
|
||||
this->rng()->NextBytes(&values, sizeof(values));
|
||||
TRACED_FOREACH(TypeParam, v1, values) {
|
||||
TRACED_FOREACH(TypeParam, v2, values) {
|
||||
if (e(v1, v2)) EXPECT_EQ(h(v1), h(v2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
struct Foo {
|
||||
@ -125,5 +158,39 @@ TEST(FunctionalTest, HashUsesArgumentDependentLookup) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(FunctionalTest, BitEqualToFloat) {
|
||||
bit_equal_to<float> pred;
|
||||
EXPECT_FALSE(pred(0.0f, -0.0f));
|
||||
EXPECT_FALSE(pred(-0.0f, 0.0f));
|
||||
float const qNaN = std::numeric_limits<float>::quiet_NaN();
|
||||
float const sNaN = std::numeric_limits<float>::signaling_NaN();
|
||||
EXPECT_PRED2(pred, qNaN, qNaN);
|
||||
EXPECT_PRED2(pred, sNaN, sNaN);
|
||||
}
|
||||
|
||||
|
||||
TEST(FunctionalTest, BitHashFloatDifferentForZeroAndMinusZero) {
|
||||
bit_hash<float> h;
|
||||
EXPECT_NE(h(0.0f), h(-0.0f));
|
||||
}
|
||||
|
||||
|
||||
TEST(FunctionalTest, BitEqualToDouble) {
|
||||
bit_equal_to<double> pred;
|
||||
EXPECT_FALSE(pred(0.0, -0.0));
|
||||
EXPECT_FALSE(pred(-0.0, 0.0));
|
||||
double const qNaN = std::numeric_limits<double>::quiet_NaN();
|
||||
double const sNaN = std::numeric_limits<double>::signaling_NaN();
|
||||
EXPECT_PRED2(pred, qNaN, qNaN);
|
||||
EXPECT_PRED2(pred, sNaN, sNaN);
|
||||
}
|
||||
|
||||
|
||||
TEST(FunctionalTest, BitHashDoubleDifferentForZeroAndMinusZero) {
|
||||
bit_hash<double> h;
|
||||
EXPECT_NE(h(0.0), h(-0.0));
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
} // namespace v8
|
||||
|
@ -133,23 +133,93 @@ class CommonOperatorTest : public TestWithZone {
|
||||
|
||||
const int kArguments[] = {1, 5, 6, 42, 100, 10000, kMaxInt};
|
||||
|
||||
const float kFloat32Values[] = {
|
||||
std::numeric_limits<float>::min(), -1.0f, -0.0f, 0.0f, 1.0f,
|
||||
std::numeric_limits<float>::max()};
|
||||
|
||||
const float kFloatValues[] = {-std::numeric_limits<float>::infinity(),
|
||||
std::numeric_limits<float>::min(),
|
||||
-1.0f,
|
||||
-0.0f,
|
||||
0.0f,
|
||||
1.0f,
|
||||
std::numeric_limits<float>::max(),
|
||||
std::numeric_limits<float>::infinity(),
|
||||
std::numeric_limits<float>::quiet_NaN(),
|
||||
std::numeric_limits<float>::signaling_NaN()};
|
||||
|
||||
|
||||
const double kDoubleValues[] = {-std::numeric_limits<double>::infinity(),
|
||||
std::numeric_limits<double>::min(),
|
||||
-1.0,
|
||||
-0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
std::numeric_limits<double>::max(),
|
||||
std::numeric_limits<double>::infinity(),
|
||||
std::numeric_limits<double>::quiet_NaN(),
|
||||
std::numeric_limits<double>::signaling_NaN()};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
TEST_F(CommonOperatorTest, Float32Constant) {
|
||||
TRACED_FOREACH(float, value, kFloat32Values) {
|
||||
TRACED_FOREACH(float, value, kFloatValues) {
|
||||
const Operator* op = common()->Float32Constant(value);
|
||||
EXPECT_FLOAT_EQ(value, OpParameter<float>(op));
|
||||
EXPECT_PRED2(base::bit_equal_to<float>(), value, OpParameter<float>(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetValueInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetControlOutputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetEffectOutputCount(op));
|
||||
EXPECT_EQ(1, OperatorProperties::GetValueOutputCount(op));
|
||||
}
|
||||
TRACED_FOREACH(float, v1, kFloatValues) {
|
||||
TRACED_FOREACH(float, v2, kFloatValues) {
|
||||
const Operator* op1 = common()->Float32Constant(v1);
|
||||
const Operator* op2 = common()->Float32Constant(v2);
|
||||
EXPECT_EQ(bit_cast<uint32_t>(v1) == bit_cast<uint32_t>(v2),
|
||||
op1->Equals(op2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_F(CommonOperatorTest, Float64Constant) {
|
||||
TRACED_FOREACH(double, value, kFloatValues) {
|
||||
const Operator* op = common()->Float64Constant(value);
|
||||
EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetValueInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetControlOutputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetEffectOutputCount(op));
|
||||
EXPECT_EQ(1, OperatorProperties::GetValueOutputCount(op));
|
||||
}
|
||||
TRACED_FOREACH(double, v1, kFloatValues) {
|
||||
TRACED_FOREACH(double, v2, kFloatValues) {
|
||||
const Operator* op1 = common()->Float64Constant(v1);
|
||||
const Operator* op2 = common()->Float64Constant(v2);
|
||||
EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
|
||||
op1->Equals(op2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_F(CommonOperatorTest, NumberConstant) {
|
||||
TRACED_FOREACH(double, value, kFloatValues) {
|
||||
const Operator* op = common()->NumberConstant(value);
|
||||
EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetValueInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetControlOutputCount(op));
|
||||
EXPECT_EQ(0, OperatorProperties::GetEffectOutputCount(op));
|
||||
EXPECT_EQ(1, OperatorProperties::GetValueOutputCount(op));
|
||||
}
|
||||
TRACED_FOREACH(double, v1, kFloatValues) {
|
||||
TRACED_FOREACH(double, v2, kFloatValues) {
|
||||
const Operator* op1 = common()->NumberConstant(v1);
|
||||
const Operator* op2 = common()->NumberConstant(v2);
|
||||
EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
|
||||
op1->Equals(op2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user