ClickHouse

Форк
0
/
intDiv.cpp 
129 строк · 5.4 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionBinaryArithmetic.h>
3

4
#include "divide/divide.h"
5

6

7
namespace DB
8
{
9
namespace ErrorCodes
10
{
11
    extern const int ILLEGAL_DIVISION;
12
}
13

14
namespace
15
{
16

17
/// Optimizations for integer division by a constant.
18

19
template <typename A, typename B>
20
struct DivideIntegralByConstantImpl
21
    : BinaryOperation<A, B, DivideIntegralImpl<A, B>>
22
{
23
    using Op = DivideIntegralImpl<A, B>;
24
    using ResultType = typename Op::ResultType;
25
    static const constexpr bool allow_fixed_string = false;
26
    static const constexpr bool allow_string_integer = false;
27

28
    template <OpCase op_case>
29
    static void NO_INLINE process(const A * __restrict a, const B * __restrict b, ResultType * __restrict c, size_t size, const NullMap * right_nullmap)
30
    {
31
        if constexpr (op_case == OpCase::RightConstant)
32
        {
33
            if (right_nullmap && (*right_nullmap)[0])
34
                return;
35

36
            vectorConstant(a, *b, c, size);
37
        }
38
        else
39
        {
40
            if (right_nullmap)
41
            {
42
                for (size_t i = 0; i < size; ++i)
43
                    if ((*right_nullmap)[i])
44
                        c[i] = ResultType();
45
                    else
46
                        apply<op_case>(a, b, c, i);
47
            }
48
            else
49
                for (size_t i = 0; i < size; ++i)
50
                    apply<op_case>(a, b, c, i);
51
        }
52
    }
53

54
    static ResultType process(A a, B b) { return Op::template apply<ResultType>(a, b); }
55

56
    static void NO_INLINE NO_SANITIZE_UNDEFINED vectorConstant(const A * __restrict a_pos, B b, ResultType * __restrict c_pos, size_t size)
57
    {
58
        /// Division by -1. By the way, we avoid FPE by division of the largest negative number by -1.
59
        if (unlikely(is_signed_v<B> && b == -1))
60
        {
61
            for (size_t i = 0; i < size; ++i)
62
                c_pos[i] = -make_unsigned_t<A>(a_pos[i]);   /// Avoid UBSan report in signed integer overflow.
63
            return;
64
        }
65

66
        /// Division with too large divisor.
67
        if (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
        {
70
            for (size_t i = 0; i < size; ++i)
71
                c_pos[i] = 0;
72
            return;
73
        }
74

75
        if (unlikely(static_cast<A>(b) == 0))
76
            throw Exception(ErrorCodes::ILLEGAL_DIVISION, "Division by zero");
77

78
        divideImpl(a_pos, b, c_pos, size);
79
    }
80

81
private:
82
    template <OpCase op_case>
83
    static inline void apply(const A * __restrict a, const B * __restrict b, ResultType * __restrict c, size_t i)
84
    {
85
        if constexpr (op_case == OpCase::Vector)
86
            c[i] = Op::template apply<ResultType>(a[i], b[i]);
87
        else
88
            c[i] = Op::template apply<ResultType>(*a, b[i]);
89
    }
90
};
91

92
/** Specializations are specified for dividing numbers of the type UInt64, UInt32, Int64, Int32 by the numbers of the same sign.
93
  * Can be expanded to all possible combinations, but more code is needed.
94
  */
95

96
}
97

98
namespace impl_
99
{
100
template <> struct BinaryOperationImpl<UInt64, UInt8, DivideIntegralImpl<UInt64, UInt8>> : DivideIntegralByConstantImpl<UInt64, UInt8> {};
101
template <> struct BinaryOperationImpl<UInt64, UInt16, DivideIntegralImpl<UInt64, UInt16>> : DivideIntegralByConstantImpl<UInt64, UInt16> {};
102
template <> struct BinaryOperationImpl<UInt64, UInt32, DivideIntegralImpl<UInt64, UInt32>> : DivideIntegralByConstantImpl<UInt64, UInt32> {};
103
template <> struct BinaryOperationImpl<UInt64, UInt64, DivideIntegralImpl<UInt64, UInt64>> : DivideIntegralByConstantImpl<UInt64, UInt64> {};
104

105
template <> struct BinaryOperationImpl<UInt32, UInt8, DivideIntegralImpl<UInt32, UInt8>> : DivideIntegralByConstantImpl<UInt32, UInt8> {};
106
template <> struct BinaryOperationImpl<UInt32, UInt16, DivideIntegralImpl<UInt32, UInt16>> : DivideIntegralByConstantImpl<UInt32, UInt16> {};
107
template <> struct BinaryOperationImpl<UInt32, UInt32, DivideIntegralImpl<UInt32, UInt32>> : DivideIntegralByConstantImpl<UInt32, UInt32> {};
108
template <> struct BinaryOperationImpl<UInt32, UInt64, DivideIntegralImpl<UInt32, UInt64>> : DivideIntegralByConstantImpl<UInt32, UInt64> {};
109

110
template <> struct BinaryOperationImpl<Int64, Int8, DivideIntegralImpl<Int64, Int8>> : DivideIntegralByConstantImpl<Int64, Int8> {};
111
template <> struct BinaryOperationImpl<Int64, Int16, DivideIntegralImpl<Int64, Int16>> : DivideIntegralByConstantImpl<Int64, Int16> {};
112
template <> struct BinaryOperationImpl<Int64, Int32, DivideIntegralImpl<Int64, Int32>> : DivideIntegralByConstantImpl<Int64, Int32> {};
113
template <> struct BinaryOperationImpl<Int64, Int64, DivideIntegralImpl<Int64, Int64>> : DivideIntegralByConstantImpl<Int64, Int64> {};
114

115
template <> struct BinaryOperationImpl<Int32, Int8, DivideIntegralImpl<Int32, Int8>> : DivideIntegralByConstantImpl<Int32, Int8> {};
116
template <> struct BinaryOperationImpl<Int32, Int16, DivideIntegralImpl<Int32, Int16>> : DivideIntegralByConstantImpl<Int32, Int16> {};
117
template <> struct BinaryOperationImpl<Int32, Int32, DivideIntegralImpl<Int32, Int32>> : DivideIntegralByConstantImpl<Int32, Int32> {};
118
template <> struct BinaryOperationImpl<Int32, Int64, DivideIntegralImpl<Int32, Int64>> : DivideIntegralByConstantImpl<Int32, Int64> {};
119
}
120

121
struct NameIntDiv { static constexpr auto name = "intDiv"; };
122
using FunctionIntDiv = BinaryArithmeticOverloadResolver<DivideIntegralImpl, NameIntDiv, false>;
123

124
REGISTER_FUNCTION(IntDiv)
125
{
126
    factory.registerFunction<FunctionIntDiv>();
127
}
128

129
}
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.