| 1 | #include <Functions/FunctionFactory.h> | 
|---|
| 2 | #include <Functions/FunctionBinaryArithmetic.h> | 
|---|
| 3 |  | 
|---|
| 4 | #ifdef __SSE2__ | 
|---|
| 5 | #define LIBDIVIDE_USE_SSE2 1 | 
|---|
| 6 | #endif | 
|---|
| 7 |  | 
|---|
| 8 | #include <libdivide.h> | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | namespace DB | 
|---|
| 12 | { | 
|---|
| 13 |  | 
|---|
| 14 | namespace ErrorCodes | 
|---|
| 15 | { | 
|---|
| 16 | extern const int ILLEGAL_DIVISION; | 
|---|
| 17 | } | 
|---|
| 18 |  | 
|---|
| 19 | template <typename A, typename B> | 
|---|
| 20 | struct ModuloImpl | 
|---|
| 21 | { | 
|---|
| 22 | using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type; | 
|---|
| 23 |  | 
|---|
| 24 | template <typename Result = ResultType> | 
|---|
| 25 | static inline Result apply(A a, B b) | 
|---|
| 26 | { | 
|---|
| 27 | throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b)); | 
|---|
| 28 | return typename NumberTraits::ToInteger<A>::Type(a) % typename NumberTraits::ToInteger<B>::Type(b); | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | #if USE_EMBEDDED_COMPILER | 
|---|
| 32 | static constexpr bool compilable = false; /// don't know how to throw from LLVM IR | 
|---|
| 33 | #endif | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 36 | template <typename A, typename B> | 
|---|
| 37 | struct ModuloByConstantImpl | 
|---|
| 38 | : BinaryOperationImplBase<A, B, ModuloImpl<A, B>> | 
|---|
| 39 | { | 
|---|
| 40 | using ResultType = typename ModuloImpl<A, B>::ResultType; | 
|---|
| 41 |  | 
|---|
| 42 | static void vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c) | 
|---|
| 43 | { | 
|---|
| 44 | if (unlikely(b == 0)) | 
|---|
| 45 | throw Exception( "Division by zero", ErrorCodes::ILLEGAL_DIVISION); | 
|---|
| 46 |  | 
|---|
| 47 | #pragma GCC diagnostic push | 
|---|
| 48 | #pragma GCC diagnostic ignored "-Wsign-compare" | 
|---|
| 49 |  | 
|---|
| 50 | if (unlikely((std::is_signed_v<B> && b == -1) || b == 1)) | 
|---|
| 51 | { | 
|---|
| 52 | size_t size = a.size(); | 
|---|
| 53 | for (size_t i = 0; i < size; ++i) | 
|---|
| 54 | c[i] = 0; | 
|---|
| 55 | return; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | #pragma GCC diagnostic pop | 
|---|
| 59 |  | 
|---|
| 60 | libdivide::divider<A> divider(b); | 
|---|
| 61 |  | 
|---|
| 62 | /// Here we failed to make the SSE variant from libdivide give an advantage. | 
|---|
| 63 | size_t size = a.size(); | 
|---|
| 64 |  | 
|---|
| 65 | /// strict aliasing optimization for char like arrays | 
|---|
| 66 | auto * __restrict src = a.data(); | 
|---|
| 67 | auto * __restrict dst = c.data(); | 
|---|
| 68 |  | 
|---|
| 69 | if (b & (b - 1)) | 
|---|
| 70 | { | 
|---|
| 71 | for (size_t i = 0; i < size; ++i) | 
|---|
| 72 | dst[i] = src[i] - (src[i] / divider) * b; /// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved. | 
|---|
| 73 | } | 
|---|
| 74 | else | 
|---|
| 75 | { | 
|---|
| 76 | // gcc libdivide doesn't work well for pow2 division | 
|---|
| 77 | auto mask = b - 1; | 
|---|
| 78 | for (size_t i = 0; i < size; ++i) | 
|---|
| 79 | dst[i] = src[i] & mask; | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | /** Specializations are specified for dividing numbers of the type UInt64 and UInt32 by the numbers of the same sign. | 
|---|
| 85 | * Can be expanded to all possible combinations, but more code is needed. | 
|---|
| 86 | */ | 
|---|
| 87 |  | 
|---|
| 88 | template <> struct BinaryOperationImpl<UInt64, UInt8, ModuloImpl<UInt64, UInt8>> : ModuloByConstantImpl<UInt64, UInt8> {}; | 
|---|
| 89 | template <> struct BinaryOperationImpl<UInt64, UInt16, ModuloImpl<UInt64, UInt16>> : ModuloByConstantImpl<UInt64, UInt16> {}; | 
|---|
| 90 | template <> struct BinaryOperationImpl<UInt64, UInt32, ModuloImpl<UInt64, UInt32>> : ModuloByConstantImpl<UInt64, UInt32> {}; | 
|---|
| 91 | template <> struct BinaryOperationImpl<UInt64, UInt64, ModuloImpl<UInt64, UInt64>> : ModuloByConstantImpl<UInt64, UInt64> {}; | 
|---|
| 92 |  | 
|---|
| 93 | template <> struct BinaryOperationImpl<UInt32, UInt8, ModuloImpl<UInt32, UInt8>> : ModuloByConstantImpl<UInt32, UInt8> {}; | 
|---|
| 94 | template <> struct BinaryOperationImpl<UInt32, UInt16, ModuloImpl<UInt32, UInt16>> : ModuloByConstantImpl<UInt32, UInt16> {}; | 
|---|
| 95 | template <> struct BinaryOperationImpl<UInt32, UInt32, ModuloImpl<UInt32, UInt32>> : ModuloByConstantImpl<UInt32, UInt32> {}; | 
|---|
| 96 | template <> struct BinaryOperationImpl<UInt32, UInt64, ModuloImpl<UInt32, UInt64>> : ModuloByConstantImpl<UInt32, UInt64> {}; | 
|---|
| 97 |  | 
|---|
| 98 | template <> struct BinaryOperationImpl<Int64, Int8, ModuloImpl<Int64, Int8>> : ModuloByConstantImpl<Int64, Int8> {}; | 
|---|
| 99 | template <> struct BinaryOperationImpl<Int64, Int16, ModuloImpl<Int64, Int16>> : ModuloByConstantImpl<Int64, Int16> {}; | 
|---|
| 100 | template <> struct BinaryOperationImpl<Int64, Int32, ModuloImpl<Int64, Int32>> : ModuloByConstantImpl<Int64, Int32> {}; | 
|---|
| 101 | template <> struct BinaryOperationImpl<Int64, Int64, ModuloImpl<Int64, Int64>> : ModuloByConstantImpl<Int64, Int64> {}; | 
|---|
| 102 |  | 
|---|
| 103 | template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : ModuloByConstantImpl<Int32, Int8> {}; | 
|---|
| 104 | template <> struct BinaryOperationImpl<Int32, Int16, ModuloImpl<Int32, Int16>> : ModuloByConstantImpl<Int32, Int16> {}; | 
|---|
| 105 | template <> struct BinaryOperationImpl<Int32, Int32, ModuloImpl<Int32, Int32>> : ModuloByConstantImpl<Int32, Int32> {}; | 
|---|
| 106 | template <> struct BinaryOperationImpl<Int32, Int64, ModuloImpl<Int32, Int64>> : ModuloByConstantImpl<Int32, Int64> {}; | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 | struct NameModulo { static constexpr auto name = "modulo"; }; | 
|---|
| 110 | using FunctionModulo = FunctionBinaryArithmetic<ModuloImpl, NameModulo, false>; | 
|---|
| 111 |  | 
|---|
| 112 | void registerFunctionModulo(FunctionFactory & factory) | 
|---|
| 113 | { | 
|---|
| 114 | factory.registerFunction<FunctionModulo>(); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|