Commit 8ec15777 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Make a few InXXX() functions constexpr.

With the recent saturation fixes, these are now simple enough to
inline without increasing binary size significantly.  Doing this
allows using them in constexpr contexts, of which there are a few (at
least for InMinutes()).

On my 64-bit Windows "official" non-branded release build, chrome.dll
had the following sizes:

Before saturation fixes: 147661312 bytes
After saturation fixes:  147660800 bytes
After this constexpr CL: 147663872 bytes

Bug: none
Change-Id: I786ed3f36e0952bd766d3826f0eca34fe676af30
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2281730
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarSatoru Takabayashi <satorux@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785640}
parent 6415e4fa
......@@ -68,18 +68,6 @@ int TimeDelta::InDaysFloored() const {
return result;
}
int TimeDelta::InHours() const {
// saturated_cast<> is necessary since very large (but still less than
// min/max) deltas would result in overflow.
return saturated_cast<int>(delta_ / Time::kMicrosecondsPerHour);
}
int TimeDelta::InMinutes() const {
// saturated_cast<> is necessary since very large (but still less than
// min/max) deltas would result in overflow.
return saturated_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
}
double TimeDelta::InSecondsF() const {
if (is_max()) {
// Preserve max to prevent overflow.
......@@ -157,10 +145,6 @@ double TimeDelta::InMicrosecondsF() const {
return static_cast<double>(delta_);
}
int64_t TimeDelta::InNanoseconds() const {
return base::ClampMul(delta_, Time::kNanosecondsPerMicrosecond);
}
std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
return os << time_delta.InSecondsF() << " s";
}
......
......@@ -237,8 +237,8 @@ class BASE_EXPORT TimeDelta {
// Hence, floating point values should not be used for storage.
int InDays() const;
int InDaysFloored() const;
int InHours() const;
int InMinutes() const;
constexpr int InHours() const;
constexpr int InMinutes() const;
double InSecondsF() const;
int64_t InSeconds() const;
double InMillisecondsF() const;
......@@ -246,7 +246,7 @@ class BASE_EXPORT TimeDelta {
int64_t InMillisecondsRoundedUp() const;
constexpr int64_t InMicroseconds() const { return delta_; }
double InMicrosecondsF() const;
int64_t InNanoseconds() const;
constexpr int64_t InNanoseconds() const;
// Computations with other deltas.
constexpr TimeDelta operator+(TimeDelta other) const {
......@@ -895,6 +895,22 @@ constexpr TimeDelta TimeDelta::FromNanosecondsD(double ns) {
return FromDouble(ns / Time::kNanosecondsPerMicrosecond);
}
constexpr int TimeDelta::InHours() const {
// saturated_cast<> is necessary since very large (but still less than
// min/max) deltas would result in overflow.
return saturated_cast<int>(delta_ / Time::kMicrosecondsPerHour);
}
constexpr int TimeDelta::InMinutes() const {
// saturated_cast<> is necessary since very large (but still less than
// min/max) deltas would result in overflow.
return saturated_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
}
constexpr int64_t TimeDelta::InNanoseconds() const {
return base::ClampMul(delta_, Time::kNanosecondsPerMicrosecond);
}
// static
constexpr TimeDelta TimeDelta::Max() {
return TimeDelta(std::numeric_limits<int64_t>::max());
......
......@@ -1450,17 +1450,21 @@ TEST(TimeDelta, InXXXOverflow) {
constexpr TimeDelta kLargeDelta =
TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::max() - 1);
static_assert(!kLargeDelta.is_max(), "");
EXPECT_EQ(std::numeric_limits<int>::max(), kLargeDelta.InHours());
EXPECT_EQ(std::numeric_limits<int>::max(), kLargeDelta.InMinutes());
EXPECT_EQ(std::numeric_limits<int64_t>::max(), kLargeDelta.InNanoseconds());
static_assert(std::numeric_limits<int>::max() == kLargeDelta.InHours(), "");
static_assert(std::numeric_limits<int>::max() == kLargeDelta.InMinutes(), "");
static_assert(
std::numeric_limits<int64_t>::max() == kLargeDelta.InNanoseconds(), "");
constexpr TimeDelta kLargeNegative =
TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min() + 1);
static_assert(!kLargeNegative.is_min(), "");
EXPECT_EQ(std::numeric_limits<int>::min(), kLargeNegative.InHours());
EXPECT_EQ(std::numeric_limits<int>::min(), kLargeNegative.InMinutes());
EXPECT_EQ(std::numeric_limits<int64_t>::min(),
kLargeNegative.InNanoseconds());
static_assert(std::numeric_limits<int>::min() == kLargeNegative.InHours(),
"");
static_assert(std::numeric_limits<int>::min() == kLargeNegative.InMinutes(),
"");
static_assert(
std::numeric_limits<int64_t>::min() == kLargeNegative.InNanoseconds(),
"");
}
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......
......@@ -46,8 +46,7 @@ constexpr base::TimeDelta kUserInputEventsDuration =
base::TimeDelta::FromMinutes(60);
// Granularity of input events is per minute.
constexpr int kNumUserInputEventsBuckets =
kUserInputEventsDuration / base::TimeDelta::FromMinutes(1);
constexpr int kNumUserInputEventsBuckets = kUserInputEventsDuration.InMinutes();
// Returns the focused visible browser unless no visible browser is focused,
// then returns the topmost visible browser.
......
......@@ -52,7 +52,7 @@ class IdleEventNotifier : public PowerManagerClient::Observer,
// Granularity of input events is per minute.
static constexpr int kNumUserInputEventsBuckets =
kUserInputEventsDuration / base::TimeDelta::FromMinutes(1);
kUserInputEventsDuration.InMinutes();
struct ActivityData {
ActivityData();
......
......@@ -44,8 +44,7 @@ constexpr base::TimeDelta kUserActivityDuration =
base::TimeDelta::FromMinutes(30);
// Granularity of input events is per minute.
constexpr int kNumUserInputEventsBuckets =
kUserActivityDuration / base::TimeDelta::FromMinutes(1);
constexpr int kNumUserInputEventsBuckets = kUserActivityDuration.InMinutes();
constexpr char kSavedFileName[] = "past_charging_events.pb";
constexpr char kSavedDir[] = "smartcharging";
......
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