Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and fix discrepency between decimal128 and decimal128_fast #642

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -458,26 +458,28 @@ jobs:
cxxstd: "14,17,20,latest"
addrmd: "32"
os: windows-2019
- toolset: msvc-14.3
cxxstd: "14,17,20,latest"
addrmd: "32"
os: windows-2022
# B2 does not work with MSVC 17.10. Once it's updated we can re-enable these tests
# Still covered in drone
#- toolset: msvc-14.3
# cxxstd: "14,17,20,latest"
# addrmd: "32"
# os: windows-2022
- toolset: msvc-14.2
cxxstd: "14,17,20,latest"
addrmd: "64"
os: windows-2019
- toolset: msvc-14.3
cxxstd: "14,17,20,latest"
addrmd: "64"
os: windows-2022
- toolset: clang-win
cxxstd: "14,17,latest"
addrmd: "32"
os: windows-2022
- toolset: clang-win
cxxstd: "14,17,latest"
addrmd: "64"
os: windows-2022
#- toolset: msvc-14.3
# cxxstd: "14,17,20,latest"
# addrmd: "64"
# os: windows-2022
#- toolset: clang-win
# cxxstd: "14,17,latest"
# addrmd: "32"
# os: windows-2022
#- toolset: clang-win
# cxxstd: "14,17,latest"
# addrmd: "64"
# os: windows-2022
- toolset: gcc
cxxstd: "03,11,14,17,2a"
addrmd: "64"
Expand Down
4 changes: 2 additions & 2 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,8 @@ constexpr auto operator+(decimal128_fast lhs, decimal128_fast rhs) noexcept -> d
detail::normalize<decimal128>(rhs_sig, rhs_exp);

const auto result {detail::d128_add_impl<detail::decimal128_fast_components>(
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_)};
lhs_sig, lhs_exp, lhs.sign_,
rhs_sig, rhs_exp, rhs.sign_)};

return {result.sig, result.exp, result.sign};
};
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ project : requirements
;

run-fail benchmarks.cpp ;
run compare_dec128_and_fast.cpp ;
compile-fail concepts_test.cpp ;
run github_issue_426.cpp ;
run github_issue_448.cpp ;
Expand Down
296 changes: 296 additions & 0 deletions test/compare_dec128_and_fast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/decimal.hpp>
#include <random>
#include <limits>
#include <climits>
#include <iostream>
#include <iomanip>

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wfloat-equal"
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif

#include <boost/core/lightweight_test.hpp>

using namespace boost::decimal;

#if !defined(BOOST_DECIMAL_REDUCE_TEST_DEPTH)
static constexpr auto N = static_cast<std::size_t>(128U); // Number of trials
#else
static constexpr auto N = static_cast<std::size_t>(8U); // Number of trials
#endif

static std::mt19937_64 rng(42);

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4146)
#endif

#if defined(__GNUC__) && __GNUC__ >= 8
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif

void test_add()
{
std::uniform_real_distribution<double> big_vals(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {big_vals(rng)};
const auto val2 {big_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 + dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 + dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}

std::uniform_real_distribution<double> small_vals(0.0, 1.0);

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {small_vals(rng)};
const auto val2 {small_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 + dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 + dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}
}

void test_sub()
{
std::uniform_real_distribution<double> big_vals(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {big_vals(rng)};
const auto val2 {big_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 - dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 + dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}

std::uniform_real_distribution<double> small_vals(0.0, 1.0);

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {small_vals(rng)};
const auto val2 {small_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 - dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 - dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}
}

void test_mul()
{
std::uniform_real_distribution<double> big_vals(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {big_vals(rng)};
const auto val2 {big_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 * dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 * dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}

std::uniform_real_distribution<double> small_vals(0.0, 1.0);

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {small_vals(rng)};
const auto val2 {small_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 * dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 * dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}
}

void test_div()
{
std::uniform_real_distribution<double> big_vals(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {big_vals(rng)};
const auto val2 {big_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 / dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 / dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}

std::uniform_real_distribution<double> small_vals(0.0, 1.0);

for (std::size_t i {}; i < N; ++i)
{
const auto val1 {small_vals(rng)};
const auto val2 {small_vals(rng)};

const decimal128 dec128_1 {val1};
const decimal128 dec128_2 {val2};
const decimal128 dec128_res {dec128_1 / dec128_2};

const decimal128_fast dec128_fast_1 {val1};
const decimal128_fast dec128_fast_2 {val2};
const decimal128_fast dec128_fast_res {dec128_fast_1 / dec128_fast_2};

if (!BOOST_TEST_EQ(static_cast<double>(dec128_res), static_cast<double>(dec128_fast_res)))
{
std::cerr << std::setprecision(35)
<< "Val 1: " << val1
<< "\nVal 2: " << val2
<< "\nDec 1: " << dec128_1
<< "\nDec 2: " << dec128_2
<< "\nDec Res: " << dec128_res
<< "\nDecfast 1: " << dec128_fast_1
<< "\nDecfast 2: " << dec128_fast_2
<< "\nDecfast res: " << dec128_fast_res << std::endl;
}
}
}

int main()
{
#if !(defined(__i386) || defined(_M_IX86)) && !(defined(_MSVC_LANG))
test_add();
test_sub();
test_mul();
test_div();
#endif

return boost::report_errors();
}
Loading