Commit 5cbe0f5d authored by lpy's avatar lpy Committed by Commit bot

Create TimeBase for time related classes.

Currently we have Time and TimeTicks sharing some methods. This patch
creates TimeBase, and makes Time and TimeTicks inherits from it, so that time
related classes won't have to implement common methods and it's easier to
introduce new time related classes.

BUG=v8:4990
LOG=n

Review-Url: https://codereview.chromium.org/1952843002
Cr-Commit-Position: refs/heads/master@{#36088}
parent fd2dbf1d
......@@ -7,6 +7,7 @@
#include <limits>
#include "src/base/logging.h"
#include "src/base/safe_math.h"
namespace v8 {
namespace base {
......@@ -48,6 +49,35 @@ int32_t SignedMod32(int32_t lhs, int32_t rhs) {
return lhs % rhs;
}
int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value) {
if (value.IsValid())
return value.ValueUnsafe();
// We could return max/min but we don't really expose what the maximum delta
// is. Instead, return max/(-max), which is something that clients can reason
// about.
// TODO(rvargas) crbug.com/332611: don't use internal values.
int64_t limit = std::numeric_limits<int64_t>::max();
if (value.validity() == internal::RANGE_UNDERFLOW)
limit = -limit;
return value.ValueOrDefault(limit);
}
int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs) {
internal::CheckedNumeric<int64_t> rv(lhs);
rv += rhs;
return FromCheckedNumeric(rv);
}
int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs) {
internal::CheckedNumeric<int64_t> rv(lhs);
rv -= rhs;
return FromCheckedNumeric(rv);
}
} // namespace bits
} // namespace base
} // namespace v8
......@@ -16,6 +16,12 @@
namespace v8 {
namespace base {
namespace internal {
template <typename T>
class CheckedNumeric;
}
namespace bits {
// CountPopulation32(value) returns the number of bits set in |value|.
......@@ -296,6 +302,21 @@ inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) {
return rhs ? lhs % rhs : 0u;
}
// Clamp |value| on overflow and underflow conditions.
int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value);
// SignedSaturatedAdd64(lhs, rhs) adds |lhs| and |rhs|,
// checks and returns the result.
int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs);
// SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|,
// checks and returns the result.
int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs);
} // namespace bits
} // namespace base
} // namespace v8
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment