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
11namespace DB
12{
13
14namespace ErrorCodes
15{
16 extern const int ILLEGAL_DIVISION;
17}
18
19template <typename A, typename B>
20struct 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
36template <typename A, typename B>
37struct 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
88template <> struct BinaryOperationImpl<UInt64, UInt8, ModuloImpl<UInt64, UInt8>> : ModuloByConstantImpl<UInt64, UInt8> {};
89template <> struct BinaryOperationImpl<UInt64, UInt16, ModuloImpl<UInt64, UInt16>> : ModuloByConstantImpl<UInt64, UInt16> {};
90template <> struct BinaryOperationImpl<UInt64, UInt32, ModuloImpl<UInt64, UInt32>> : ModuloByConstantImpl<UInt64, UInt32> {};
91template <> struct BinaryOperationImpl<UInt64, UInt64, ModuloImpl<UInt64, UInt64>> : ModuloByConstantImpl<UInt64, UInt64> {};
92
93template <> struct BinaryOperationImpl<UInt32, UInt8, ModuloImpl<UInt32, UInt8>> : ModuloByConstantImpl<UInt32, UInt8> {};
94template <> struct BinaryOperationImpl<UInt32, UInt16, ModuloImpl<UInt32, UInt16>> : ModuloByConstantImpl<UInt32, UInt16> {};
95template <> struct BinaryOperationImpl<UInt32, UInt32, ModuloImpl<UInt32, UInt32>> : ModuloByConstantImpl<UInt32, UInt32> {};
96template <> struct BinaryOperationImpl<UInt32, UInt64, ModuloImpl<UInt32, UInt64>> : ModuloByConstantImpl<UInt32, UInt64> {};
97
98template <> struct BinaryOperationImpl<Int64, Int8, ModuloImpl<Int64, Int8>> : ModuloByConstantImpl<Int64, Int8> {};
99template <> struct BinaryOperationImpl<Int64, Int16, ModuloImpl<Int64, Int16>> : ModuloByConstantImpl<Int64, Int16> {};
100template <> struct BinaryOperationImpl<Int64, Int32, ModuloImpl<Int64, Int32>> : ModuloByConstantImpl<Int64, Int32> {};
101template <> struct BinaryOperationImpl<Int64, Int64, ModuloImpl<Int64, Int64>> : ModuloByConstantImpl<Int64, Int64> {};
102
103template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : ModuloByConstantImpl<Int32, Int8> {};
104template <> struct BinaryOperationImpl<Int32, Int16, ModuloImpl<Int32, Int16>> : ModuloByConstantImpl<Int32, Int16> {};
105template <> struct BinaryOperationImpl<Int32, Int32, ModuloImpl<Int32, Int32>> : ModuloByConstantImpl<Int32, Int32> {};
106template <> struct BinaryOperationImpl<Int32, Int64, ModuloImpl<Int32, Int64>> : ModuloByConstantImpl<Int32, Int64> {};
107
108
109struct NameModulo { static constexpr auto name = "modulo"; };
110using FunctionModulo = FunctionBinaryArithmetic<ModuloImpl, NameModulo, false>;
111
112void registerFunctionModulo(FunctionFactory & factory)
113{
114 factory.registerFunction<FunctionModulo>();
115}
116
117}
118