Commit 9bbbbd3b by Abseil Team Committed by Copybara-Service

Optimize `absl::Duration` division and modulo: Avoid repeated redundant…

Optimize `absl::Duration` division and modulo: Avoid repeated redundant comparisons in `IDivFastPath`.

PiperOrigin-RevId: 610758911
Change-Id: I3d0fa2f52a3bd75bdd2b5c365d79878b4160bd29
parent bde089f9
...@@ -280,34 +280,36 @@ inline bool IDivFastPath(const Duration num, const Duration den, int64_t* q, ...@@ -280,34 +280,36 @@ inline bool IDivFastPath(const Duration num, const Duration den, int64_t* q,
int64_t den_hi = time_internal::GetRepHi(den); int64_t den_hi = time_internal::GetRepHi(den);
uint32_t den_lo = time_internal::GetRepLo(den); uint32_t den_lo = time_internal::GetRepLo(den);
if (den_hi == 0 && den_lo == kTicksPerNanosecond) { if (den_hi == 0) {
if (den_lo == kTicksPerNanosecond) {
// Dividing by 1ns // Dividing by 1ns
if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000000) { if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000000) {
*q = num_hi * 1000000000 + num_lo / kTicksPerNanosecond; *q = num_hi * 1000000000 + num_lo / kTicksPerNanosecond;
*rem = time_internal::MakeDuration(0, num_lo % den_lo); *rem = time_internal::MakeDuration(0, num_lo % den_lo);
return true; return true;
} }
} else if (den_hi == 0 && den_lo == 100 * kTicksPerNanosecond) { } else if (den_lo == 100 * kTicksPerNanosecond) {
// Dividing by 100ns (common when converting to Universal time) // Dividing by 100ns (common when converting to Universal time)
if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 10000000) { if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 10000000) {
*q = num_hi * 10000000 + num_lo / (100 * kTicksPerNanosecond); *q = num_hi * 10000000 + num_lo / (100 * kTicksPerNanosecond);
*rem = time_internal::MakeDuration(0, num_lo % den_lo); *rem = time_internal::MakeDuration(0, num_lo % den_lo);
return true; return true;
} }
} else if (den_hi == 0 && den_lo == 1000 * kTicksPerNanosecond) { } else if (den_lo == 1000 * kTicksPerNanosecond) {
// Dividing by 1us // Dividing by 1us
if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000) { if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000) {
*q = num_hi * 1000000 + num_lo / (1000 * kTicksPerNanosecond); *q = num_hi * 1000000 + num_lo / (1000 * kTicksPerNanosecond);
*rem = time_internal::MakeDuration(0, num_lo % den_lo); *rem = time_internal::MakeDuration(0, num_lo % den_lo);
return true; return true;
} }
} else if (den_hi == 0 && den_lo == 1000000 * kTicksPerNanosecond) { } else if (den_lo == 1000000 * kTicksPerNanosecond) {
// Dividing by 1ms // Dividing by 1ms
if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000) { if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000) {
*q = num_hi * 1000 + num_lo / (1000000 * kTicksPerNanosecond); *q = num_hi * 1000 + num_lo / (1000000 * kTicksPerNanosecond);
*rem = time_internal::MakeDuration(0, num_lo % den_lo); *rem = time_internal::MakeDuration(0, num_lo % den_lo);
return true; return true;
} }
}
} else if (den_hi > 0 && den_lo == 0) { } else if (den_hi > 0 && den_lo == 0) {
// Dividing by positive multiple of 1s // Dividing by positive multiple of 1s
if (num_hi >= 0) { if (num_hi >= 0) {
......
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