Skip to content
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
14 changes: 8 additions & 6 deletions cpp/src/gandiva/precompiled/extended_math_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,13 @@ gdv_int32 round_int32_int32(gdv_int32 number, gdv_int32 precision) {
if (precision >= 0) {
return number;
}
gdv_int32 abs_precision = -precision;
// This is to ensure that there is no overflow while calculating 10^precision, 9 is
// the smallest N for which 10^N does not fit into 32 bits, so we can safely return 0
if (abs_precision > 9) {
// the smallest N for which 10^N does not fit into 32 bits, so we can safely return 0.
// The bound is checked before negating because -INT32_MIN is still negative.
if (precision < -9) {
return 0;
}
gdv_int32 abs_precision = -precision;
gdv_int32 num_sign = (number > 0) ? 1 : -1;
gdv_int32 abs_number = number * num_sign;
gdv_int32 power_of_10 = static_cast<gdv_int32>(get_power_of_10(abs_precision));
Expand All @@ -349,12 +350,13 @@ gdv_int64 round_int64_int32(gdv_int64 number, gdv_int32 precision) {
if (precision >= 0) {
return number;
}
gdv_int32 abs_precision = -precision;
// This is to ensure that there is no overflow while calculating 10^precision, 19 is
// the smallest N for which 10^N does not fit into 64 bits, so we can safely return 0
if (abs_precision > 18) {
// the smallest N for which 10^N does not fit into 64 bits, so we can safely return 0.
// The bound is checked before negating because -INT32_MIN is still negative.
if (precision < -18) {
return 0;
}
gdv_int32 abs_precision = -precision;
gdv_int32 num_sign = (number > 0) ? 1 : -1;
gdv_int64 abs_number = number * num_sign;
gdv_int64 power_of_10 = get_power_of_10(abs_precision);
Expand Down
5 changes: 5 additions & 0 deletions cpp/src/gandiva/precompiled/extended_math_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ TEST(TestExtendedMathOps, TestRound) {
EXPECT_EQ(round_int64_int32(-23453462343, -4), -23453460000);
EXPECT_EQ(round_int64_int32(-23453462343, -5), -23453500000);
EXPECT_EQ(round_int64_int32(345353425343, -12), 0);

// The most negative precision must be rejected by the range check rather than
// negated into an out-of-range table index.
EXPECT_EQ(round_int32_int32(1234, std::numeric_limits<int32_t>::min()), 0);
EXPECT_EQ(round_int64_int32(345353425343, std::numeric_limits<int32_t>::min()), 0);
}

TEST(TestExtendedMathOps, TestTruncate) {
Expand Down
Loading