Re-try Boost-ification draft

This commit is contained in:
Christopher Kormanyos
2021-07-26 14:23:19 +02:00
parent 6e31db76a7
commit 5d79883096
4 changed files with 355 additions and 584 deletions

View File

@@ -12,7 +12,7 @@
// g++ -O3 -Wall -march=native -std=c++11 -I/mnt/c/MyGitRepos/BoostGSoC21_multiprecision/include -I/mnt/c/boost/boost_1_76_0 test.cpp -o test_double_float.exe
// TBD: Handle interaction with Boost's wrap of libquadmath __float128.
// g++ -O3 -Wall -march=native -std=gnu++11 -I/mnt/c/MyGitRepos/BoostGSoC21_multiprecision/include -I/mnt/c/boost/boost_1_76_0 -DBOOST_MATH_USE_FLOAT128 test.cpp -o -lquadmath test_double_float.exe
// g++ -O3 -Wall -march=native -std=gnu++11 -I/mnt/c/MyGitRepos/BoostGSoC21_multiprecision/include -I/mnt/c/boost/boost_1_76_0 -DBOOST_MATH_USE_FLOAT128 test.cpp -lquadmath -o test_double_float.exe
#include <ctime>
#include <iomanip>
@@ -33,9 +33,9 @@
namespace test_arithmetic_cpp_double_float {
template <typename FloatingPointType,
typename std::enable_if<( boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), bool>::type = true>
FloatingPointType uniform_real()
template<typename FloatingPointType,
typename std::enable_if<(boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), bool>::type = true>
FloatingPointType uniform_real(const FloatingPointType&)
{
using distribution_type = boost::random::uniform_real_distribution<FloatingPointType>;
@@ -43,7 +43,7 @@ FloatingPointType uniform_real()
static std::random_device rd;
static std::mt19937 gen(rd());
static distribution_type dis(0.0, 1.0);
static distribution_type dis(FloatingPointType(0.0L), FloatingPointType(1.0L));
if((seed_scaler % 0x100000UL) == 0U)
{
@@ -55,68 +55,91 @@ FloatingPointType uniform_real()
int rand_in_range(int a, int b)
{
return a + int(float(b - a) * uniform_real<float>());
return a + int(float(b - a) * uniform_real<float>(1.0F));
}
template <typename FloatingPointType,
typename std::enable_if<( boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), bool>::type = true>
FloatingPointType uniform_rand()
template<typename FloatingPointType,
typename std::enable_if<(boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), bool>::type = true>
FloatingPointType uniform_rand(const FloatingPointType&)
{
return uniform_real<FloatingPointType>();
return uniform_real<FloatingPointType>(FloatingPointType(1.0F));
}
template <typename FloatingPointType>
boost::multiprecision::backends::cpp_double_float<typename FloatingPointType::float_type> uniform_rand()
template <typename CompositeFloatType>
CompositeFloatType uniform_rand(const CompositeFloatType&)
{
using float_type = typename FloatingPointType::float_type;
return boost::multiprecision::backends::cpp_double_float<float_type>(uniform_real<float_type>()) * boost::multiprecision::backends::cpp_double_float<float_type>(uniform_real<float_type>());
using float_type = typename CompositeFloatType::backend_type::float_type;
return CompositeFloatType(uniform_real<float_type>(float_type()))
* CompositeFloatType(uniform_real<float_type>(float_type()));
}
template <typename FloatingPointType>
typename std::enable_if<( boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), FloatingPointType>::type
log_rand()
template<typename FloatingPointType,
typename std::enable_if<(boost::multiprecision::backends::detail::is_floating_point_or_float128<FloatingPointType>::value == true), bool>::type = true>
FloatingPointType log_rand(const FloatingPointType&)
{
if (uniform_real<float>() < (1. / 100.))
if (uniform_real<float>(1.0F) < (1.0F / 100.0F))
return 0; // throw in a few zeroes
using std::ldexp;
FloatingPointType ret = ldexp(uniform_real<FloatingPointType>(), rand_in_range(std::numeric_limits<FloatingPointType>::min_exponent, std::numeric_limits<FloatingPointType>::max_exponent));
using boost::multiprecision::ldexp;
FloatingPointType ret = ldexp(uniform_real<FloatingPointType>(FloatingPointType(1.0F)), rand_in_range(std::numeric_limits<FloatingPointType>::min_exponent, std::numeric_limits<FloatingPointType>::max_exponent));
using std::fmax;
return fmax(ret, std::numeric_limits<FloatingPointType>::epsilon());
}
template <typename FloatingPointType>
boost::multiprecision::backends::cpp_double_float<typename FloatingPointType::float_type> log_rand()
template<typename CompositeFloatType,
typename std::enable_if<(boost::multiprecision::backends::detail::is_floating_point_or_float128<CompositeFloatType>::value == false), bool>::type = true>
CompositeFloatType log_rand(const CompositeFloatType&)
{
boost::multiprecision::backends::cpp_double_float<typename FloatingPointType::float_type> a(uniform_rand<boost::multiprecision::backends::cpp_double_float<typename FloatingPointType::float_type> >() + typename FloatingPointType::float_type(1));
a *= log_rand<typename FloatingPointType::float_type>();
using float_type = typename CompositeFloatType::backend_type::float_type;
CompositeFloatType
a
(
uniform_rand<CompositeFloatType>(CompositeFloatType(1.0F))
+ CompositeFloatType(float_type(1.0F))
);
a *= CompositeFloatType(log_rand<float_type>(float_type(1.0F)));
return a;
}
template <typename ConstructionType, typename FloatingPointType, typename std::enable_if<std::numeric_limits<FloatingPointType>::is_iec559>::type const* = nullptr>
ConstructionType construct_from(FloatingPointType f) {
ConstructionType construct_from(FloatingPointType f)
{
return ConstructionType(f);
}
template <typename ConstructionType, typename FloatingPointType, typename std::enable_if<!std::numeric_limits<FloatingPointType>::is_iec559>::type const* = nullptr>
ConstructionType construct_from(FloatingPointType f)
{
return ConstructionType(f.first()) + ConstructionType(f.second());
const ConstructionType ct_x(ConstructionType::canonical_value(f).first());
const ConstructionType ct_y(ConstructionType::canonical_value(f).second());
const ConstructionType ct = ct_x + ct_y;
return ct;
}
template <typename FloatingPointType>
bool test_op(char op, const unsigned count = 0x20000U)
{
using naked_double_float_type = FloatingPointType;
using control_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<std::numeric_limits<naked_double_float_type>::digits10 * 2 + 1>, boost::multiprecision::et_off>;
const control_float_type MaxError = boost::multiprecision::ldexp(control_float_type(1), -std::numeric_limits<naked_double_float_type>::digits + 1);
constexpr int d10 = std::numeric_limits<naked_double_float_type>::digits10 * 2 + 1;
using control_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<d10>, boost::multiprecision::et_off>;
using boost::multiprecision::ldexp;
const control_float_type MaxError = ldexp(control_float_type(1), -std::numeric_limits<naked_double_float_type>::digits + 1);
std::cout << "testing operator" << op << " (accuracy = " << std::numeric_limits<naked_double_float_type>::digits << " bits)...";
for (unsigned i = 0U; i < count; ++i)
{
naked_double_float_type df_a = log_rand<naked_double_float_type>();
naked_double_float_type df_b = log_rand<naked_double_float_type>();
naked_double_float_type df_a = log_rand<naked_double_float_type>(naked_double_float_type(1.0F));
naked_double_float_type df_b = log_rand<naked_double_float_type>(naked_double_float_type(1.0F));
const control_float_type ctrl_a = construct_from<control_float_type, naked_double_float_type>(df_a);
const control_float_type ctrl_b = construct_from<control_float_type, naked_double_float_type>(df_b);
@@ -163,12 +186,6 @@ bool test_op(char op, const unsigned count = 0x20000U)
std::cerr << std::setprecision(std::numeric_limits<naked_double_float_type>::digits10 + 2);
std::cerr << " [FAILED] while performing '" << std::setprecision(100000) << ctrl_a << "' " << op << " '" << ctrl_b << "', got incorrect result: " << (df_c) << std::endl;
// uncomment for more debugging information (only for cpp_double_float<> type)
//std::cerr << "(df_a = " << df_a.get_raw_str() << ", df_b = " << df_b.get_raw_str() << ")" << std::endl;
//std::cerr << "expected: " << ctrl_c << std::endl;
//std::cerr << "actual : " << ctrl_df_c << " (" << df_c.get_raw_str() << ")" << std::endl;
//std::cerr << "error : " << delta << std::endl;
return false;
}
}
@@ -201,19 +218,11 @@ int main()
{
bool result_is_ok = true;
// uncomment to check if tests themselves are correct
//result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<float>();
//result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<double>();
//result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<long double>();
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::number<boost::multiprecision::backends::cpp_double_float<float>, boost::multiprecision::et_off>>();
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::number<boost::multiprecision::backends::cpp_double_float<double>, boost::multiprecision::et_off>>();
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::number<boost::multiprecision::backends::cpp_double_float<long double>, boost::multiprecision::et_off>>();
#ifdef BOOST_MATH_USE_FLOAT128
//result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::float128>();
#endif
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::backends::cpp_double_float<float> >();
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::backends::cpp_double_float<double> >();
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::backends::cpp_double_float<long double> >();
#ifdef BOOST_MATH_USE_FLOAT128
result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::backends::cpp_double_float<boost::multiprecision::float128> >();
//result_is_ok &= test_arithmetic_cpp_double_float::test_arithmetic<boost::multiprecision::number<boost::multiprecision::backends::cpp_double_float<boost::multiprecision::float128>, boost::multiprecision::et_off>>();
#endif
return (result_is_ok ? 0 : -1);