Skip to content

Commit 17c0457

Browse files
committed
GH-45083: [C++] Add HalfFloat kernels for is_nan, is_inf, is_finite, negate, negate_checked, sign
Also improve the kernel generation infrastructure so as to make further half-float kernels easier to implement.
1 parent 19fb7bb commit 17c0457

13 files changed

Lines changed: 472 additions & 215 deletions

‎cpp/src/arrow/compute/kernels/base_arithmetic_internal.h‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "arrow/type.h"
2525
#include "arrow/type_traits.h"
2626
#include "arrow/util/decimal.h"
27+
#include "arrow/util/float16.h"
2728
#include "arrow/util/int_util_overflow.h"
29+
#include "arrow/util/logging.h"
2830
#include "arrow/util/macros.h"
2931

3032
namespace arrow {
@@ -34,6 +36,7 @@ using internal::DivideWithOverflow;
3436
using internal::MultiplyWithOverflow;
3537
using internal::NegateWithOverflow;
3638
using internal::SubtractWithOverflow;
39+
using util::Float16;
3740

3841
namespace compute {
3942
namespace internal {
@@ -470,6 +473,11 @@ struct Negate {
470473
return -arg;
471474
}
472475

476+
template <typename T, typename Arg>
477+
static constexpr enable_if_half_float_value<T> Call(KernelContext*, Arg arg, Status*) {
478+
return -arg;
479+
}
480+
473481
template <typename T, typename Arg>
474482
static constexpr enable_if_unsigned_integer_value<T> Call(KernelContext*, Arg arg,
475483
Status*) {
@@ -517,6 +525,12 @@ struct NegateChecked {
517525
return -arg;
518526
}
519527

528+
template <typename T, typename Arg>
529+
static constexpr enable_if_half_float_value<T> Call(KernelContext*, Arg arg, Status*) {
530+
static_assert(std::is_same<T, Arg>::value, "");
531+
return -arg;
532+
}
533+
520534
template <typename T, typename Arg>
521535
static constexpr enable_if_decimal_value<Arg, T> Call(KernelContext*, Arg arg,
522536
Status*) {
@@ -633,6 +647,15 @@ struct Sign {
633647
return std::isnan(arg) ? arg : ((arg == 0) ? 0 : (std::signbit(arg) ? -1 : 1));
634648
}
635649

650+
template <typename T, typename Arg>
651+
static constexpr enable_if_half_float_value<Arg, T> Call(KernelContext*, Arg arg,
652+
Status*) {
653+
return arg.is_nan()
654+
? arg
655+
: (arg.is_zero() ? Float16::zero()
656+
: (arg.signbit() ? -Float16::one() : Float16::one()));
657+
}
658+
636659
template <typename T, typename Arg>
637660
static constexpr enable_if_unsigned_integer_value<Arg, T> Call(KernelContext*, Arg arg,
638661
Status*) {
@@ -705,7 +728,13 @@ struct Identity;
705728
template <>
706729
struct Identity<Add> {
707730
template <typename Value>
708-
static constexpr Value value{0};
731+
static constexpr Value value() {
732+
if constexpr (std::is_same_v<Float16, Value>) {
733+
return Float16::zero();
734+
} else {
735+
return 0;
736+
}
737+
}
709738
};
710739

711740
template <>
@@ -714,7 +743,13 @@ struct Identity<AddChecked> : Identity<Add> {};
714743
template <>
715744
struct Identity<Multiply> {
716745
template <typename Value>
717-
static constexpr Value value{1};
746+
static constexpr Value value() {
747+
if constexpr (std::is_same_v<Float16, Value>) {
748+
return Float16::one();
749+
} else {
750+
return 1;
751+
}
752+
}
718753
};
719754

720755
template <>
@@ -723,13 +758,17 @@ struct Identity<MultiplyChecked> : Identity<Multiply> {};
723758
template <>
724759
struct Identity<Max> {
725760
template <typename Value>
726-
static constexpr Value value{std::numeric_limits<Value>::min()};
761+
static constexpr Value value() {
762+
return std::numeric_limits<Value>::min();
763+
}
727764
};
728765

729766
template <>
730767
struct Identity<Min> {
731768
template <typename Value>
732-
static constexpr Value value{std::numeric_limits<Value>::max()};
769+
static constexpr Value value() {
770+
return std::numeric_limits<Value>::max();
771+
}
733772
};
734773

735774
} // namespace internal

‎cpp/src/arrow/compute/kernels/codegen_internal.h‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "arrow/util/bitmap_writer.h"
4545
#include "arrow/util/checked_cast.h"
4646
#include "arrow/util/decimal.h"
47+
#include "arrow/util/float16.h"
4748
#include "arrow/util/logging.h"
4849
#include "arrow/util/macros.h"
4950
#include "arrow/util/visibility.h"
@@ -60,6 +61,7 @@ using internal::GenerateBitsUnrolled;
6061
using internal::VisitBitBlocks;
6162
using internal::VisitBitBlocksVoid;
6263
using internal::VisitTwoBitBlocksVoid;
64+
using util::Float16;
6365

6466
namespace compute {
6567
namespace internal {
@@ -131,6 +133,14 @@ struct GetViewType<Type, enable_if_has_c_type<Type>> {
131133
static T LogicalValue(PhysicalType value) { return value; }
132134
};
133135

136+
template <>
137+
struct GetViewType<HalfFloatType> {
138+
using T = Float16;
139+
using PhysicalType = uint16_t;
140+
141+
static T LogicalValue(PhysicalType value) { return T::FromBits(value); }
142+
};
143+
134144
template <typename Type>
135145
struct GetViewType<Type, enable_if_t<is_base_binary_type<Type>::value ||
136146
is_fixed_size_binary_type<Type>::value ||
@@ -197,6 +207,11 @@ struct GetOutputType<Type, enable_if_has_c_type<Type>> {
197207
using T = typename Type::c_type;
198208
};
199209

210+
template <>
211+
struct GetOutputType<HalfFloatType> {
212+
using T = Float16;
213+
};
214+
200215
template <typename Type>
201216
struct GetOutputType<Type, enable_if_t<is_string_like_type<Type>::value>> {
202217
using T = std::string;
@@ -257,6 +272,9 @@ using enable_if_floating_value = enable_if_t<std::is_floating_point<T>::value, R
257272
template <typename T, typename R = T>
258273
using enable_if_not_floating_value = enable_if_t<!std::is_floating_point<T>::value, R>;
259274

275+
template <typename T, typename R = T>
276+
using enable_if_half_float_value = enable_if_t<std::is_same_v<T, Float16>, R>;
277+
260278
template <typename T, typename R = T>
261279
using enable_if_decimal_value =
262280
enable_if_t<std::is_same<Decimal32, T>::value || std::is_same<Decimal64, T>::value ||
@@ -285,6 +303,15 @@ struct ArrayIterator<Type, enable_if_c_number_or_decimal<Type>> {
285303
T operator()() { return *values++; }
286304
};
287305

306+
template <>
307+
struct ArrayIterator<HalfFloatType> {
308+
using T = Float16;
309+
const T* values;
310+
311+
explicit ArrayIterator(const ArraySpan& arr) : values(arr.GetValues<T>(1)) {}
312+
T operator()() { return *values++; }
313+
};
314+
288315
template <typename Type>
289316
struct ArrayIterator<Type, enable_if_boolean<Type>> {
290317
BitmapReader reader;
@@ -381,6 +408,14 @@ struct UnboxScalar<Type, enable_if_has_c_type<Type>> {
381408
}
382409
};
383410

411+
template <>
412+
struct UnboxScalar<HalfFloatType> {
413+
using T = Float16;
414+
static T Unbox(const Scalar& val) {
415+
return T(checked_cast<const HalfFloatScalar&>(val).value);
416+
}
417+
};
418+
384419
template <typename Type>
385420
struct UnboxScalar<Type, enable_if_has_string_view<Type>> {
386421
using T = std::string_view;
@@ -559,7 +594,8 @@ struct OutputAdapter<Type, enable_if_boolean<Type>> {
559594

560595
template <typename Type>
561596
struct OutputAdapter<Type, enable_if_c_number_or_decimal<Type>> {
562-
using T = typename TypeTraits<Type>::ScalarType::ValueType;
597+
using T = std::conditional_t<std::is_same_v<Type, HalfFloatType>, Float16,
598+
typename TypeTraits<Type>::ScalarType::ValueType>;
563599

564600
template <typename Generator>
565601
static Status Write(KernelContext*, ArraySpan* out, Generator&& generator) {

‎cpp/src/arrow/compute/kernels/scalar_arithmetic.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ void AddDecimalBinaryKernels(const std::string& name, ScalarFunction* func) {
687687
DCHECK_OK(func->AddKernel({in_type256, in_type256}, out_type, exec256));
688688
}
689689

690+
template <typename Op>
691+
void AddHalfFloatUnaryKernel(ScalarFunction* func) {
692+
OutputType out_type(FirstType);
693+
auto in_type = InputType(Type::HALF_FLOAT);
694+
auto exec = ScalarUnaryNotNull<HalfFloatType, HalfFloatType, Op>::Exec;
695+
DCHECK_OK(func->AddKernel({in_type}, out_type, exec));
696+
}
697+
690698
// Generate a kernel given an arithmetic functor
691699
template <template <typename...> class KernelGenerator, typename OutType, typename Op>
692700
ArrayKernelExec GenerateArithmeticWithFixedIntOutType(detail::GetTypeId get_id) {
@@ -1695,6 +1703,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
16951703
// ----------------------------------------------------------------------
16961704
auto negate = MakeUnaryArithmeticFunction<Negate>("negate", negate_doc);
16971705
AddDecimalUnaryKernels<Negate>(negate.get());
1706+
AddHalfFloatUnaryKernel<Negate>(negate.get());
16981707

16991708
// Add neg(duration) -> duration
17001709
for (auto unit : TimeUnit::values()) {
@@ -1708,6 +1717,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
17081717
auto negate_checked = MakeUnarySignedArithmeticFunctionNotNull<NegateChecked>(
17091718
"negate_checked", negate_checked_doc);
17101719
AddDecimalUnaryKernels<NegateChecked>(negate_checked.get());
1720+
AddHalfFloatUnaryKernel<NegateChecked>(negate_checked.get());
17111721

17121722
// Add neg_checked(duration) -> duration
17131723
for (auto unit : TimeUnit::values()) {
@@ -1755,6 +1765,10 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
17551765
auto exec = ScalarUnary<Int8Type, Int64Type, Sign>::Exec;
17561766
DCHECK_OK(sign->AddKernel({duration(unit)}, int8(), std::move(exec)));
17571767
}
1768+
{
1769+
auto exec = ScalarUnary<HalfFloatType, HalfFloatType, Sign>::Exec;
1770+
DCHECK_OK(sign->AddKernel({InputType(Type::HALF_FLOAT)}, float16(), std::move(exec)));
1771+
}
17581772
DCHECK_OK(registry->AddFunction(std::move(sign)));
17591773

17601774
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)