edponce commented on a change in pull request #10349:
URL: https://github.com/apache/arrow/pull/10349#discussion_r705514453



##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -1276,6 +1496,65 @@ std::shared_ptr<ScalarFunction> 
MakeUnaryArithmeticFunctionNotNull(
   return func;
 }
 
+// Generate a kernel given an arithmetic rounding functor
+template <template <RoundMode> class Op>
+ArrayKernelExec GenerateArithmeticRound(RoundMode rmode, detail::GetTypeId ty) 
{
+  switch (rmode) {
+    case RoundMode::DOWN:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull, 
Op<RoundMode::DOWN>>(ty);
+    case RoundMode::UP:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull, 
Op<RoundMode::UP>>(ty);
+    case RoundMode::TOWARDS_ZERO:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             Op<RoundMode::TOWARDS_ZERO>>(ty);
+    case RoundMode::TOWARDS_INFINITY:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             
Op<RoundMode::TOWARDS_INFINITY>>(ty);
+    case RoundMode::HALF_DOWN:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             Op<RoundMode::HALF_DOWN>>(ty);
+    case RoundMode::HALF_UP:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull, 
Op<RoundMode::HALF_UP>>(
+          ty);
+    case RoundMode::HALF_TOWARDS_ZERO:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             
Op<RoundMode::HALF_TOWARDS_ZERO>>(ty);
+    case RoundMode::HALF_TOWARDS_INFINITY:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             
Op<RoundMode::HALF_TOWARDS_INFINITY>>(ty);
+    case RoundMode::HALF_TO_EVEN:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             Op<RoundMode::HALF_TO_EVEN>>(ty);
+    case RoundMode::HALF_TO_ODD:
+      return GenerateArithmeticFloatingPoint<ScalarUnaryNotNull,
+                                             Op<RoundMode::HALF_TO_ODD>>(ty);
+    default:
+      DCHECK(false);
+      return ExecFail;
+  }
+}
+
+// Like MakeUnaryArithmeticFunction, but for unary rounding functions that 
control
+// kernel dispatch based on RoundMode, only on non-null output.
+template <template <RoundMode> class Op, typename OptionsType>
+std::shared_ptr<ScalarFunction> MakeUnaryRoundFunction(std::string name,
+                                                       const FunctionDoc* doc) 
{
+  using State = RoundOptionsWrapper<OptionsType>;
+
+  static const OptionsType kDefaultOptions = OptionsType::Defaults();
+  auto func = std::make_shared<ArithmeticFloatingPointFunction>(name, 
Arity::Unary(), doc,
+                                                                
&kDefaultOptions);
+  for (const auto& ty : FloatingPointTypes()) {
+    auto exec = [&](KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+      auto options = State::Get(ctx);
+      auto exec_ = GenerateArithmeticRound<Op>(options.round_mode, ty);

Review comment:
       I agree. The purpose of doing this here is to generate dispatchers once 
prior to kernel invocation. Previously, I tried 2 solutions:
   * Have a vector of precompute `GenerateArithmeticFloatingPoint` and use the 
`options.round_mode` to index this vector. But this required vector to be 
ordered identically to the round modes in `enum RoundMode`.
   * Similar to above but using an `unordered_map` indexed by 
`options.round_mode`. This requires adding hash support for `RoundMode` data 
type. This approach does not imposes a full constraint on the ordering of 
`RoundMode`.
   
   Which one do you think is best?




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