]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/performance/rational_bernoulli_bench.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / performance / rational_bernoulli_bench.cpp
1 // Copyright 2020 John Maddock. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying file
3 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
4
5 #include <iostream>
6 #include <benchmark/benchmark.h>
7 #include <boost/multiprecision/cpp_int.hpp>
8 #include <boost/multiprecision/gmp.hpp>
9
10 #include <gmpxx.h>
11
12 template <class Integer>
13 inline Integer factorial(unsigned n)
14 {
15 Integer result = 1;
16 for (unsigned k = 1; k <= n; ++k)
17 result *= k;
18 return result;
19 }
20
21 template <class Rational, class Integer = typename Rational::value_type>
22 inline Rational binomial(unsigned n, unsigned k)
23 {
24 return Rational(factorial<Integer>(n), factorial<Integer>(k) * factorial<Integer>(n - k));
25 }
26
27 inline mpz_class pow(mpz_class i, unsigned p)
28 {
29 mpz_class result;
30 mpz_pow_ui(result.get_mpz_t(), i.get_mpz_t(), p);
31 return result;
32 }
33
34 template <class Rational, class Integer = typename Rational::value_type>
35 Rational Bernoulli(unsigned m)
36 {
37 Rational result = 0;
38
39 for (unsigned k = 0; k <= m; ++k)
40 {
41 Rational inner = 0;
42 for (unsigned v = 0; v <= k; ++v)
43 {
44 Rational term = binomial<Rational, Integer>(k, v) * Rational(pow(Integer(v), m), k + 1);
45 if (v & 1)
46 term = -term;
47 inner += term;
48 }
49 result += inner;
50 }
51 return result;
52 }
53
54 template <class Rational, class Integer = typename Rational::value_type>
55 static void BM_bernoulli(benchmark::State& state)
56 {
57 int m = state.range(0);
58 for (auto _ : state)
59 {
60 benchmark::DoNotOptimize(Bernoulli<Rational, Integer>(m));
61 }
62 }
63
64
65 BENCHMARK_TEMPLATE(BM_bernoulli, boost::multiprecision::cpp_rational)->DenseRange(50, 200, 4);
66 BENCHMARK_TEMPLATE(BM_bernoulli, boost::multiprecision::mpq_rational)->DenseRange(50, 200, 4);
67 BENCHMARK_TEMPLATE(BM_bernoulli, boost::multiprecision::number<boost::multiprecision::rational_adaptor<boost::multiprecision::gmp_int> >)->DenseRange(50, 200, 4);
68 BENCHMARK_TEMPLATE(BM_bernoulli, mpq_class, mpz_class)->DenseRange(50, 200, 4);
69
70 BENCHMARK_MAIN();