summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--absl/base/casts.h57
-rw-r--r--absl/container/internal/compressed_tuple_test.cc10
-rw-r--r--absl/numeric/BUILD.bazel14
-rw-r--r--absl/numeric/bits_benchmark.cc73
-rw-r--r--absl/random/internal/distribution_caller.h2
-rwxr-xr-xci/linux_clang-latest_libcxx_asan_bazel.sh4
-rwxr-xr-xci/linux_clang-latest_libcxx_bazel.sh4
-rwxr-xr-xci/linux_clang-latest_libcxx_tsan_bazel.sh4
-rw-r--r--ci/linux_docker_containers.sh4
9 files changed, 137 insertions, 35 deletions
diff --git a/absl/base/casts.h b/absl/base/casts.h
index 83c69126..b16af233 100644
--- a/absl/base/casts.h
+++ b/absl/base/casts.h
@@ -105,47 +105,50 @@ constexpr To implicit_cast(typename absl::internal::identity_t<To> to) {
// bit_cast()
//
-// Performs a bitwise cast on a type without changing the underlying bit
-// representation of that type's value. The two types must be of the same size
-// and both types must be trivially copyable. As with most casts, use with
-// caution. A `bit_cast()` might be needed when you need to temporarily treat a
-// type as some other type, such as in the following cases:
-//
-// * Serialization (casting temporarily to `char *` for those purposes is
-// always allowed by the C++ standard)
-// * Managing the individual bits of a type within mathematical operations
-// that are not normally accessible through that type
-// * Casting non-pointer types to pointer types (casting the other way is
-// allowed by `reinterpret_cast()` but round-trips cannot occur the other
-// way).
-//
-// Example:
+// Creates a value of the new type `Dest` whose representation is the same as
+// that of the argument, which is of (deduced) type `Source` (a "bitwise cast";
+// every bit in the value representation of the result is equal to the
+// corresponding bit in the object representation of the source). Source and
+// destination types must be of the same size, and both types must be trivially
+// copyable.
+//
+// As with most casts, use with caution. A `bit_cast()` might be needed when you
+// need to treat a value as the value of some other type, for example, to access
+// the individual bits of an object which are not normally accessible through
+// the object's type, such as for working with the binary representation of a
+// floating point value:
//
// float f = 3.14159265358979;
// int i = bit_cast<int32_t>(f);
// // i = 0x40490fdb
//
-// Casting non-pointer types to pointer types and then dereferencing them
-// traditionally produces undefined behavior.
+// Reinterpreting and accessing a value directly as a different type (as shown
+// below) usually results in undefined behavior.
//
// Example:
//
// // WRONG
-// float f = 3.14159265358979; // WRONG
-// int i = * reinterpret_cast<int*>(&f); // WRONG
+// float f = 3.14159265358979;
+// 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
-// 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 values of "different type".
+// Using bit_cast on a pointer and then dereferencing it is no better than using
+// reinterpret_cast. You should only use bit_cast on the value itself.
//
// 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
-// issue by implementing its casts using `memcpy()`, which avoids introducing
-// this undefined behavior.
+// issue by copying the object representation to a new value, which avoids
+// 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
-// proposal p0476 due to the need for workarounds and lack of intrinsics.
+// NOTE: The requirements here are stricter than the bit_cast of standard
+// proposal P0476 due to the need for workarounds and lack of intrinsics.
// Specifically, this implementation also requires `Dest` to be
// default-constructible.
template <
diff --git a/absl/container/internal/compressed_tuple_test.cc b/absl/container/internal/compressed_tuple_test.cc
index 62a7483e..74111f97 100644
--- a/absl/container/internal/compressed_tuple_test.cc
+++ b/absl/container/internal/compressed_tuple_test.cc
@@ -403,6 +403,16 @@ TEST(CompressedTupleTest, EmptyFinalClass) {
}
#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 container_internal
ABSL_NAMESPACE_END
diff --git a/absl/numeric/BUILD.bazel b/absl/numeric/BUILD.bazel
index 1f9e0f20..eaa27dfd 100644
--- a/absl/numeric/BUILD.bazel
+++ b/absl/numeric/BUILD.bazel
@@ -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(
name = "bits_test",
size = "small",
diff --git a/absl/numeric/bits_benchmark.cc b/absl/numeric/bits_benchmark.cc
new file mode 100644
index 00000000..3de1dbfa
--- /dev/null
+++ b/absl/numeric/bits_benchmark.cc
@@ -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
diff --git a/absl/random/internal/distribution_caller.h b/absl/random/internal/distribution_caller.h
index fc81b787..f1ad5ccd 100644
--- a/absl/random/internal/distribution_caller.h
+++ b/absl/random/internal/distribution_caller.h
@@ -32,6 +32,8 @@ namespace random_internal {
// to intercept such calls.
template <typename URBG>
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
// bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
//
diff --git a/ci/linux_clang-latest_libcxx_asan_bazel.sh b/ci/linux_clang-latest_libcxx_asan_bazel.sh
index 5245933a..0605e2b3 100755
--- a/ci/linux_clang-latest_libcxx_asan_bazel.sh
+++ b/ci/linux_clang-latest_libcxx_asan_bazel.sh
@@ -70,8 +70,8 @@ for std in ${STD}; do
--rm \
-e CC="/opt/llvm/clang/bin/clang" \
-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 CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \
+ -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/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \
/usr/local/bin/bazel test ... \
diff --git a/ci/linux_clang-latest_libcxx_bazel.sh b/ci/linux_clang-latest_libcxx_bazel.sh
index e0fe653d..00517749 100755
--- a/ci/linux_clang-latest_libcxx_bazel.sh
+++ b/ci/linux_clang-latest_libcxx_bazel.sh
@@ -71,8 +71,8 @@ for std in ${STD}; do
--rm \
-e CC="/opt/llvm/clang/bin/clang" \
-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 CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \
+ -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/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \
/bin/sh -c "
diff --git a/ci/linux_clang-latest_libcxx_tsan_bazel.sh b/ci/linux_clang-latest_libcxx_tsan_bazel.sh
index 555f6b1c..da4fcd0a 100755
--- a/ci/linux_clang-latest_libcxx_tsan_bazel.sh
+++ b/ci/linux_clang-latest_libcxx_tsan_bazel.sh
@@ -70,8 +70,8 @@ for std in ${STD}; do
--rm \
-e CC="/opt/llvm/clang/bin/clang" \
-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 CPLUS_INCLUDE_PATH="/opt/llvm/libcxx-tsan/include/c++/v1" \
+ -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/x86_64-unknown-linux-gnu/c++/v1:/opt/llvm/libcxx-tsan/include/c++/v1" \
${DOCKER_EXTRA_ARGS:-} \
${DOCKER_CONTAINER} \
/usr/local/bin/bazel test ... \
diff --git a/ci/linux_docker_containers.sh b/ci/linux_docker_containers.sh
index 32865b83..37be5310 100644
--- a/ci/linux_docker_containers.sh
+++ b/ci/linux_docker_containers.sh
@@ -16,6 +16,6 @@
# Test scripts should source this file to get the identifiers.
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_GCC_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:20220113"
readonly LINUX_GCC_FLOOR_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-floor:20210617"