| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | POLYFLOAT - Extended precision floating points | ||
| 4 | Copyright : POLYFLOAT Contributors & Maintainers | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | #include <polyfloat/details/abi.hpp> | ||
| 10 | #include <eve/traits/overload.hpp> | ||
| 11 | #include <mpreal.h> | ||
| 12 | #include <string> | ||
| 13 | #include <iostream> | ||
| 14 | #include <iomanip> | ||
| 15 | |||
| 16 | namespace plf::_ | ||
| 17 | { | ||
| 18 | //////////////////////////////////////////////////////////////////////////////// | ||
| 19 | // printing on decimal form form using mpfr for debug purposes | ||
| 20 | //////////////////////////////////////////////////////////////////////////////// | ||
| 21 | |||
| 22 | 36 | template<typename T> T to_polyfloat(mpfr::mpreal mpa, eve::as<T>) | |
| 23 | { | ||
| 24 | using u_t = eve::underlying_type_t<T>; | ||
| 25 | using mu_t = decltype(mpa); | ||
| 26 | if constexpr (plf::dimension_v<T> == 1) | ||
| 27 | { | ||
| 28 | 12 | auto h = u_t(mpa); | |
| 29 | 12 | return h; | |
| 30 | } | ||
| 31 | else if constexpr (plf::dimension_v<T> == 2) | ||
| 32 | { | ||
| 33 | 12 | auto h = u_t(mpa); | |
| 34 |
5/5✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
|
12 | auto l = u_t(mpa - mu_t(h)); |
| 35 | 12 | return plf::as_polyfloat_n_t<2, u_t>(h, l); | |
| 36 | } | ||
| 37 | else if constexpr (plf::dimension_v<T> == 3) | ||
| 38 | { | ||
| 39 | 12 | auto h = u_t(mpa); | |
| 40 |
5/5✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
|
12 | auto m = u_t(mpa - mu_t(h)); |
| 41 | // auto [z1, e1] = eve::two_add(h, m); | ||
| 42 | // auto l = u_t(mpa - mpfr::mpreal((mu_t(z1)+mu_t(e1)))); | ||
| 43 |
9/9✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
✓ Branch 19 taken 6 times.
✓ Branch 22 taken 6 times.
✓ Branch 25 taken 6 times.
|
12 | auto l = u_t(mpa - mpfr::mpreal(mu_t(h) + mu_t(m))); |
| 44 | 12 | return plf::as_polyfloat_n_t<3, u_t>(h, m, l); | |
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | 28 | template<typename T> constexpr auto bitprec() | |
| 49 | { | ||
| 50 | using u_t = decltype(plf::hi(T())); | ||
| 51 | 8 | if constexpr (plf::dimension_v<T> == 1) return (sizeof(u_t) == 8 ? 53 : 24); | |
| 52 | 10 | if constexpr (plf::dimension_v<T> == 2) return (sizeof(u_t) == 8 ? 106 : 48); | |
| 53 | 10 | else if constexpr (plf::dimension_v<T> == 3) return (sizeof(u_t) == 8 ? 159 : 72); | |
| 54 | } | ||
| 55 | |||
| 56 | template<typename T> auto from_string(std::string const& s, eve::as<T> target) | ||
| 57 | { | ||
| 58 | using mpfr::mpreal; | ||
| 59 | mpfr::mpreal::set_default_prec(bitprec<T>() * 10); | ||
| 60 | auto a = mpreal(s); | ||
| 61 | return to_polyfloat(a, target); | ||
| 62 | } | ||
| 63 | |||
| 64 | 28 | template<typename T> auto to_mpreal(T a) | |
| 65 | { | ||
| 66 | using mpfr::mpreal; | ||
| 67 | 28 | mpfr::mpreal::set_default_prec(bitprec<T>() * 10); | |
| 68 | 8 | if constexpr (plf::dimension_v<T> == 1) return mpreal(a); | |
| 69 |
7/7✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 8 taken 5 times.
✓ Branch 11 taken 5 times.
✓ Branch 14 taken 5 times.
✓ Branch 18 taken 5 times.
✓ Branch 21 taken 5 times.
|
10 | else if constexpr (plf::dimension_v<T> == 2) return mpreal(plf::hi(a)) + mpreal(plf::lo(a)); |
| 70 |
11/11✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 8 taken 5 times.
✓ Branch 11 taken 5 times.
✓ Branch 14 taken 5 times.
✓ Branch 18 taken 5 times.
✓ Branch 21 taken 5 times.
✓ Branch 24 taken 5 times.
✓ Branch 28 taken 5 times.
✓ Branch 31 taken 5 times.
✓ Branch 34 taken 5 times.
|
10 | else if constexpr (plf::dimension_v<T> == 3) return mpreal(plf::hi(a)) + mpreal(plf::md(a)) + mpreal(plf::lo(a)); |
| 71 | } | ||
| 72 | |||
| 73 | template<typename T> void print(std::string s, T a) | ||
| 74 | { | ||
| 75 | auto prec = 2 * int(std::log10(std::exp2(bitprec<T>()))) + 1; | ||
| 76 | auto oldprec = std::cout.precision(); | ||
| 77 | std::cout << s; | ||
| 78 | std::cout << std::setprecision(prec) << to_mpreal(a) << std::setprecision(oldprec); | ||
| 79 | } | ||
| 80 | |||
| 81 | 4 | template<typename T> void println(std::string s, T a) | |
| 82 | { | ||
| 83 | 4 | auto prec = 2 * int(std::log10(std::exp2(bitprec<T>()))) + 1; | |
| 84 | 4 | auto oldprec = std::cout.precision(); | |
| 85 | 4 | std::cout << s; | |
| 86 |
3/3✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 11 taken 2 times.
|
4 | std::cout << std::setprecision(prec) << to_mpreal(a) << std::setprecision(oldprec) << std::endl; |
| 87 | 4 | } | |
| 88 | } | ||
| 89 |