|
15 | 15 | */
|
16 | 16 | #pragma once
|
17 | 17 |
|
18 |
| -#include <string> |
19 | 18 | #include "velox/functions/Macros.h"
|
20 |
| -#include "velox/functions/lib/RegistrationHelpers.h" |
21 | 19 |
|
22 | 20 | namespace facebook::velox::functions::sparksql {
|
23 | 21 |
|
24 |
| -void registerBitwiseFunctions(const std::string& prefix); |
| 22 | +template <typename T> |
| 23 | +struct BitwiseAndFunction { |
| 24 | + template <typename TInput> |
| 25 | + FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a, TInput b) { |
| 26 | + result = a & b; |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +template <typename T> |
| 31 | +struct BitwiseOrFunction { |
| 32 | + template <typename TInput> |
| 33 | + FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a, TInput b) { |
| 34 | + result = a | b; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +template <typename T> |
| 39 | +struct BitwiseXorFunction { |
| 40 | + template <typename TInput> |
| 41 | + FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a, TInput b) { |
| 42 | + result = a ^ b; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +template <typename T> |
| 47 | +struct BitwiseNotFunction { |
| 48 | + template <typename TInput> |
| 49 | + FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a) { |
| 50 | + result = ~a; |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +template <typename T> |
| 55 | +struct ShiftLeftFunction { |
| 56 | + template <typename TInput1, typename TInput2> |
| 57 | + FOLLY_ALWAYS_INLINE void call(TInput1& result, TInput1 a, TInput2 b) { |
| 58 | + if constexpr (std::is_same_v<TInput1, int32_t>) { |
| 59 | + if (b < 0) { |
| 60 | + b = b % 32 + 32; |
| 61 | + } |
| 62 | + if (b >= 32) { |
| 63 | + b = b % 32; |
| 64 | + } |
| 65 | + } |
| 66 | + if constexpr (std::is_same_v<TInput1, int64_t>) { |
| 67 | + if (b < 0) { |
| 68 | + b = b % 64 + 64; |
| 69 | + } |
| 70 | + if (b >= 64) { |
| 71 | + b = b % 64; |
| 72 | + } |
| 73 | + } |
| 74 | + result = a << b; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +struct ShiftRightFunction { |
| 80 | + template <typename TInput1, typename TInput2> |
| 81 | + FOLLY_ALWAYS_INLINE void call(TInput1& result, TInput1 a, TInput2 b) { |
| 82 | + if constexpr (std::is_same_v<TInput1, int32_t>) { |
| 83 | + if (b < 0) { |
| 84 | + b = b % 32 + 32; |
| 85 | + } |
| 86 | + if (b >= 32) { |
| 87 | + b = b % 32; |
| 88 | + } |
| 89 | + } |
| 90 | + if constexpr (std::is_same_v<TInput1, int64_t>) { |
| 91 | + if (b < 0) { |
| 92 | + b = b % 64 + 64; |
| 93 | + } |
| 94 | + if (b >= 64) { |
| 95 | + b = b % 64; |
| 96 | + } |
| 97 | + } |
| 98 | + result = a >> b; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +template <typename T> |
| 103 | +struct BitCountFunction { |
| 104 | + template <typename TInput> |
| 105 | + FOLLY_ALWAYS_INLINE void call(int32_t& result, TInput num) { |
| 106 | + constexpr int kMaxBits = sizeof(TInput) * CHAR_BIT; |
| 107 | + auto value = static_cast<uint64_t>(num); |
| 108 | + result = bits::countBits(&value, 0, kMaxBits); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +template <typename T> |
| 113 | +struct BitGetFunction { |
| 114 | + template <typename TInput> |
| 115 | + FOLLY_ALWAYS_INLINE void call(int8_t& result, TInput num, int32_t pos) { |
| 116 | + constexpr int kMaxBits = sizeof(TInput) * CHAR_BIT; |
| 117 | + VELOX_USER_CHECK_GE( |
| 118 | + pos, |
| 119 | + 0, |
| 120 | + "The value of 'pos' argument must be greater than or equal to zero."); |
| 121 | + VELOX_USER_CHECK_LT( |
| 122 | + pos, |
| 123 | + kMaxBits, |
| 124 | + "The value of 'pos' argument must not exceed the number of bits in 'x' - 1."); |
| 125 | + result = (num >> pos) & 1; |
| 126 | + } |
| 127 | +}; |
25 | 128 |
|
26 | 129 | } // namespace facebook::velox::functions::sparksql
|
0 commit comments