Skip to content

Commit ab21820

Browse files
Abseil Teamfowles
authored andcommitted
Export of internal Abseil changes
-- e2de21d54c02b6419c57c0f4e2a16b608deca260 by Evan Brown <[email protected]>: Remove the InsertEnd benchmark. This benchmark has significantly different possible behaviors that can result in misleading metrics. Specifically, we can have a case where we are deallocating the last node in the b-tree in the erase and then allocating a new node in the insert call repeatedly, whereas normally, we end up just inserting/erasing a value from the last node. Also, the name of the benchmark is misleading because it involves an erase and an insert, but the name only mentions the insert. PiperOrigin-RevId: 360930639 -- 51f6bb97b9cbdb809c31b77e93ce080ca3cba9ea by Benjamin Barenblat <[email protected]>: Stop testing with double-double random variables On POWER, long double is often represented as a pair of doubles added together (double-double arithmetic). We’ve already special-cased double-double arithmetic in a number of tests, but compiler bugs [1, 2, 3] have now triggered both false positives and false negatives, which suggests testing with double doubles is unlikely to yield useful signal. Remove the special casing and detect if we’re on a double-double system; if so, just don’t test long doubles. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048 [2] https://bugs.llvm.org/show_bug.cgi?id=49131 [3] https://bugs.llvm.org/show_bug.cgi?id=49132 PiperOrigin-RevId: 360793161 -- 07fb4d7932c2f5d711c480f759dacb0be60f975e by Abseil Team <[email protected]>: internal change PiperOrigin-RevId: 360712825 GitOrigin-RevId: e2de21d54c02b6419c57c0f4e2a16b608deca260 Change-Id: I98389b5a8789dcc8f35abc00c767e909181665f0
1 parent b073597 commit ab21820

File tree

14 files changed

+138
-113
lines changed

14 files changed

+138
-113
lines changed

CMake/AbseilDll.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ set(ABSL_INTERNAL_DLL_FILES
131131
"numeric/int128.cc"
132132
"numeric/int128.h"
133133
"numeric/internal/bits.h"
134+
"numeric/internal/representation.h"
134135
"random/bernoulli_distribution.h"
135136
"random/beta_distribution.h"
136137
"random/bit_gen_ref.h"

absl/container/btree_benchmark.cc

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,39 +101,6 @@ void BM_InsertSorted(benchmark::State& state) {
101101
BM_InsertImpl<T>(state, true);
102102
}
103103

104-
// container::insert sometimes returns a pair<iterator, bool> and sometimes
105-
// returns an iterator (for multi- containers).
106-
template <typename Iter>
107-
Iter GetIterFromInsert(const std::pair<Iter, bool>& pair) {
108-
return pair.first;
109-
}
110-
template <typename Iter>
111-
Iter GetIterFromInsert(const Iter iter) {
112-
return iter;
113-
}
114-
115-
// Benchmark insertion of values into a container at the end.
116-
template <typename T>
117-
void BM_InsertEnd(benchmark::State& state) {
118-
using V = typename remove_pair_const<typename T::value_type>::type;
119-
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
120-
121-
T container;
122-
const int kSize = 10000;
123-
for (int i = 0; i < kSize; ++i) {
124-
container.insert(Generator<V>(kSize)(i));
125-
}
126-
V v = Generator<V>(kSize)(kSize - 1);
127-
typename T::key_type k = key_of_value(v);
128-
129-
auto it = container.find(k);
130-
while (state.KeepRunning()) {
131-
// Repeatedly removing then adding v.
132-
container.erase(it);
133-
it = GetIterFromInsert(container.insert(v));
134-
}
135-
}
136-
137104
// Benchmark inserting the first few elements in a container. In b-tree, this is
138105
// when the root node grows.
139106
template <typename T>
@@ -513,7 +480,6 @@ BTREE_TYPES(Time);
513480
#define MY_BENCHMARK3(type) \
514481
MY_BENCHMARK4(type, Insert); \
515482
MY_BENCHMARK4(type, InsertSorted); \
516-
MY_BENCHMARK4(type, InsertEnd); \
517483
MY_BENCHMARK4(type, InsertSmall); \
518484
MY_BENCHMARK4(type, Lookup); \
519485
MY_BENCHMARK4(type, FullLookup); \

absl/numeric/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ cc_test(
101101
"@com_github_google_benchmark//:benchmark_main",
102102
],
103103
)
104+
105+
cc_library(
106+
name = "representation",
107+
hdrs = [
108+
"internal/representation.h",
109+
],
110+
copts = ABSL_DEFAULT_COPTS,
111+
linkopts = ABSL_DEFAULT_LINKOPTS,
112+
deps = [
113+
"//absl/base:config",
114+
],
115+
)

absl/numeric/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,15 @@ absl_cc_library(
8686
absl::int128
8787
PUBLIC
8888
)
89+
90+
absl_cc_library(
91+
NAME
92+
numeric_representation
93+
HDRS
94+
"internal/representation.h"
95+
COPTS
96+
${ABSL_DEFAULT_COPTS}
97+
DEPS
98+
absl::config
99+
PUBLIC
100+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021 The Abseil Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
16+
#define ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
17+
18+
#include <limits>
19+
20+
#include "absl/base/config.h"
21+
22+
namespace absl {
23+
ABSL_NAMESPACE_BEGIN
24+
namespace numeric_internal {
25+
26+
// Returns true iff long double is represented as a pair of doubles added
27+
// together.
28+
inline constexpr bool IsDoubleDouble() {
29+
// A double-double value always has exactly twice the precision of a double
30+
// value--one double carries the high digits and one double carries the low
31+
// digits. This property is not shared with any other common floating-point
32+
// representation, so this test won't trigger false positives. For reference,
33+
// this table gives the number of bits of precision of each common
34+
// floating-point representation:
35+
//
36+
// type precision
37+
// IEEE single 24 b
38+
// IEEE double 53
39+
// x86 long double 64
40+
// double-double 106
41+
// IEEE quadruple 113
42+
//
43+
// Note in particular that a quadruple-precision float has greater precision
44+
// than a double-double float despite taking up the same amount of memory; the
45+
// quad has more of its bits allocated to the mantissa than the double-double
46+
// has.
47+
return std::numeric_limits<long double>::digits ==
48+
2 * std::numeric_limits<double>::digits;
49+
}
50+
51+
} // namespace numeric_internal
52+
ABSL_NAMESPACE_END
53+
} // namespace absl
54+
55+
#endif // ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_

absl/random/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ cc_test(
188188
":distributions",
189189
":random",
190190
"//absl/base:raw_logging_internal",
191+
"//absl/numeric:representation",
191192
"//absl/random/internal:distribution_test_util",
192193
"//absl/random/internal:pcg_engine",
193194
"//absl/random/internal:sequence_urbg",
@@ -308,6 +309,7 @@ cc_test(
308309
":random",
309310
"//absl/base:core_headers",
310311
"//absl/base:raw_logging_internal",
312+
"//absl/numeric:representation",
311313
"//absl/random/internal:distribution_test_util",
312314
"//absl/random/internal:pcg_engine",
313315
"//absl/random/internal:sequence_urbg",
@@ -331,6 +333,7 @@ cc_test(
331333
":random",
332334
"//absl/base:core_headers",
333335
"//absl/base:raw_logging_internal",
336+
"//absl/numeric:representation",
334337
"//absl/random/internal:distribution_test_util",
335338
"//absl/random/internal:sequence_urbg",
336339
"//absl/strings",
@@ -377,6 +380,7 @@ cc_test(
377380
":distributions",
378381
":random",
379382
"//absl/base:raw_logging_internal",
383+
"//absl/numeric:representation",
380384
"//absl/random/internal:distribution_test_util",
381385
"//absl/random/internal:pcg_engine",
382386
"//absl/random/internal:sequence_urbg",

absl/random/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ absl_cc_test(
259259
LINKOPTS
260260
${ABSL_DEFAULT_LINKOPTS}
261261
DEPS
262+
absl::numeric_representation
262263
absl::random_distributions
263264
absl::random_random
264265
absl::random_internal_distribution_test_util
@@ -381,6 +382,7 @@ absl_cc_test(
381382
${ABSL_DEFAULT_LINKOPTS}
382383
DEPS
383384
absl::core_headers
385+
absl::numeric_representation
384386
absl::random_distributions
385387
absl::random_internal_distribution_test_util
386388
absl::random_internal_pcg_engine
@@ -404,6 +406,7 @@ absl_cc_test(
404406
${ABSL_DEFAULT_LINKOPTS}
405407
DEPS
406408
absl::core_headers
409+
absl::numeric_representation
407410
absl::random_distributions
408411
absl::random_internal_distribution_test_util
409412
absl::random_internal_sequence_urbg
@@ -446,6 +449,7 @@ absl_cc_test(
446449
LINKOPTS
447450
${ABSL_DEFAULT_LINKOPTS}
448451
DEPS
452+
absl::numeric_representation
449453
absl::random_distributions
450454
absl::random_internal_distribution_test_util
451455
absl::random_internal_pcg_engine

absl/random/beta_distribution_test.cc

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
#include <random>
2222
#include <sstream>
2323
#include <string>
24+
#include <type_traits>
2425
#include <unordered_map>
2526
#include <vector>
2627

2728
#include "gmock/gmock.h"
2829
#include "gtest/gtest.h"
2930
#include "absl/base/internal/raw_logging.h"
31+
#include "absl/numeric/internal/representation.h"
3032
#include "absl/random/internal/chi_square.h"
3133
#include "absl/random/internal/distribution_test_util.h"
3234
#include "absl/random/internal/pcg_engine.h"
@@ -42,7 +44,15 @@ namespace {
4244
template <typename IntType>
4345
class BetaDistributionInterfaceTest : public ::testing::Test {};
4446

45-
using RealTypes = ::testing::Types<float, double, long double>;
47+
// double-double arithmetic is not supported well by either GCC or Clang; see
48+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048,
49+
// https://bugs.llvm.org/show_bug.cgi?id=49131, and
50+
// https://bugs.llvm.org/show_bug.cgi?id=49132. Don't bother running these tests
51+
// with double doubles until compiler support is better.
52+
using RealTypes =
53+
std::conditional<absl::numeric_internal::IsDoubleDouble(),
54+
::testing::Types<float, double>,
55+
::testing::Types<float, double, long double>>::type;
4656
TYPED_TEST_CASE(BetaDistributionInterfaceTest, RealTypes);
4757

4858
TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) {
@@ -53,9 +63,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) {
5363
const TypeParam kLargeA =
5464
std::exp(std::log((std::numeric_limits<TypeParam>::max)()) -
5565
std::log(std::log((std::numeric_limits<TypeParam>::max)())));
56-
const TypeParam kLargeAPPC = std::exp(
57-
std::log((std::numeric_limits<TypeParam>::max)()) -
58-
std::log(std::log((std::numeric_limits<TypeParam>::max)())) - 10.0f);
5966
using param_type = typename absl::beta_distribution<TypeParam>::param_type;
6067

6168
constexpr int kCount = 1000;
@@ -76,9 +83,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) {
7683
kLargeA, //
7784
std::nextafter(kLargeA, TypeParam(0)), //
7885
std::nextafter(kLargeA, std::numeric_limits<TypeParam>::max()),
79-
kLargeAPPC, //
80-
std::nextafter(kLargeAPPC, TypeParam(0)),
81-
std::nextafter(kLargeAPPC, std::numeric_limits<TypeParam>::max()),
8286
// Boundary cases.
8387
std::numeric_limits<TypeParam>::max(),
8488
std::numeric_limits<TypeParam>::epsilon(),
@@ -125,28 +129,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) {
125129

126130
ss >> after;
127131

128-
#if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \
129-
defined(__ppc__) || defined(__PPC__)
130-
if (std::is_same<TypeParam, long double>::value) {
131-
// Roundtripping floating point values requires sufficient precision
132-
// to reconstruct the exact value. It turns out that long double
133-
// has some errors doing this on ppc.
134-
if (alpha <= std::numeric_limits<double>::max() &&
135-
alpha >= std::numeric_limits<double>::lowest()) {
136-
EXPECT_EQ(static_cast<double>(before.alpha()),
137-
static_cast<double>(after.alpha()))
138-
<< ss.str();
139-
}
140-
if (beta <= std::numeric_limits<double>::max() &&
141-
beta >= std::numeric_limits<double>::lowest()) {
142-
EXPECT_EQ(static_cast<double>(before.beta()),
143-
static_cast<double>(after.beta()))
144-
<< ss.str();
145-
}
146-
continue;
147-
}
148-
#endif
149-
150132
EXPECT_EQ(before.alpha(), after.alpha());
151133
EXPECT_EQ(before.beta(), after.beta());
152134
EXPECT_EQ(before, after) //

absl/random/exponential_distribution_test.cc

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "gtest/gtest.h"
3131
#include "absl/base/internal/raw_logging.h"
3232
#include "absl/base/macros.h"
33+
#include "absl/numeric/internal/representation.h"
3334
#include "absl/random/internal/chi_square.h"
3435
#include "absl/random/internal/distribution_test_util.h"
3536
#include "absl/random/internal/pcg_engine.h"
@@ -47,7 +48,15 @@ using absl::random_internal::kChiSquared;
4748
template <typename RealType>
4849
class ExponentialDistributionTypedTest : public ::testing::Test {};
4950

50-
using RealTypes = ::testing::Types<float, double, long double>;
51+
// double-double arithmetic is not supported well by either GCC or Clang; see
52+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048,
53+
// https://bugs.llvm.org/show_bug.cgi?id=49131, and
54+
// https://bugs.llvm.org/show_bug.cgi?id=49132. Don't bother running these tests
55+
// with double doubles until compiler support is better.
56+
using RealTypes =
57+
std::conditional<absl::numeric_internal::IsDoubleDouble(),
58+
::testing::Types<float, double>,
59+
::testing::Types<float, double, long double>>::type;
5160
TYPED_TEST_CASE(ExponentialDistributionTypedTest, RealTypes);
5261

5362
TYPED_TEST(ExponentialDistributionTypedTest, SerializeTest) {
@@ -126,23 +135,6 @@ TYPED_TEST(ExponentialDistributionTypedTest, SerializeTest) {
126135

127136
ss >> after;
128137

129-
#if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \
130-
defined(__ppc__) || defined(__PPC__)
131-
if (std::is_same<TypeParam, long double>::value) {
132-
// Roundtripping floating point values requires sufficient precision to
133-
// reconstruct the exact value. It turns out that long double has some
134-
// errors doing this on ppc, particularly for values
135-
// near {1.0 +/- epsilon}.
136-
if (lambda <= std::numeric_limits<double>::max() &&
137-
lambda >= std::numeric_limits<double>::lowest()) {
138-
EXPECT_EQ(static_cast<double>(before.lambda()),
139-
static_cast<double>(after.lambda()))
140-
<< ss.str();
141-
}
142-
continue;
143-
}
144-
#endif
145-
146138
EXPECT_EQ(before.lambda(), after.lambda()) //
147139
<< ss.str() << " " //
148140
<< (ss.good() ? "good " : "") //

absl/random/gaussian_distribution_test.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
#include <iterator>
2222
#include <random>
2323
#include <string>
24+
#include <type_traits>
2425
#include <vector>
2526

2627
#include "gmock/gmock.h"
2728
#include "gtest/gtest.h"
2829
#include "absl/base/internal/raw_logging.h"
2930
#include "absl/base/macros.h"
31+
#include "absl/numeric/internal/representation.h"
3032
#include "absl/random/internal/chi_square.h"
3133
#include "absl/random/internal/distribution_test_util.h"
3234
#include "absl/random/internal/sequence_urbg.h"
@@ -43,7 +45,15 @@ using absl::random_internal::kChiSquared;
4345
template <typename RealType>
4446
class GaussianDistributionInterfaceTest : public ::testing::Test {};
4547

46-
using RealTypes = ::testing::Types<float, double, long double>;
48+
// double-double arithmetic is not supported well by either GCC or Clang; see
49+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048,
50+
// https://bugs.llvm.org/show_bug.cgi?id=49131, and
51+
// https://bugs.llvm.org/show_bug.cgi?id=49132. Don't bother running these tests
52+
// with double doubles until compiler support is better.
53+
using RealTypes =
54+
std::conditional<absl::numeric_internal::IsDoubleDouble(),
55+
::testing::Types<float, double>,
56+
::testing::Types<float, double, long double>>::type;
4757
TYPED_TEST_CASE(GaussianDistributionInterfaceTest, RealTypes);
4858

4959
TYPED_TEST(GaussianDistributionInterfaceTest, SerializeTest) {
@@ -129,29 +139,6 @@ TYPED_TEST(GaussianDistributionInterfaceTest, SerializeTest) {
129139

130140
ss >> after;
131141

132-
#if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \
133-
defined(__ppc__) || defined(__PPC__)
134-
if (std::is_same<TypeParam, long double>::value) {
135-
// Roundtripping floating point values requires sufficient precision
136-
// to reconstruct the exact value. It turns out that long double
137-
// has some errors doing this on ppc, particularly for values
138-
// near {1.0 +/- epsilon}.
139-
if (mean <= std::numeric_limits<double>::max() &&
140-
mean >= std::numeric_limits<double>::lowest()) {
141-
EXPECT_EQ(static_cast<double>(before.mean()),
142-
static_cast<double>(after.mean()))
143-
<< ss.str();
144-
}
145-
if (stddev <= std::numeric_limits<double>::max() &&
146-
stddev >= std::numeric_limits<double>::lowest()) {
147-
EXPECT_EQ(static_cast<double>(before.stddev()),
148-
static_cast<double>(after.stddev()))
149-
<< ss.str();
150-
}
151-
continue;
152-
}
153-
#endif
154-
155142
EXPECT_EQ(before.mean(), after.mean());
156143
EXPECT_EQ(before.stddev(), after.stddev()) //
157144
<< ss.str() << " " //

0 commit comments

Comments
 (0)