File: cpp_int_mul_timing.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (259 lines) | stat: -rw-r--r-- 7,525 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
///////////////////////////////////////////////////////////////////
//  Copyright Christopher Kormanyos 2019.                        //
//  Distributed under the Boost Software License,                //
//  Version 1.0. (See accompanying file LICENSE_1_0.txt          //
//  or copy at http://www.boost.org/LICENSE_1_0.txt)             //
///////////////////////////////////////////////////////////////////

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <limits>
#include <iterator>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

class random_pcg32_fast_base
{
protected:
  using itype = std::uint64_t;

  static constexpr bool is_mcg = false;

public:
  virtual ~random_pcg32_fast_base() = default;

protected:
  explicit random_pcg32_fast_base(const itype = itype()) { }

  random_pcg32_fast_base(const random_pcg32_fast_base&) = default;

  random_pcg32_fast_base& operator=(const random_pcg32_fast_base&) = default;

  template<typename ArithmeticType>
  static ArithmeticType rotr(const ArithmeticType& value_being_shifted,
                             const std::size_t     bits_to_shift)
  {
    const std::size_t left_shift_amount =
      std::numeric_limits<ArithmeticType>::digits - bits_to_shift;

    const ArithmeticType part1 = ((bits_to_shift > 0U) ? ArithmeticType(value_being_shifted >> bits_to_shift)     : value_being_shifted);
    const ArithmeticType part2 = ((bits_to_shift > 0U) ? ArithmeticType(value_being_shifted << left_shift_amount) : 0U);

    return ArithmeticType(part1 | part2);
  }

  template<typename xtype,
           typename itype>
  struct xsh_rr_mixin
  {
    static xtype output(const itype internal_value)
    {
      using bitcount_t = std::size_t;

      constexpr bitcount_t bits         = bitcount_t(sizeof(itype) * 8U);
      constexpr bitcount_t xtypebits    = bitcount_t(sizeof(xtype) * 8U);
      constexpr bitcount_t sparebits    = bits - xtypebits;
      constexpr bitcount_t wantedopbits =   ((xtypebits >= 128U) ? 7U
                                          : ((xtypebits >=  64U) ? 6U
                                          : ((xtypebits >=  32U) ? 5U
                                          : ((xtypebits >=  16U) ? 4U
                                          :                        3U))));

      constexpr bitcount_t opbits       = ((sparebits >= wantedopbits) ? wantedopbits : sparebits);
      constexpr bitcount_t amplifier    = wantedopbits - opbits;
      constexpr bitcount_t mask         = (1ULL << opbits) - 1U;
      constexpr bitcount_t topspare     = opbits;
      constexpr bitcount_t bottomspare  = sparebits - topspare;
      constexpr bitcount_t xshift       = (topspare + xtypebits) / 2U;

      const bitcount_t rot =
        ((opbits != 0U) ? (bitcount_t(internal_value >> (bits - opbits)) & mask)
                        : 0U);

      const bitcount_t amprot = (rot << amplifier) & mask;

      const itype internal_value_xor = internal_value ^ itype(internal_value >> xshift);

      const xtype result = rotr(xtype(internal_value_xor >> bottomspare), amprot);

      return result;
    }
  };
};

class random_pcg32_fast : public random_pcg32_fast_base
{
private:
  static constexpr itype default_multiplier = static_cast<itype>(6364136223846793005ULL);
  static constexpr itype default_increment  = static_cast<itype>(1442695040888963407ULL);

public:
  using result_type = std::uint32_t;

  static constexpr itype default_seed = static_cast<itype>(0xCAFEF00DD15EA5E5ULL);

  explicit random_pcg32_fast(const itype state = default_seed)
    : random_pcg32_fast_base(state),
      my_inc  (default_increment),
      my_state(is_mcg ? state | itype(3U) : bump(state + increment())) { }

  random_pcg32_fast(const random_pcg32_fast& other)
    : random_pcg32_fast_base(other),
      my_inc  (other.my_inc),
      my_state(other.my_state) { }

  virtual ~random_pcg32_fast() = default;

  random_pcg32_fast& operator=(const random_pcg32_fast& other)
  {
    static_cast<void>(random_pcg32_fast_base::operator=(other));

    if(this != &other)
    {
      my_inc   = other.my_inc;
      my_state = other.my_state;
    }

    return *this;
  }

  void seed(const itype state = default_seed)
  {
    my_inc = default_increment;

    my_state = (is_mcg ? state | itype(3U) : bump(state + increment()));
  }

  result_type operator()()
  {
    const result_type value =
      xsh_rr_mixin<result_type, itype>::output(base_generate0());

    return value;
  }

private:
  itype my_inc;
  itype my_state;

  itype multiplier() const
  {
    return default_multiplier;
  }

  itype increment() const
  {
    return default_increment;
  }

  itype bump(const itype state)
  {
    return itype(state * multiplier()) + increment();
  }

  itype base_generate0()
  {
    const itype old_state = my_state;

    my_state = bump(my_state);

    return old_state;
  }
};


template<typename UnsignedIntegralIteratorType,
         typename RandomEngineType>
void get_random_big_uint(RandomEngineType& rng, UnsignedIntegralIteratorType it_out)
{
  using local_random_value_type = typename RandomEngineType::result_type;

  using local_uint_type = typename std::iterator_traits<UnsignedIntegralIteratorType>::value_type;

  constexpr std::size_t digits_of_uint___type = static_cast<std::size_t>(std::numeric_limits<local_uint_type>::digits);
  constexpr std::size_t digits_of_random_type = static_cast<std::size_t>(std::numeric_limits<local_random_value_type>::digits);

  local_random_value_type next_random = rng();

  *it_out = next_random;

  for(std::size_t i = digits_of_random_type; i < digits_of_uint___type; i += digits_of_random_type)
  {
    (*it_out) <<= digits_of_random_type;

    next_random = rng();

    (*it_out) |= next_random;
  }
}

using big_uint_backend_type =
  boost::multiprecision::cpp_int_backend<8192UL << 1U,
                                         8192UL << 1U,
                                         boost::multiprecision::unsigned_magnitude>;

using big_uint_type = boost::multiprecision::number<big_uint_backend_type>;

namespace local
{
  std::vector<big_uint_type> a(1024U);
  std::vector<big_uint_type> b(a.size());
}

int main()
{
  random_pcg32_fast rng;

  rng.seed(std::clock());

  for(auto i = 0U; i < local::a.size(); ++i)
  {
    get_random_big_uint(rng, local::a.begin() + i);
    get_random_big_uint(rng, local::b.begin() + i);
  }

  std::size_t count = 0U;

  float total_time = 0.0F;

  const std::clock_t start = std::clock();

  do
  {
    const std::size_t index = count % local::a.size();

    local::a[index] * local::b[index];

    ++count;
  }
  while((total_time = (float(std::clock() - start) / float(CLOCKS_PER_SEC))) < 10.0F);

  // Boost.Multiprecision 1.71
  // bits: 16384, kops_per_sec: 4.7
  // bits: 32768, kops_per_sec: 1.2
  // bits: 65536, kops_per_sec: 0.3
  // bits: 131072, kops_per_sec: 0.075
  // bits: 262144, kops_per_sec: 0.019
  // bits: 524288, kops_per_sec: 0.0047

  // Boost.Multiprecision + kara mult
  // bits: 16384, kops_per_sec: 4.8
  // bits: 32768, kops_per_sec: 1.6
  // bits: 65536, kops_per_sec: 0.5
  // bits: 131072, kops_per_sec: 0.15
  // bits: 262144, kops_per_sec: 0.043
  // bits: 524288, kops_per_sec: 0.011

  const float kops_per_sec = (float(count) / total_time) / 1000.0F;

  std::cout << "bits: "
            << std::numeric_limits<big_uint_type>::digits
            << ", kops_per_sec: "
            << kops_per_sec
            << count << std::endl;
}