File: test_unchecked_cpp_int.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (97 lines) | stat: -rw-r--r-- 2,888 bytes parent folder | download | duplicates (15)
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
///////////////////////////////////////////////////////////////
//  Copyright 2017 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt

//
// Check results of truncated overflow.
//

#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif

#include <boost/multiprecision/cpp_int.hpp>
#include "test.hpp"
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>

template <class T>
T generate_random(unsigned bits_wanted)
{
   static boost::random::mt19937               gen;
   typedef boost::random::mt19937::result_type random_type;

   T        max_val;
   unsigned digits;
   if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
   {
      max_val = (std::numeric_limits<T>::max)();
      digits  = std::numeric_limits<T>::digits;
   }
   else
   {
      max_val = T(1) << bits_wanted;
      digits  = bits_wanted;
   }

   unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
   while ((random_type(1) << bits_per_r_val) > (gen.max)())
      --bits_per_r_val;

   unsigned terms_needed = digits / bits_per_r_val + 1;

   T val = 0;
   for (unsigned i = 0; i < terms_needed; ++i)
   {
      val *= (gen.max)();
      val += gen();
   }
   val %= max_val;
   return val;
}

template <class Number>
void test()
{
   using namespace boost::multiprecision;
   typedef Number test_type;

   for (unsigned i = 30; i < std::numeric_limits<test_type>::digits; ++i)
   {
      for (unsigned j = std::numeric_limits<test_type>::digits - i - 1; j < std::numeric_limits<test_type>::digits; ++j)
      {
         for (unsigned k = 0; k < 10; ++k)
         {
            test_type a = static_cast<test_type>(generate_random<cpp_int>(i));
            test_type b = static_cast<test_type>(generate_random<cpp_int>(j));
            test_type c = static_cast<test_type>(cpp_int(a) * cpp_int(b));
            test_type d = a * b;
            BOOST_CHECK_EQUAL(c, d);

            if ((k == 0) && (j == 0))
            {
               for (unsigned s = 1; s < std::numeric_limits<test_type>::digits; ++s)
                  BOOST_CHECK_EQUAL(a << s, test_type(cpp_int(a) << s));
            }
         }
      }
   }
}

int main()
{
   using namespace boost::multiprecision;

   test<int512_t>();
   test<uint512_t>();

   //
   // We also need to test type with "odd" bit counts in order to ensure full code coverage:
   //
   test<number<cpp_int_backend<528, 528, signed_magnitude, unchecked, void> > >();
   test<number<cpp_int_backend<528, 528, unsigned_magnitude, unchecked, void> > >();
   test<number<cpp_int_backend<48, 48, signed_magnitude, unchecked, void> > >();
   test<number<cpp_int_backend<48, 48, unsigned_magnitude, unchecked, void> > >();
   return boost::report_errors();
}