Export of internal Abseil changes

--
0db7f4046f9b59c0f8c3df2f0eb7fd88fc328439 by Abseil Team <absl-team@google.com>:

Revise documentation of bit_cast:

* Removes inappropriate examples (round-tripping pointers, serialization), for which reinterpret_cast is more appropriate.
* Removes mention of "bit representation", which is not an explicit notion in C++. The best we get is "byte representation".
* Removes a circular defition of "bitcast" as itself, and instead explains what it does.
* Removes the mathism "for some values of", which is probably not totally accessible to a general audience, and in any case needless verbiage.
* Fixes comments in the example.
* Replaces some colloquialisms with simpler, more direct language.

PiperOrigin-RevId: 421791786

--
e04e64df55d93c1b9a09c0483b97cc4d8763260d by Derek Mauro <dmauro@google.com>:

Update Docker image to use GCC 11.2, Clang 14 (prerelease),
CMake 3.22.1, and Bazel 4.2.2

PiperOrigin-RevId: 421658559

--
d002bb3dc5cd1fc5b4cbd79a450efc894caa567c by Chris Kennelly <ckennelly@google.com>:

Add a small microbenchmark for absl::bit_width.

PiperOrigin-RevId: 421604852

--
131b057d1b76ecd7170421b48d661bb958ff676b by Evan Brown <ezb@google.com>:

Adds a disabled test for EBO in nested `CompressedTuple`s.

PiperOrigin-RevId: 421413134

--
e34c7876d3a1212d90c73c030ccae6169b682d43 by Jorg Brown <jorg@google.com>:

Show users a better error message if they pass a pointer to absl::Uniform.

PiperOrigin-RevId: 421090472
GitOrigin-RevId: 0db7f4046f9b59c0f8c3df2f0eb7fd88fc328439
Change-Id: I5a004e8d17e974fa4897a09d1466ae8fc65dfdbb
pull/1095/head
Abseil Team 3 years ago committed by vslashg
parent 2a042b082c
commit c59e7e59f5
  1. 57
      absl/base/casts.h
  2. 10
      absl/container/internal/compressed_tuple_test.cc
  3. 14
      absl/numeric/BUILD.bazel
  4. 73
      absl/numeric/bits_benchmark.cc
  5. 2
      absl/random/internal/distribution_caller.h
  6. 4
      ci/linux_clang-latest_libcxx_asan_bazel.sh
  7. 4
      ci/linux_clang-latest_libcxx_bazel.sh
  8. 4
      ci/linux_clang-latest_libcxx_tsan_bazel.sh
  9. 4
      ci/linux_docker_containers.sh

@ -105,47 +105,50 @@ constexpr To implicit_cast(typename absl::internal::identity_t<To> to) {
// bit_cast() // bit_cast()
// //
// Performs a bitwise cast on a type without changing the underlying bit // Creates a value of the new type `Dest` whose representation is the same as
// representation of that type's value. The two types must be of the same size // that of the argument, which is of (deduced) type `Source` (a "bitwise cast";
// and both types must be trivially copyable. As with most casts, use with // every bit in the value representation of the result is equal to the
// caution. A `bit_cast()` might be needed when you need to temporarily treat a // corresponding bit in the object representation of the source). Source and
// type as some other type, such as in the following cases: // destination types must be of the same size, and both types must be trivially
// // copyable.
// * Serialization (casting temporarily to `char *` for those purposes is //
// always allowed by the C++ standard) // As with most casts, use with caution. A `bit_cast()` might be needed when you
// * Managing the individual bits of a type within mathematical operations // need to treat a value as the value of some other type, for example, to access
// that are not normally accessible through that type // the individual bits of an object which are not normally accessible through
// * Casting non-pointer types to pointer types (casting the other way is // the object's type, such as for working with the binary representation of a
// allowed by `reinterpret_cast()` but round-trips cannot occur the other // floating point value:
// way).
//
// Example:
// //
// float f = 3.14159265358979; // float f = 3.14159265358979;
// int i = bit_cast<int32_t>(f); // int i = bit_cast<int32_t>(f);
// // i = 0x40490fdb // // i = 0x40490fdb
// //
// Casting non-pointer types to pointer types and then dereferencing them // Reinterpreting and accessing a value directly as a different type (as shown
// traditionally produces undefined behavior. // below) usually results in undefined behavior.
// //
// Example: // Example:
// //
// // WRONG // // WRONG
// float f = 3.14159265358979; // WRONG // float f = 3.14159265358979;
// int i = * reinterpret_cast<int*>(&f); // WRONG // int i = reinterpret_cast<int&>(f); // Wrong
// int j = *reinterpret_cast<int*>(&f); // Equally wrong
// int k = *bit_cast<int*>(&f); // Equally wrong
//
// Reinterpret-casting results in undefined behavior according to the ISO C++
// specification, section [basic.lval]. Roughly, this section says: if an object
// in memory has one type, and a program accesses it with a different type, the
// result is undefined behavior for most "different type".
// //
// The address-casting method produces undefined behavior according to the ISO // Using bit_cast on a pointer and then dereferencing it is no better than using
// C++ specification section [basic.lval]. Roughly, this section says: if an // reinterpret_cast. You should only use bit_cast on the value itself.
// object in memory has one type, and a program accesses it with a different
// type, the result is undefined behavior for most values of "different type".
// //
// Such casting results in type punning: holding an object in memory of one type // Such casting results in type punning: holding an object in memory of one type
// and reading its bits back using a different type. A `bit_cast()` avoids this // and reading its bits back using a different type. A `bit_cast()` avoids this
// issue by implementing its casts using `memcpy()`, which avoids introducing // issue by copying the object representation to a new value, which avoids
// this undefined behavior. // introducing this undefined behavior (since the original value is never
// accessed in the wrong way).
// //
// NOTE: The requirements here are more strict than the bit_cast of standard // NOTE: The requirements here are stricter than the bit_cast of standard
// proposal p0476 due to the need for workarounds and lack of intrinsics. // proposal P0476 due to the need for workarounds and lack of intrinsics.
// Specifically, this implementation also requires `Dest` to be // Specifically, this implementation also requires `Dest` to be
// default-constructible. // default-constructible.
template < template <

@ -403,6 +403,16 @@ TEST(CompressedTupleTest, EmptyFinalClass) {
} }
#endif #endif
// TODO(b/214288561): enable this test.
TEST(CompressedTupleTest, DISABLED_NestedEbo) {
struct Empty1 {};
struct Empty2 {};
CompressedTuple<Empty1, CompressedTuple<Empty2>, int> x;
CompressedTuple<Empty1, Empty2, int> y;
// Currently fails with sizeof(x) == 8, sizeof(y) == 4.
EXPECT_EQ(sizeof(x), sizeof(y));
}
} // namespace } // namespace
} // namespace container_internal } // namespace container_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END

@ -37,6 +37,20 @@ cc_library(
], ],
) )
cc_binary(
name = "bits_benchmark",
testonly = 1,
srcs = ["bits_benchmark.cc"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":bits",
"//absl/base:core_headers",
"//absl/random",
"@com_github_google_benchmark//:benchmark_main",
],
)
cc_test( cc_test(
name = "bits_test", name = "bits_test",
size = "small", size = "small",

@ -0,0 +1,73 @@
// Copyright 2022 The Abseil Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include <vector>
#include "benchmark/benchmark.h"
#include "absl/base/optimization.h"
#include "absl/numeric/bits.h"
#include "absl/random/random.h"
namespace absl {
namespace {
template <typename T>
static void BM_bitwidth(benchmark::State& state) {
const int count = state.range(0);
absl::BitGen rng;
std::vector<T> values;
values.reserve(count);
for (int i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 0, std::numeric_limits<T>::max()));
}
while (state.KeepRunningBatch(count)) {
for (int i = 0; i < count; ++i) {
benchmark::DoNotOptimize(values[i]);
}
}
}
BENCHMARK_TEMPLATE(BM_bitwidth, uint8_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth, uint16_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth, uint32_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth, uint64_t)->Range(1, 1 << 20);
template <typename T>
static void BM_bitwidth_nonzero(benchmark::State& state) {
const int count = state.range(0);
absl::BitGen rng;
std::vector<T> values;
values.reserve(count);
for (int i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 1, std::numeric_limits<T>::max()));
}
while (state.KeepRunningBatch(count)) {
for (int i = 0; i < count; ++i) {
const T value = values[i];
ABSL_INTERNAL_ASSUME(value > 0);
benchmark::DoNotOptimize(value);
}
}
}
BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint8_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint16_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint32_t)->Range(1, 1 << 20);
BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint64_t)->Range(1, 1 << 20);
} // namespace
} // namespace absl

@ -32,6 +32,8 @@ namespace random_internal {
// to intercept such calls. // to intercept such calls.
template <typename URBG> template <typename URBG>
struct DistributionCaller { struct DistributionCaller {
static_assert(!std::is_pointer<URBG>::value,
"You must pass a reference, not a pointer.");
// SFINAE to detect whether the URBG type includes a member matching // SFINAE to detect whether the URBG type includes a member matching
// bool InvokeMock(base_internal::FastTypeIdType, void*, void*). // bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
// //

@ -70,8 +70,8 @@ for std in ${STD}; do
--rm \ --rm \
-e CC="/opt/llvm/clang/bin/clang" \ -e CC="/opt/llvm/clang/bin/clang" \
-e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \
-e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib" \ -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib/x86_64-unknown-linux-gnu:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib/x86_64-unknown-linux-gnu" \
-e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \ -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \ ${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \ ${DOCKER_CONTAINER} \
/usr/local/bin/bazel test ... \ /usr/local/bin/bazel test ... \

@ -71,8 +71,8 @@ for std in ${STD}; do
--rm \ --rm \
-e CC="/opt/llvm/clang/bin/clang" \ -e CC="/opt/llvm/clang/bin/clang" \
-e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \
-e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib" \ -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib/x86_64-unknown-linux-gnu:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib/x86_64-unknown-linux-gnu" \
-e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \ -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \ ${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \ ${DOCKER_CONTAINER} \
/bin/sh -c " /bin/sh -c "

@ -70,8 +70,8 @@ for std in ${STD}; do
--rm \ --rm \
-e CC="/opt/llvm/clang/bin/clang" \ -e CC="/opt/llvm/clang/bin/clang" \
-e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \
-e BAZEL_LINKOPTS="-L/opt/llvm/libcxx-tsan/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx-tsan/lib" \ -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx-tsan/lib/x86_64-unknown-linux-gnu:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx-tsan/lib/x86_64-unknown-linux-gnu" \
-e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx-tsan/include/c++/v1" \ -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx-tsan/include/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx-tsan/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \ ${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \ ${DOCKER_CONTAINER} \
/usr/local/bin/bazel test ... \ /usr/local/bin/bazel test ... \

@ -16,6 +16,6 @@
# Test scripts should source this file to get the identifiers. # Test scripts should source this file to get the identifiers.
readonly LINUX_ALPINE_CONTAINER="gcr.io/google.com/absl-177019/alpine:20201026" readonly LINUX_ALPINE_CONTAINER="gcr.io/google.com/absl-177019/alpine:20201026"
readonly LINUX_CLANG_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20210617" readonly LINUX_CLANG_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20220113"
readonly LINUX_GCC_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20210617" readonly LINUX_GCC_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20220113"
readonly LINUX_GCC_FLOOR_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-floor:20210617" readonly LINUX_GCC_FLOOR_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-floor:20210617"

Loading…
Cancel
Save