66#include < stdint.h>
77#include < type_traits>
88
9- // Arrow decimal schemas fix the physical values buffer width:
10- // - Decimal32 : 4 bytes per value.
11- // - Decimal64: 8 bytes per value .
12- // - Decimal128: 16 bytes per value.
13- // - Decimal256: 32 bytes per value .
9+ // Widen decimal values buffers to a wider physical width. Two callers use these kernels :
10+ // - The Arrow export casts to the width that the Arrow schema fixes : 4 bytes for Decimal32,
11+ // 8 bytes for Decimal64, 16 bytes for Decimal128, and 32 bytes for Decimal256 .
12+ // - The Widen executor casts a narrow decimal child to the canonical width of its decimal
13+ // dtype, from 2 to 32 bytes .
1414//
15- // Vortex storage width can differ, so export casts to the schema-implied width.
16- // Rust-side export rejects narrowing casts because detecting overflow on-device
17- // would require synchronizing an overflow flag back to the host .
15+ // Only widening pairs are instantiated. A narrowing cast can overflow, and detecting that on the
16+ // device would require synchronizing an overflow flag back to the host, so the Rust side rejects
17+ // narrowing before it loads a kernel .
1818
19- // Low 64-bit conversion for Decimal32/64 outputs.
20- template <typename Input>
21- __device__ __forceinline__ int64_t decimal_to_i64 (Input value) {
22- if constexpr (std::is_same_v<Input, int128_t >) {
23- return value.lo ;
24- } else if constexpr (std::is_same_v<Input, int256_t >) {
25- return value.parts [0 ];
26- } else {
27- return static_cast <int64_t >(value);
28- }
29- }
30-
31- // 128-bit conversion for Decimal128 outputs.
32- template <typename Input>
33- __device__ __forceinline__ int128_t decimal_to_i128 (Input value) {
34- if constexpr (std::is_same_v<Input, int128_t >) {
35- return value;
36- } else if constexpr (std::is_same_v<Input, int256_t >) {
37- return int128_t {value.parts [0 ], value.parts [1 ]};
38- } else {
39- const int64_t lo = static_cast <int64_t >(value);
40- const int64_t hi = value < 0 ? -1 : 0 ;
41- return int128_t {lo, hi};
42- }
43- }
44-
45- // Convert one value to the Arrow schema's physical width.
19+ // Sign-extend one value to the wider output width in a single step, with no intermediate width.
20+ // Native outputs use one `static_cast`. The 128-bit and 256-bit outputs keep the input's words
21+ // and fill the upper words with its sign.
4622template <typename Output, typename Input>
4723__device__ __forceinline__ Output decimal_cast_value (Input value) {
48- if constexpr (std::is_same_v<Output, int32_t >) {
49- return static_cast <int32_t >(decimal_to_i64 (value));
50- } else if constexpr (std::is_same_v<Output, int64_t >) {
51- return decimal_to_i64 (value);
52- } else if constexpr (std::is_same_v<Output, int128_t >) {
53- return decimal_to_i128 (value);
24+ static_assert (sizeof (Input) < sizeof (Output), " decimal casts on the GPU only widen" );
25+
26+ if constexpr (std::is_same_v<Output, int128_t >) {
27+ static_assert (std::is_integral_v<Input> && std::is_signed_v<Input>);
28+ const int64_t lo = static_cast <int64_t >(value);
29+ return int128_t {lo, lo < 0 ? -1 : 0 };
30+ } else if constexpr (std::is_same_v<Output, int256_t > && std::is_same_v<Input, int128_t >) {
31+ const int64_t sign = value.hi < 0 ? -1 : 0 ;
32+ return int256_t {{value.lo , value.hi , sign, sign}};
33+ } else if constexpr (std::is_same_v<Output, int256_t >) {
34+ static_assert (std::is_integral_v<Input> && std::is_signed_v<Input>);
35+ const int64_t lo = static_cast <int64_t >(value);
36+ const int64_t sign = lo < 0 ? -1 : 0 ;
37+ return int256_t {{lo, sign, sign, sign}};
5438 } else {
55- static_assert (std::is_same_v<Output, int256_t >);
56- if constexpr (std::is_same_v<Input, int256_t >) {
57- return value;
58- } else {
59- const int128_t value128 = decimal_to_i128 (value);
60- const int64_t sign = value128.hi < 0 ? -1 : 0 ;
61- return int256_t {{value128.lo , value128.hi , sign, sign}};
62- }
39+ static_assert (std::is_integral_v<Input> && std::is_signed_v<Input>);
40+ static_assert (std::is_integral_v<Output> && std::is_signed_v<Output>);
41+ return static_cast <Output>(value);
6342 }
6443}
6544
66- // Cast a contiguous values buffer to the Arrow schema's physical width.
45+ // Cast a contiguous values buffer to the output physical width.
6746template <typename Input, typename Output>
6847__device__ void
6948decimal_cast_device (const Input *__restrict input, Output *__restrict output, uint64_t array_len) {
@@ -76,28 +55,26 @@ decimal_cast_device(const Input *__restrict input, Output *__restrict output, ui
7655 }
7756}
7857
79- // Generate Decimal32/64/128/256 cast kernels for one input storage type.
80- #define GENERATE_DECIMAL_CAST_KERNELS (input_suffix, InputType ) \
81- extern " C" __global__ void decimal_cast_##input_suffix##_i32(const InputType *__restrict input, \
82- int32_t *__restrict output, \
83- uint64_t array_len) { \
84- decimal_cast_device (input, output, array_len); \
85- } \
86- extern " C" __global__ void decimal_cast_##input_suffix##_i64(const InputType *__restrict input, \
87- int64_t *__restrict output, \
88- uint64_t array_len) { \
89- decimal_cast_device (input, output, array_len); \
90- } \
91- extern " C" __global__ void decimal_cast_##input_suffix##_i128(const InputType *__restrict input, \
92- int128_t *__restrict output, \
93- uint64_t array_len) { \
94- decimal_cast_device (input, output, array_len); \
95- } \
96- extern " C" __global__ void decimal_cast_##input_suffix##_i256(const InputType *__restrict input, \
97- int256_t *__restrict output, \
98- uint64_t array_len) { \
58+ #define GENERATE_DECIMAL_CAST (input_suffix, InputType, output_suffix, OutputType ) \
59+ extern " C" __global__ void decimal_cast_##input_suffix##_##output_suffix( \
60+ const InputType *__restrict input, \
61+ OutputType *__restrict output, \
62+ uint64_t array_len) { \
9963 decimal_cast_device (input, output, array_len); \
10064 }
10165
102- FOR_EACH_SIGNED_INT (GENERATE_DECIMAL_CAST_KERNELS )
103- FOR_EACH_LARGE_DECIMAL (GENERATE_DECIMAL_CAST_KERNELS )
66+ GENERATE_DECIMAL_CAST (i8 , int8_t , i16 , int16_t )
67+ GENERATE_DECIMAL_CAST (i8 , int8_t , i32 , int32_t )
68+ GENERATE_DECIMAL_CAST (i8 , int8_t , i64 , int64_t )
69+ GENERATE_DECIMAL_CAST (i8 , int8_t , i128 , int128_t )
70+ GENERATE_DECIMAL_CAST (i8 , int8_t , i256, int256_t )
71+ GENERATE_DECIMAL_CAST (i16 , int16_t , i32 , int32_t )
72+ GENERATE_DECIMAL_CAST (i16 , int16_t , i64 , int64_t )
73+ GENERATE_DECIMAL_CAST (i16 , int16_t , i128 , int128_t )
74+ GENERATE_DECIMAL_CAST (i16 , int16_t , i256, int256_t )
75+ GENERATE_DECIMAL_CAST (i32 , int32_t , i64 , int64_t )
76+ GENERATE_DECIMAL_CAST (i32 , int32_t , i128 , int128_t )
77+ GENERATE_DECIMAL_CAST (i32 , int32_t , i256, int256_t )
78+ GENERATE_DECIMAL_CAST (i64 , int64_t , i128 , int128_t )
79+ GENERATE_DECIMAL_CAST (i64 , int64_t , i256, int256_t )
80+ GENERATE_DECIMAL_CAST (i128 , int128_t , i256, int256_t )
0 commit comments