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

FIX: avoid overflow on overflow check for Mac M1 #945

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion include/boost/math/special_functions/gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,8 @@ T gamma_p_derivative_imp(T a, T x, const Policy& pol)
//
typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
T f1 = detail::regularised_gamma_prefix(a, x, pol, lanczos_type());
if((x < 1) && (tools::max_value<T>() * x < f1))
static const T inv_max_value = 1.0 / tools::max_value<T>();
if((x < 1) && (x < inv_max_value * f1))
{
// overflow:
return policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", nullptr, pol);
Expand Down
11 changes: 9 additions & 2 deletions include/boost/math/tools/roots.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ namespace detail {
BOOST_MATH_INSTRUMENT_VARIABLE(denom);
BOOST_MATH_INSTRUMENT_VARIABLE(num);

if ((fabs(num) < 1) && (fabs(denom) >= fabs(num) * tools::max_value<T>()))
// denom/num overflows if:
// |denom| >= |num| * max_value
// RHS may overflow on Apple M1, so rearrange:
// |denom| * 1/max_value >= |num|
static const T inv_max_value = 1.0 / tools::max_value<T>();
if ((fabs(num) < 1) && (inv_max_value * fabs(denom) >= fabs(num)))
{
// possible overflow, use Newton step:
delta = f0 / f1;
Expand Down Expand Up @@ -647,7 +652,9 @@ namespace detail {
}
else if (result > max)
{
T diff = ((fabs(max) < 1) && (fabs(result) > 1) && (tools::max_value<T>() / fabs(result) < fabs(max))) ? T(1000) : T(result / max);
const T amax = fabs(max);
volatile const T aresult = fabs(result); // volatile: force compiler to honor data-dependency in chained bool exprs below
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should really only need const volatile in a shared memory environment. Do you still get overflow errors with just const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried all the variations I could think of. What also worked was predclaring the amax and aresult and switching around tools::max_value<T>() / fabs(result) < fabs(max) to tools::max_value<T>() / amax < aresult, but I that was only suppressing it in this case for a specific relationship between result and max, so the volatile in my mind makes it a little more generic

T diff = ((amax < 1) && (aresult > 1) && (tools::max_value<T>() / aresult < amax)) ? T(1000) : T(result / max);
if (fabs(diff) < 1)
diff = 1 / diff;
if (!out_of_bounds_sentry && (diff > 0) && (diff < 3))
Expand Down
3 changes: 2 additions & 1 deletion include/boost/math/tools/toms748_solve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ inline T safe_div(T num, T denom, T r)

if(fabs(denom) < 1)
{
if(fabs(denom * tools::max_value<T>()) <= fabs(num))
static const T inv_max_value = 1.0 / tools::max_value<T>();
if(fabs(denom) <= inv_max_value * fabs(num))
return r;
}
return num / denom;
Expand Down