]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_int_sqrt.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_int_sqrt.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5
6 #include <boost/multiprecision/cpp_int.hpp>
7 #include "test.hpp"
8
9 using Integer = boost::multiprecision::cpp_int;
10
11 void check_sqrt(const Integer& s, const Integer& r, const Integer& v) {
12 BOOST_CHECK_EQUAL(s * s + r, v);
13 BOOST_CHECK_GE(r, Integer(0));
14 BOOST_CHECK_LE(s * s, v);
15 BOOST_CHECK_GT((s + 1) * (s + 1), v);
16 }
17
18 template<typename I>
19 void check(const I& v) {
20 I r;
21 I s = boost::multiprecision::sqrt(v, r);
22 check_sqrt(Integer(s), Integer(r), Integer(v));
23 }
24
25 void check_types(const Integer& v) {
26 using namespace boost::multiprecision;
27 size_t bits = 0;
28 if (v > 0) {
29 bits = msb(v);
30 }
31 if (bits < 32) {
32 check(static_cast<uint32_t>(v));
33 }
34 if (bits < 64) {
35 check(static_cast<uint64_t>(v));
36 }
37 if (bits < 128) {
38 check(uint128_t(v));
39 }
40 if (bits < 256) {
41 check(uint256_t(v));
42 }
43 if (bits < 512) {
44 check(uint512_t(v));
45 }
46 check(v);
47 }
48
49 void check_near(const Integer& v) {
50 check_types(v);
51 for (size_t j = 0; j < 8; j++) {
52 check_types(v - Integer(1 << j));
53 check_types(v - Integer(1 << j) - 1);
54 check_types(v - Integer(1 << j) + 1);
55 check_types(v + Integer(1 << j));
56 check_types(v + Integer(1 << j) - 1);
57 check_types(v + Integer(1 << j) + 1);
58 }
59 }
60
61 void test_first() {
62 for (size_t i = 0; i < (1 << 16); i++) {
63 check_types(Integer(i));
64 }
65 }
66
67 void test_perfect() {
68 for (size_t i = 256; i < (1 << 14); i++) {
69 check_near(Integer(i) * Integer(i));
70 }
71 }
72
73 void test_powers() {
74 for (size_t i = 24; i < 2048; i++) {
75 check_near(Integer(i) << i);
76 }
77 }
78
79 void test_big() {
80 for (size_t bits = 128; bits <= 2048; bits *= 2) {
81 Integer i = Integer(1) << bits;
82 Integer s = (i >> 8);
83 Integer step = (i - s) / (1 << 8);
84 for (Integer j = s; j <= i; j += step) {
85 check_near(j);
86 }
87 }
88 }
89
90 int main()
91 {
92 using namespace boost::multiprecision;
93
94 test_first();
95 test_perfect();
96 test_powers();
97 test_big();
98
99 return boost::report_errors();
100 }