Skip to content

Commit b073597

Browse files
Abseil Teamfowles
authored andcommitted
Export of internal Abseil changes
-- a74bdb72c3a6983e08a805938dd0e20e97d55bba by Abseil Team <[email protected]>: Fix typo: calcualte -> calculate PiperOrigin-RevId: 360515509 -- 3ddf8ac194e81a13e9de095e59dd061c1beacfe3 by Benjamin Barenblat <[email protected]>: Make tests tolerant of FMA contraction Weaken Duration.ToDoubleSecondsCheckEdgeCases and Duration.ToDoubleSecondsCheckRandom to make them less sensitive to fused multiply/add contraction. PiperOrigin-RevId: 360297653 GitOrigin-RevId: a74bdb72c3a6983e08a805938dd0e20e97d55bba Change-Id: I0c55383bc13040ea77511c4130d142368103dc57
1 parent a766987 commit b073597

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

absl/numeric/internal/bits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
343343
typename std::enable_if<std::is_unsigned<T>::value, T>::type
344344
BitCeilNonPowerOf2(T x) {
345345
// If T is narrower than unsigned, it undergoes promotion to unsigned when we
346-
// shift. We calcualte the number of bits added by the wider type.
346+
// shift. We calculate the number of bits added by the wider type.
347347
return BitCeilPromotionHelper(
348348
static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
349349
T{sizeof(T) >= sizeof(unsigned) ? 0

absl/time/duration_test.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,13 @@ TEST(Duration, SmallConversions) {
13691369
EXPECT_THAT(ToTimeval(absl::Nanoseconds(2000)), TimevalMatcher(tv));
13701370
}
13711371

1372-
void VerifySameAsMul(double time_as_seconds, int* const misses) {
1372+
void VerifyApproxSameAsMul(double time_as_seconds, int* const misses) {
13731373
auto direct_seconds = absl::Seconds(time_as_seconds);
13741374
auto mul_by_one_second = time_as_seconds * absl::Seconds(1);
1375-
if (direct_seconds != mul_by_one_second) {
1375+
// These are expected to differ by up to one tick due to fused multiply/add
1376+
// contraction.
1377+
if (absl::AbsDuration(direct_seconds - mul_by_one_second) >
1378+
absl::time_internal::MakeDuration(0, 1u)) {
13761379
if (*misses > 10) return;
13771380
ASSERT_LE(++(*misses), 10) << "Too many errors, not reporting more.";
13781381
EXPECT_EQ(direct_seconds, mul_by_one_second)
@@ -1384,7 +1387,8 @@ void VerifySameAsMul(double time_as_seconds, int* const misses) {
13841387
// For a variety of interesting durations, we find the exact point
13851388
// where one double converts to that duration, and the very next double
13861389
// converts to the next duration. For both of those points, verify that
1387-
// Seconds(point) returns the same duration as point * Seconds(1.0)
1390+
// Seconds(point) returns a duration near point * Seconds(1.0). (They may
1391+
// not be exactly equal due to fused multiply/add contraction.)
13881392
TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
13891393
constexpr uint32_t kTicksPerSecond = absl::time_internal::kTicksPerSecond;
13901394
constexpr auto duration_tick = absl::time_internal::MakeDuration(0, 1u);
@@ -1423,8 +1427,8 @@ TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
14231427
}
14241428
// Now low_edge is the highest double that converts to Duration d,
14251429
// and high_edge is the lowest double that converts to Duration after_d.
1426-
VerifySameAsMul(low_edge, &misses);
1427-
VerifySameAsMul(high_edge, &misses);
1430+
VerifyApproxSameAsMul(low_edge, &misses);
1431+
VerifyApproxSameAsMul(high_edge, &misses);
14281432
}
14291433
}
14301434
}
@@ -1444,8 +1448,8 @@ TEST(Duration, ToDoubleSecondsCheckRandom) {
14441448
int misses = 0;
14451449
for (int i = 0; i < 1000000; ++i) {
14461450
double d = std::exp(uniform(gen));
1447-
VerifySameAsMul(d, &misses);
1448-
VerifySameAsMul(-d, &misses);
1451+
VerifyApproxSameAsMul(d, &misses);
1452+
VerifyApproxSameAsMul(-d, &misses);
14491453
}
14501454
}
14511455

0 commit comments

Comments
 (0)