emkornfield commented on a change in pull request #7885: URL: https://github.com/apache/arrow/pull/7885#discussion_r472717430
########## File path: cpp/src/gandiva/precompiled/extended_math_ops.cc ########## @@ -111,6 +112,76 @@ LOG_WITH_BASE(float64, float64, float64) POWER(float64, float64, float64) +FORCE_INLINE +gdv_int32 round_int32_int32(gdv_int32 number, gdv_int32 precision) { + // for integers, there is nothing following the decimal point, + // so round() always returns the same number if precision >= 0 + if (precision >= 0 || number == 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) { + return 0; + } + gdv_int32 power_of_10 = static_cast<gdv_int32>(get_power_of_10(abs_precision)); + gdv_int32 remainder = number % power_of_10, quotient = number / power_of_10; + // if the fractional part of the quotient >= 0.5, round to next higher integer + if (remainder >= power_of_10 / 2) { + quotient++; + } + return quotient * power_of_10; +} + +FORCE_INLINE +gdv_int64 round_int64_int32(gdv_int64 number, gdv_int32 precision) { + // for long integers, there is nothing following the decimal point, + // so round() always returns the same number if precision >= 0 + if (precision >= 0 || number == 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) { + return 0; + } + gdv_int64 power_of_10 = get_power_of_10(abs_precision); + gdv_int64 remainder = number % power_of_10, quotient = number / power_of_10; Review comment: nit: two separate lines. Further nit: I think you can get rid of a multiply and a divide you make the code: ``` number -= remainder; if (remainder >= power_of_10 / 2) { number += power_of_10; } return number; ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org