ViniciusSouzaRoque commented on a change in pull request #12306:
URL: https://github.com/apache/arrow/pull/12306#discussion_r797650455



##########
File path: cpp/src/gandiva/gdv_function_stubs.cc
##########
@@ -683,6 +683,202 @@ const char* gdv_fn_upper_utf8(int64_t context, const 
char* data, int32_t data_le
   return out;
 }
 
+GDV_FORCE_INLINE
+uint64_t unsigned_long_div(gdv_int64 x, gdv_int32 m) {
+  if (x >= 0) {
+    return x / m;
+  }
+  return x / m + 2 * (LONG_MAX / m) + 2 / m + (x % m + 2 * (LONG_MAX % m) + 2 
% m) / m;
+}
+
+GDV_FORCE_INLINE
+gdv_int64 encode(gdv_int32 radix, gdv_int32 fromPos, const char* value,
+                 gdv_int32 valueLen) {
+  uint64_t val = 0;
+  uint64_t bound = unsigned_long_div(-1 - radix, radix);
+
+  for (int i = fromPos; i < valueLen && value[i] >= 0; i++) {
+    if (val >= bound) {
+      if (unsigned_long_div(-1 - value[i], radix) < val) {
+        return -1;
+      }
+    }
+    val = val * radix + value[i];
+  }
+  return val;
+}
+
+GDV_FORCE_INLINE
+void decode(gdv_int64 val, gdv_int32 radix, char* value, gdv_int32 valueLen) {
+  for (int i = 0; i < valueLen; i++) {
+    value[i] = static_cast<char>(0);
+  }
+
+  for (int i = valueLen - 1; val != 0; i--) {
+    gdv_int64 q = unsigned_long_div(val, radix);
+    value[i] = static_cast<char>((val - q * radix));
+    val = q;
+  }
+}
+
+GDV_FORCE_INLINE
+char character_for_digit(gdv_int32 value, gdv_int32 radix) {  // From Decimal 
to Any Base

Review comment:
       Done

##########
File path: cpp/src/gandiva/gdv_function_stubs.cc
##########
@@ -683,6 +683,202 @@ const char* gdv_fn_upper_utf8(int64_t context, const 
char* data, int32_t data_le
   return out;
 }
 
+GDV_FORCE_INLINE
+uint64_t unsigned_long_div(gdv_int64 x, gdv_int32 m) {
+  if (x >= 0) {
+    return x / m;
+  }
+  return x / m + 2 * (LONG_MAX / m) + 2 / m + (x % m + 2 * (LONG_MAX % m) + 2 
% m) / m;
+}
+
+GDV_FORCE_INLINE
+gdv_int64 encode(gdv_int32 radix, gdv_int32 fromPos, const char* value,
+                 gdv_int32 valueLen) {
+  uint64_t val = 0;
+  uint64_t bound = unsigned_long_div(-1 - radix, radix);
+
+  for (int i = fromPos; i < valueLen && value[i] >= 0; i++) {
+    if (val >= bound) {
+      if (unsigned_long_div(-1 - value[i], radix) < val) {
+        return -1;
+      }
+    }
+    val = val * radix + value[i];
+  }
+  return val;
+}
+
+GDV_FORCE_INLINE
+void decode(gdv_int64 val, gdv_int32 radix, char* value, gdv_int32 valueLen) {
+  for (int i = 0; i < valueLen; i++) {
+    value[i] = static_cast<char>(0);
+  }
+
+  for (int i = valueLen - 1; val != 0; i--) {
+    gdv_int64 q = unsigned_long_div(val, radix);
+    value[i] = static_cast<char>((val - q * radix));
+    val = q;
+  }
+}
+
+GDV_FORCE_INLINE
+char character_for_digit(gdv_int32 value, gdv_int32 radix) {  // From Decimal 
to Any Base
+  // This function is similar to Character.forDigit in Java
+  int digit = 0;
+  digit = value % radix;
+  if (digit < 10) {
+    return static_cast<char>(digit + '0');
+  } else {
+    return static_cast<char>(digit + 'A' - 10);
+  }
+}
+
+GDV_FORCE_INLINE
+gdv_int64 character_digit(char value, gdv_int32 radix) {  // From any base to 
Decimal

Review comment:
       Done




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to