abseil-cpp/absl/random/internal/fastmath.h
Go to the documentation of this file.
1 // Copyright 2017 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_RANDOM_INTERNAL_FASTMATH_H_
16 #define ABSL_RANDOM_INTERNAL_FASTMATH_H_
17 
18 // This file contains fast math functions (bitwise ops as well as some others)
19 // which are implementation details of various absl random number distributions.
20 
21 #include <cassert>
22 #include <cmath>
23 #include <cstdint>
24 
25 #include "absl/numeric/bits.h"
26 
27 namespace absl {
29 namespace random_internal {
30 
31 // Compute log2(n) using integer operations.
32 // While std::log2 is more accurate than std::log(n) / std::log(2), for
33 // very large numbers--those close to std::numeric_limits<uint64_t>::max() - 2,
34 // for instance--std::log2 rounds up rather than down, which introduces
35 // definite skew in the results.
36 inline int IntLog2Floor(uint64_t n) {
37  return (n <= 1) ? 0 : (63 - countl_zero(n));
38 }
39 inline int IntLog2Ceil(uint64_t n) {
40  return (n <= 1) ? 0 : (64 - countl_zero(n - 1));
41 }
42 
43 inline double StirlingLogFactorial(double n) {
44  assert(n >= 1);
45  // Using Stirling's approximation.
46  constexpr double kLog2PI = 1.83787706640934548356;
47  const double logn = std::log(n);
48  const double ninv = 1.0 / static_cast<double>(n);
49  return n * logn - n + 0.5 * (kLog2PI + logn) + (1.0 / 12.0) * ninv -
50  (1.0 / 360.0) * ninv * ninv * ninv;
51 }
52 
53 } // namespace random_internal
55 } // namespace absl
56 
57 #endif // ABSL_RANDOM_INTERNAL_FASTMATH_H_
absl::random_internal::IntLog2Ceil
int IntLog2Ceil(uint64_t n)
Definition: abseil-cpp/absl/random/internal/fastmath.h:39
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::random_internal::IntLog2Floor
int IntLog2Floor(uint64_t n)
Definition: abseil-cpp/absl/random/internal/fastmath.h:36
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::countl_zero
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, int >::type countl_zero(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:77
absl::random_internal::StirlingLogFactorial
double StirlingLogFactorial(double n)
Definition: abseil-cpp/absl/random/internal/fastmath.h:43
log
bool log
Definition: abseil-cpp/absl/synchronization/mutex.cc:310
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22