File: test_int_sqrt.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 (100 lines) | stat: -rw-r--r-- 2,287 bytes parent folder | download | duplicates (11)
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
///////////////////////////////////////////////////////////////
//  Copyright 2012 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

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

using Integer = boost::multiprecision::cpp_int;

void check_sqrt(const Integer& s, const Integer& r, const Integer& v) {
   BOOST_CHECK_EQUAL(s * s + r, v);
   BOOST_CHECK_GE(r, Integer(0));
   BOOST_CHECK_LE(s * s, v);
   BOOST_CHECK_GT((s + 1) * (s + 1), v);
}

template<typename I>
void check(const I& v) {
   I r;
   I s = boost::multiprecision::sqrt(v, r);
   check_sqrt(Integer(s), Integer(r), Integer(v));
}

void check_types(const Integer& v) {
   using namespace boost::multiprecision;
   size_t bits = 0;
   if (v > 0) {
      bits = msb(v);
   }
   if (bits < 32) {
      check(static_cast<uint32_t>(v));
   }
   if (bits < 64) {
      check(static_cast<uint64_t>(v));
   }
   if (bits < 128) {
      check(uint128_t(v));
   }
   if (bits < 256) {
      check(uint256_t(v));
   }
   if (bits < 512) {
      check(uint512_t(v));
   }
   check(v);
}

void check_near(const Integer& v) {
   check_types(v);
   for (size_t j = 0; j < 8; j++) {
      check_types(v - Integer(1 << j));
      check_types(v - Integer(1 << j) - 1);
      check_types(v - Integer(1 << j) + 1);
      check_types(v + Integer(1 << j));
      check_types(v + Integer(1 << j) - 1);
      check_types(v + Integer(1 << j) + 1);
   }
}

void test_first() {
   for (size_t i = 0; i < (1 << 16); i++) {
      check_types(Integer(i));
   }
}

void test_perfect() {
   for (size_t i = 256; i < (1 << 14); i++) {
      check_near(Integer(i) * Integer(i));
   }
}

void test_powers() {
   for (size_t i = 24; i < 2048; i++) {
      check_near(Integer(i) << i);
   }
}

void test_big() {
   for (size_t bits = 128; bits <= 2048; bits *= 2) {
      Integer i = Integer(1) << bits;
      Integer s = (i >> 8);
      Integer step = (i - s) / (1 << 8);
      for (Integer j = s; j <= i; j += step) {
         check_near(j);
      }
   }
}

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

   test_first();
   test_perfect();
   test_powers();
   test_big();

   return boost::report_errors();
}