sagnikc-dremio commented on a change in pull request #7885:
URL: https://github.com/apache/arrow/pull/7885#discussion_r478803356



##########
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:
       What is your concern regarding negative numbers? As far as the unit 
tests with negative numbers are concerned, verified that the output is 
expected. Do you think more unit tests should be added?




----------------------------------------------------------------
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


Reply via email to