ClickHouse
193 строки · 7.6 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionBinaryArithmetic.h>
3
4#include <libdivide-config.h>
5#include <libdivide.h>
6
7
8namespace DB
9{
10namespace ErrorCodes
11{
12extern const int ILLEGAL_DIVISION;
13}
14
15namespace
16{
17
18/// Optimizations for integer modulo by a constant.
19
20template <typename A, typename B>
21struct ModuloByConstantImpl
22: BinaryOperation<A, B, ModuloImpl<A, B>>
23{
24using Op = ModuloImpl<A, B>;
25using ResultType = typename Op::ResultType;
26static const constexpr bool allow_fixed_string = false;
27static const constexpr bool allow_string_integer = false;
28
29template <OpCase op_case>
30static void NO_INLINE process(const A * __restrict a, const B * __restrict b, ResultType * __restrict c, size_t size, const NullMap * right_nullmap)
31{
32if constexpr (op_case == OpCase::RightConstant)
33{
34if (right_nullmap && (*right_nullmap)[0])
35return;
36vectorConstant(a, *b, c, size);
37}
38else
39{
40if (right_nullmap)
41{
42for (size_t i = 0; i < size; ++i)
43if ((*right_nullmap)[i])
44c[i] = ResultType();
45else
46apply<op_case>(a, b, c, i);
47}
48else
49for (size_t i = 0; i < size; ++i)
50apply<op_case>(a, b, c, i);
51}
52}
53
54static ResultType process(A a, B b) { return Op::template apply<ResultType>(a, b); }
55
56static void NO_INLINE NO_SANITIZE_UNDEFINED vectorConstant(const A * __restrict src, B b, ResultType * __restrict dst, size_t size)
57{
58/// Modulo with too small divisor.
59if (unlikely((std::is_signed_v<B> && b == -1) || b == 1))
60{
61for (size_t i = 0; i < size; ++i)
62dst[i] = 0;
63return;
64}
65
66/// Modulo with too large divisor.
67if (unlikely(b > std::numeric_limits<A>::max()
68|| (std::is_signed_v<A> && std::is_signed_v<B> && b < std::numeric_limits<A>::lowest())))
69{
70for (size_t i = 0; i < size; ++i)
71dst[i] = static_cast<ResultType>(src[i]);
72return;
73}
74
75if (unlikely(static_cast<A>(b) == 0))
76throw Exception(ErrorCodes::ILLEGAL_DIVISION, "Division by zero");
77
78/// Division by min negative value.
79if (std::is_signed_v<B> && b == std::numeric_limits<B>::lowest())
80throw Exception(ErrorCodes::ILLEGAL_DIVISION, "Division by the most negative number");
81
82/// Modulo of division by negative number is the same as the positive number.
83if (b < 0)
84b = -b;
85
86/// Here we failed to make the SSE variant from libdivide give an advantage.
87
88if (b & (b - 1))
89{
90libdivide::divider<A> divider(static_cast<A>(b));
91for (size_t i = 0; i < size; ++i)
92{
93/// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved.
94dst[i] = static_cast<ResultType>(src[i] - (src[i] / divider) * b);
95}
96}
97else
98{
99// gcc libdivide doesn't work well for pow2 division
100auto mask = b - 1;
101for (size_t i = 0; i < size; ++i)
102dst[i] = static_cast<ResultType>(src[i] & mask);
103}
104}
105
106private:
107template <OpCase op_case>
108static inline void apply(const A * __restrict a, const B * __restrict b, ResultType * __restrict c, size_t i)
109{
110if constexpr (op_case == OpCase::Vector)
111c[i] = Op::template apply<ResultType>(a[i], b[i]);
112else
113c[i] = Op::template apply<ResultType>(*a, b[i]);
114}
115};
116
117template <typename A, typename B>
118struct ModuloLegacyByConstantImpl : ModuloByConstantImpl<A, B>
119{
120using Op = ModuloLegacyImpl<A, B>;
121};
122
123}
124
125/** Specializations are specified for dividing numbers of the type UInt64 and UInt32 by the numbers of the same sign.
126* Can be expanded to all possible combinations, but more code is needed.
127*/
128
129namespace impl_
130{
131template <> struct BinaryOperationImpl<UInt64, UInt8, ModuloImpl<UInt64, UInt8>> : ModuloByConstantImpl<UInt64, UInt8> {};
132template <> struct BinaryOperationImpl<UInt64, UInt16, ModuloImpl<UInt64, UInt16>> : ModuloByConstantImpl<UInt64, UInt16> {};
133template <> struct BinaryOperationImpl<UInt64, UInt32, ModuloImpl<UInt64, UInt32>> : ModuloByConstantImpl<UInt64, UInt32> {};
134template <> struct BinaryOperationImpl<UInt64, UInt64, ModuloImpl<UInt64, UInt64>> : ModuloByConstantImpl<UInt64, UInt64> {};
135
136template <> struct BinaryOperationImpl<UInt32, UInt8, ModuloImpl<UInt32, UInt8>> : ModuloByConstantImpl<UInt32, UInt8> {};
137template <> struct BinaryOperationImpl<UInt32, UInt16, ModuloImpl<UInt32, UInt16>> : ModuloByConstantImpl<UInt32, UInt16> {};
138template <> struct BinaryOperationImpl<UInt32, UInt32, ModuloImpl<UInt32, UInt32>> : ModuloByConstantImpl<UInt32, UInt32> {};
139template <> struct BinaryOperationImpl<UInt32, UInt64, ModuloImpl<UInt32, UInt64>> : ModuloByConstantImpl<UInt32, UInt64> {};
140
141template <> struct BinaryOperationImpl<Int64, Int8, ModuloImpl<Int64, Int8>> : ModuloByConstantImpl<Int64, Int8> {};
142template <> struct BinaryOperationImpl<Int64, Int16, ModuloImpl<Int64, Int16>> : ModuloByConstantImpl<Int64, Int16> {};
143template <> struct BinaryOperationImpl<Int64, Int32, ModuloImpl<Int64, Int32>> : ModuloByConstantImpl<Int64, Int32> {};
144template <> struct BinaryOperationImpl<Int64, Int64, ModuloImpl<Int64, Int64>> : ModuloByConstantImpl<Int64, Int64> {};
145
146template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : ModuloByConstantImpl<Int32, Int8> {};
147template <> struct BinaryOperationImpl<Int32, Int16, ModuloImpl<Int32, Int16>> : ModuloByConstantImpl<Int32, Int16> {};
148template <> struct BinaryOperationImpl<Int32, Int32, ModuloImpl<Int32, Int32>> : ModuloByConstantImpl<Int32, Int32> {};
149template <> struct BinaryOperationImpl<Int32, Int64, ModuloImpl<Int32, Int64>> : ModuloByConstantImpl<Int32, Int64> {};
150}
151
152struct NameModulo { static constexpr auto name = "modulo"; };
153using FunctionModulo = BinaryArithmeticOverloadResolver<ModuloImpl, NameModulo, false>;
154
155REGISTER_FUNCTION(Modulo)
156{
157factory.registerFunction<FunctionModulo>();
158factory.registerAlias("mod", "modulo", FunctionFactory::CaseInsensitive);
159}
160
161struct NameModuloLegacy { static constexpr auto name = "moduloLegacy"; };
162using FunctionModuloLegacy = BinaryArithmeticOverloadResolver<ModuloLegacyImpl, NameModuloLegacy, false>;
163
164REGISTER_FUNCTION(ModuloLegacy)
165{
166factory.registerFunction<FunctionModuloLegacy>();
167}
168
169struct NamePositiveModulo
170{
171static constexpr auto name = "positiveModulo";
172};
173using FunctionPositiveModulo = BinaryArithmeticOverloadResolver<PositiveModuloImpl, NamePositiveModulo, false>;
174
175REGISTER_FUNCTION(PositiveModulo)
176{
177factory.registerFunction<FunctionPositiveModulo>(FunctionDocumentation
178{
179.description = R"(
180Calculates the remainder when dividing `a` by `b`. Similar to function `modulo` except that `positiveModulo` always return non-negative number.
181Returns the difference between `a` and the nearest integer not greater than `a` divisible by `b`.
182In other words, the function returning the modulus (modulo) in the terms of Modular Arithmetic.
183)",
184.examples{{"positiveModulo", "SELECT positiveModulo(-1, 10);", ""}},
185.categories{"Arithmetic"}},
186FunctionFactory::CaseInsensitive);
187
188factory.registerAlias("positive_modulo", "positiveModulo", FunctionFactory::CaseInsensitive);
189/// Compatibility with Spark:
190factory.registerAlias("pmod", "positiveModulo", FunctionFactory::CaseInsensitive);
191}
192
193}
194