Fix usage common::addOverflow for floats in AggregateFunctionSparkbar

This commit is contained in:
vdimir 2023-03-31 13:35:07 +00:00
parent 2d18689af6
commit 2a179aed49
No known key found for this signature in database
GPG Key ID: 6EE4CE2BEDC51862

View File

@ -45,11 +45,19 @@ struct AggregateFunctionSparkbarData
auto [it, inserted] = points.insert({x, y});
if (!inserted)
{
if constexpr (std::is_floating_point_v<Y>)
{
it->getMapped() += y;
return it->getMapped();
}
else
{
Y res;
bool has_overfllow = common::addOverflow(it->getMapped(), y, res)
bool has_overfllow = common::addOverflow(it->getMapped(), y, res);
it->getMapped() = has_overfllow ? std::numeric_limits<Y>::max() : res;
}
}
return it->getMapped();
}
@ -184,7 +192,12 @@ private:
size_t index = std::min<size_t>(static_cast<size_t>(w / delta * value), histogram.size() - 1);
Y res;
bool has_overfllow = common::addOverflow(histogram[index], point.getMapped(), res);
bool has_overfllow = false;
if constexpr (std::is_floating_point_v<Y>)
res = histogram[index] + point.getMapped();
else
has_overfllow = common::addOverflow(histogram[index], point.getMapped(), res);
if (unlikely(has_overfllow))
{
/// In case of overflow, just saturate
@ -229,14 +242,21 @@ private:
}
constexpr auto levels_num = static_cast<Y>(BAR_LEVELS - 1);
/// handle potential overflow
if constexpr (std::is_floating_point_v<Y>)
{
y = y / (y_max / levels_num) + 1;
}
else
{
Y scaled;
bool has_overfllow = common::mulOverflow(y, levels_num, scaled);
bool has_overfllow = common::mulOverflow<Y>(y, levels_num, scaled);
if (has_overfllow)
y = y / (y_max / levels_num) + 1;
else
y = scaled / y_max + 1;
}
}
size_t sz = 0;
for (const auto & y : histogram)