2015-01-30 09:29:25 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
#include "src/base/logging.h"
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
#include "src/objects.h"
|
2015-01-30 09:29:25 +00:00
|
|
|
#include "testing/gtest-support.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace base {
|
|
|
|
|
2016-11-29 15:02:16 +00:00
|
|
|
namespace {
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
|
|
|
|
#define CHECK_SUCCEED(NAME, lhs, rhs) \
|
|
|
|
{ \
|
|
|
|
std::string* error_message = \
|
|
|
|
Check##NAME##Impl<decltype(lhs), decltype(rhs)>((lhs), (rhs), ""); \
|
|
|
|
EXPECT_EQ(nullptr, error_message); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_FAIL(NAME, lhs, rhs) \
|
|
|
|
{ \
|
|
|
|
std::string* error_message = \
|
|
|
|
Check##NAME##Impl<decltype(lhs), decltype(rhs)>((lhs), (rhs), ""); \
|
|
|
|
EXPECT_NE(nullptr, error_message); \
|
2016-12-19 11:58:28 +00:00
|
|
|
delete error_message; \
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 15:02:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
TEST(LoggingTest, CheckEQImpl) {
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
CHECK_SUCCEED(EQ, 0.0, 0.0)
|
|
|
|
CHECK_SUCCEED(EQ, 0.0, -0.0)
|
|
|
|
CHECK_SUCCEED(EQ, -0.0, 0.0)
|
|
|
|
CHECK_SUCCEED(EQ, -0.0, -0.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareSignedMismatch) {
|
|
|
|
CHECK_SUCCEED(EQ, static_cast<int32_t>(14), static_cast<uint32_t>(14))
|
|
|
|
CHECK_FAIL(EQ, static_cast<int32_t>(14), static_cast<uint32_t>(15))
|
|
|
|
CHECK_FAIL(EQ, static_cast<int32_t>(-1), static_cast<uint32_t>(-1))
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(-1), static_cast<uint32_t>(0))
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(-1), static_cast<uint32_t>(-1))
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(-1), static_cast<uint32_t>(0))
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(55), static_cast<uint32_t>(55))
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(55), static_cast<uint32_t>(0x7fffff00))
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(55), static_cast<uint32_t>(0x7fffff00))
|
|
|
|
CHECK_SUCCEED(GE, static_cast<uint32_t>(0x7fffff00), static_cast<int32_t>(55))
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint32_t>(0x7fffff00), static_cast<int32_t>(55))
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint32_t>(-1), static_cast<int32_t>(-1))
|
|
|
|
CHECK_SUCCEED(GE, static_cast<uint32_t>(0), static_cast<int32_t>(-1))
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int8_t>(-1), static_cast<uint32_t>(0))
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint64_t>(0x7f01010101010101), 0)
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int64_t>(0xff01010101010101),
|
|
|
|
static_cast<uint8_t>(13))
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareAgainstStaticConstPointer) {
|
|
|
|
// These used to produce link errors before http://crrev.com/2524093002.
|
|
|
|
CHECK_FAIL(EQ, v8::internal::Smi::kZero, v8::internal::Smi::FromInt(17));
|
|
|
|
CHECK_SUCCEED(GT, 0, v8::internal::Smi::kMinValue);
|
2015-01-30 09:29:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 15:28:54 +00:00
|
|
|
TEST(LoggingTest, CompareWithDifferentSignedness) {
|
|
|
|
#define CHECK_BOTH(name, lhs, rhs) \
|
|
|
|
CHECK_##name(lhs, rhs); \
|
|
|
|
DCHECK_##name(lhs, rhs)
|
|
|
|
|
|
|
|
int32_t i32 = 10;
|
|
|
|
uint32_t u32 = 20;
|
|
|
|
int64_t i64 = 30;
|
|
|
|
uint64_t u64 = 40;
|
|
|
|
|
|
|
|
// All these checks should compile (!) and succeed.
|
|
|
|
CHECK_BOTH(EQ, i32 + 10, u32);
|
|
|
|
CHECK_BOTH(LT, i32, u64);
|
|
|
|
CHECK_BOTH(LE, u32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, i32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, u32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, !u32, !i64);
|
|
|
|
}
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
} // namespace base
|
|
|
|
} // namespace v8
|