From ec0d76f1d012cc1a4b3b08dfafcfc5237f5ba2c9 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 22 Nov 2021 08:22:28 -0800 Subject: Export of internal Abseil changes -- a0847bf19789c97689f1a8b0133a53b8af5e5caa by Samuel Benzaquen : Add support for absl::(u)int128 to random distributions and generators. PiperOrigin-RevId: 411565479 GitOrigin-RevId: a0847bf19789c97689f1a8b0133a53b8af5e5caa Change-Id: Ide434bdd93fcab8e90f791796498de14833b667c --- absl/random/log_uniform_int_distribution.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'absl/random/log_uniform_int_distribution.h') diff --git a/absl/random/log_uniform_int_distribution.h b/absl/random/log_uniform_int_distribution.h index 43e10116..4afff8f6 100644 --- a/absl/random/log_uniform_int_distribution.h +++ b/absl/random/log_uniform_int_distribution.h @@ -69,10 +69,8 @@ class log_uniform_int_distribution { if (base_ == 2) { // Determine where the first set bit is on range(), giving a log2(range) // value which can be used to construct bounds. - log_range_ = - (std::min)(bit_width(range()), - static_cast( - std::numeric_limits::digits)); + log_range_ = (std::min)(random_internal::BitWidth(range()), + std::numeric_limits::digits); } else { // NOTE: Computing the logN(x) introduces error from 2 sources: // 1. Conversion of int to double loses precision for values >= @@ -83,7 +81,7 @@ class log_uniform_int_distribution { // // Thus a result which should equal K may equal K +/- epsilon, // which can eliminate some values depending on where the bounds fall. - const double inv_log_base = 1.0 / std::log(base_); + const double inv_log_base = 1.0 / std::log(static_cast(base_)); const double log_range = std::log(static_cast(range()) + 0.5); log_range_ = static_cast(std::ceil(inv_log_base * log_range)); } @@ -113,7 +111,7 @@ class log_uniform_int_distribution { unsigned_type range_; // max - min int log_range_; // ceil(logN(range_)) - static_assert(std::is_integral::value, + static_assert(random_internal::IsIntegral::value, "Class-template absl::log_uniform_int_distribution<> must be " "parameterized using an integral type."); }; @@ -139,7 +137,7 @@ class log_uniform_int_distribution { template result_type operator()(URBG& g, // NOLINT(runtime/references) const param_type& p) { - return (p.min)() + Generate(g, p); + return static_cast((p.min)() + Generate(g, p)); } result_type(min)() const { return (param_.min)(); } @@ -193,8 +191,8 @@ log_uniform_int_distribution::Generate( ? (std::numeric_limits::max)() : (static_cast(1) << e) - 1; } else { - const double r = std::pow(p.base(), d); - const double s = (r * p.base()) - 1.0; + const double r = std::pow(static_cast(p.base()), d); + const double s = (r * static_cast(p.base())) - 1.0; base_e = (r > static_cast((std::numeric_limits::max)())) @@ -211,7 +209,8 @@ log_uniform_int_distribution::Generate( const unsigned_type hi = (top_e >= p.range()) ? p.range() : top_e; // choose uniformly over [lo, hi] - return absl::uniform_int_distribution(lo, hi)(g); + return absl::uniform_int_distribution( + static_cast(lo), static_cast(hi))(g); } template -- cgit v1.2.3