1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionBinaryArithmetic.h>
3#include <Functions/intDiv.h>
4
5#ifdef __SSE2__
6 #define LIBDIVIDE_USE_SSE2 1
7#endif
8
9#include <libdivide.h>
10
11
12namespace DB
13{
14
15/// Optimizations for integer division by a constant.
16
17template <typename A, typename B>
18struct DivideIntegralByConstantImpl
19 : BinaryOperationImplBase<A, B, DivideIntegralImpl<A, B>>
20{
21 using ResultType = typename DivideIntegralImpl<A, B>::ResultType;
22
23 static void vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c)
24 {
25 if (unlikely(b == 0))
26 throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
27
28#pragma GCC diagnostic push
29#pragma GCC diagnostic ignored "-Wsign-compare"
30
31 if (unlikely(is_signed_v<B> && b == -1))
32 {
33 size_t size = a.size();
34 for (size_t i = 0; i < size; ++i)
35 c[i] = -c[i];
36 return;
37 }
38
39#pragma GCC diagnostic pop
40
41 libdivide::divider<A> divider(b);
42
43 size_t size = a.size();
44 const A * a_pos = a.data();
45 const A * a_end = a_pos + size;
46 ResultType * c_pos = c.data();
47
48#ifdef __SSE2__
49 static constexpr size_t values_per_sse_register = 16 / sizeof(A);
50 const A * a_end_sse = a_pos + size / values_per_sse_register * values_per_sse_register;
51
52 while (a_pos < a_end_sse)
53 {
54 _mm_storeu_si128(reinterpret_cast<__m128i *>(c_pos),
55 _mm_loadu_si128(reinterpret_cast<const __m128i *>(a_pos)) / divider);
56
57 a_pos += values_per_sse_register;
58 c_pos += values_per_sse_register;
59 }
60#endif
61
62 while (a_pos < a_end)
63 {
64 *c_pos = *a_pos / divider;
65 ++a_pos;
66 ++c_pos;
67 }
68 }
69};
70
71/** Specializations are specified for dividing numbers of the type UInt64 and UInt32 by the numbers of the same sign.
72 * Can be expanded to all possible combinations, but more code is needed.
73 */
74
75template <> struct BinaryOperationImpl<UInt64, UInt8, DivideIntegralImpl<UInt64, UInt8>> : DivideIntegralByConstantImpl<UInt64, UInt8> {};
76template <> struct BinaryOperationImpl<UInt64, UInt16, DivideIntegralImpl<UInt64, UInt16>> : DivideIntegralByConstantImpl<UInt64, UInt16> {};
77template <> struct BinaryOperationImpl<UInt64, UInt32, DivideIntegralImpl<UInt64, UInt32>> : DivideIntegralByConstantImpl<UInt64, UInt32> {};
78template <> struct BinaryOperationImpl<UInt64, UInt64, DivideIntegralImpl<UInt64, UInt64>> : DivideIntegralByConstantImpl<UInt64, UInt64> {};
79
80template <> struct BinaryOperationImpl<UInt32, UInt8, DivideIntegralImpl<UInt32, UInt8>> : DivideIntegralByConstantImpl<UInt32, UInt8> {};
81template <> struct BinaryOperationImpl<UInt32, UInt16, DivideIntegralImpl<UInt32, UInt16>> : DivideIntegralByConstantImpl<UInt32, UInt16> {};
82template <> struct BinaryOperationImpl<UInt32, UInt32, DivideIntegralImpl<UInt32, UInt32>> : DivideIntegralByConstantImpl<UInt32, UInt32> {};
83template <> struct BinaryOperationImpl<UInt32, UInt64, DivideIntegralImpl<UInt32, UInt64>> : DivideIntegralByConstantImpl<UInt32, UInt64> {};
84
85template <> struct BinaryOperationImpl<Int64, Int8, DivideIntegralImpl<Int64, Int8>> : DivideIntegralByConstantImpl<Int64, Int8> {};
86template <> struct BinaryOperationImpl<Int64, Int16, DivideIntegralImpl<Int64, Int16>> : DivideIntegralByConstantImpl<Int64, Int16> {};
87template <> struct BinaryOperationImpl<Int64, Int32, DivideIntegralImpl<Int64, Int32>> : DivideIntegralByConstantImpl<Int64, Int32> {};
88template <> struct BinaryOperationImpl<Int64, Int64, DivideIntegralImpl<Int64, Int64>> : DivideIntegralByConstantImpl<Int64, Int64> {};
89
90template <> struct BinaryOperationImpl<Int32, Int8, DivideIntegralImpl<Int32, Int8>> : DivideIntegralByConstantImpl<Int32, Int8> {};
91template <> struct BinaryOperationImpl<Int32, Int16, DivideIntegralImpl<Int32, Int16>> : DivideIntegralByConstantImpl<Int32, Int16> {};
92template <> struct BinaryOperationImpl<Int32, Int32, DivideIntegralImpl<Int32, Int32>> : DivideIntegralByConstantImpl<Int32, Int32> {};
93template <> struct BinaryOperationImpl<Int32, Int64, DivideIntegralImpl<Int32, Int64>> : DivideIntegralByConstantImpl<Int32, Int64> {};
94
95
96struct NameIntDiv { static constexpr auto name = "intDiv"; };
97using FunctionIntDiv = FunctionBinaryArithmetic<DivideIntegralImpl, NameIntDiv, false>;
98
99void registerFunctionIntDiv(FunctionFactory & factory)
100{
101 factory.registerFunction<FunctionIntDiv>();
102}
103
104}
105