You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/06/21 14:39:40 UTC

[GitHub] [arrow] anthonylouisbsb commented on a change in pull request #10516: ARROW-13049: [C++][Gandiva] Implement BIN Hive function on Gandiva

anthonylouisbsb commented on a change in pull request #10516:
URL: https://github.com/apache/arrow/pull/10516#discussion_r655430066



##########
File path: cpp/src/gandiva/precompiled/extended_math_ops.cc
##########
@@ -367,4 +367,31 @@ gdv_float64 get_scale_multiplier(gdv_int32 scale) {
   return power_float64_float64(10.0, scale);
 }
 
+// convert input unsigned long to its binary representation
+void bin(uint64_t n, char* ret, int32_t* position) {
+  if (n > 1) {
+    bin(n / 2, ret, position);
+  }
+  ret[*position] = (n % 2) == 0 ? '0' : '1';
+  *position += 1;
+}
+
+// returns the binary representation of a given integer (e.g. 928 -> 1110100000)
+#define BIN_INTEGER(IN_TYPE)                                                          \
+  FORCE_INLINE                                                                        \
+  const char* bin_##IN_TYPE(int64_t context, gdv_##IN_TYPE value, int32_t* out_len) { \
+    *out_len = 0;                                                                     \
+    char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, 64));    \
+    if (ret == nullptr) {                                                             \
+      gdv_fn_context_set_error_msg(context, "Could not allocate memory for output");  \
+      return "";                                                                      \
+    }                                                                                 \
+    /* generate bin representation recursively */                                     \
+    bin(value, ret, out_len);                                                         \
+    return ret;                                                                       \
+  }
+
+BIN_INTEGER(int32)
+BIN_INTEGER(int64)
+

Review comment:
       I think you can call `#undef BIN_INTEGER` since the macro you not be used in any place.

##########
File path: cpp/src/gandiva/precompiled/extended_math_ops.cc
##########
@@ -367,4 +367,31 @@ gdv_float64 get_scale_multiplier(gdv_int32 scale) {
   return power_float64_float64(10.0, scale);
 }
 
+// convert input unsigned long to its binary representation
+void bin(uint64_t n, char* ret, int32_t* position) {
+  if (n > 1) {
+    bin(n / 2, ret, position);
+  }
+  ret[*position] = (n % 2) == 0 ? '0' : '1';
+  *position += 1;
+}
+
+// returns the binary representation of a given integer (e.g. 928 -> 1110100000)
+#define BIN_INTEGER(IN_TYPE)                                                          \
+  FORCE_INLINE                                                                        \
+  const char* bin_##IN_TYPE(int64_t context, gdv_##IN_TYPE value, int32_t* out_len) { \
+    *out_len = 0;                                                                     \
+    char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, 64));    \

Review comment:
       The output string size depends on the size of the input integer, right?
   
   I think you can add an additional parameter in the macro for the output size.




-- 
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:
users@infra.apache.org