diff options
Diffstat (limited to 'absl/base')
-rw-r--r-- | absl/base/internal/endian_test.cc | 6 | ||||
-rw-r--r-- | absl/base/optimization.h | 34 |
2 files changed, 38 insertions, 2 deletions
diff --git a/absl/base/internal/endian_test.cc b/absl/base/internal/endian_test.cc index aa6b8496905c..678a0bf78be1 100644 --- a/absl/base/internal/endian_test.cc +++ b/absl/base/internal/endian_test.cc @@ -57,6 +57,7 @@ const uint16_t k16ValueBE{0x2301}; template<typename T> std::vector<T> GenerateAllValuesForType() { std::vector<T> result; + result.reserve(size_t{1} << (sizeof(T) * 8)); T next = std::numeric_limits<T>::min(); while (true) { result.push_back(next); @@ -68,10 +69,11 @@ std::vector<T> GenerateAllValuesForType() { } template<typename T> -std::vector<T> GenerateRandomIntegers(size_t numValuesToTest) { +std::vector<T> GenerateRandomIntegers(size_t num_values_to_test) { std::vector<T> result; + result.reserve(num_values_to_test); std::mt19937_64 rng(kRandomSeed); - for (size_t i = 0; i < numValuesToTest; ++i) { + for (size_t i = 0; i < num_values_to_test; ++i) { result.push_back(rng()); } return result; diff --git a/absl/base/optimization.h b/absl/base/optimization.h index 646523b34612..1541d7a8dc31 100644 --- a/absl/base/optimization.h +++ b/absl/base/optimization.h @@ -178,4 +178,38 @@ #define ABSL_PREDICT_TRUE(x) (x) #endif +// ABSL_INTERNAL_ASSUME(cond) +// Informs the compiler than a condition is always true and that it can assume +// it to be true for optimization purposes. The call has undefined behavior if +// the condition is false. +// In !NDEBUG mode, the condition is checked with an assert(). +// NOTE: The expression must not have side effects, as it will only be evaluated +// in some compilation modes and not others. +// +// Example: +// +// int x = ...; +// ABSL_INTERNAL_ASSUME(x >= 0); +// // The compiler can optimize the division to a simple right shift using the +// // assumption specified above. +// int y = x / 16; +// +#if !defined(NDEBUG) +#define ABSL_INTERNAL_ASSUME(cond) assert(cond) +#elif ABSL_HAVE_BUILTIN(__builtin_assume) +#define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond) +#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable) +#define ABSL_INTERNAL_ASSUME(cond) \ + do { \ + if (!(cond)) __builtin_unreachable(); \ + } while (0) +#elif defined(_MSC_VER) +#define ABSL_INTERNAL_ASSUME(cond) __assume(cond) +#else +#define ABSL_INTERNAL_ASSUME(cond) \ + do { \ + static_cast<void>(false && (cond)); \ + } while (0) +#endif + #endif // ABSL_BASE_OPTIMIZATION_H_ |