ClickHouse

Форк
0
/
multiply.cpp 
61 строка · 2.0 Кб
1
#include <type_traits>
2
#include <Functions/FunctionFactory.h>
3
#include <Functions/FunctionBinaryArithmetic.h>
4
#include <base/arithmeticOverflow.h>
5

6
namespace DB
7
{
8

9
template <typename A, typename B>
10
struct MultiplyImpl
11
{
12
    using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
13
    static const constexpr bool allow_fixed_string = false;
14
    static const constexpr bool allow_string_integer = false;
15

16
    template <typename Result = ResultType>
17
    static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
18
    {
19
        if constexpr (is_big_int_v<A> || is_big_int_v<B>)
20
        {
21
            using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;
22
            using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;
23

24
            return static_cast<Result>(static_cast<CastA>(a)) * static_cast<Result>(static_cast<CastB>(b));
25
        }
26
        else
27
            return static_cast<Result>(a) * b;
28
    }
29

30
    /// Apply operation and check overflow. It's used for Decimal operations. @returns true if overflowed, false otherwise.
31
    template <typename Result = ResultType>
32
    static inline bool apply(A a, B b, Result & c)
33
    {
34
        if constexpr (std::is_same_v<Result, float> || std::is_same_v<Result, double>)
35
        {
36
            c = static_cast<Result>(a) * b;
37
            return false;
38
        }
39
        else
40
            return common::mulOverflow(static_cast<Result>(a), b, c);
41
    }
42

43
#if USE_EMBEDDED_COMPILER
44
    static constexpr bool compilable = true;
45

46
    static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
47
    {
48
        return left->getType()->isIntegerTy() ? b.CreateMul(left, right) : b.CreateFMul(left, right);
49
    }
50
#endif
51
};
52

53
struct NameMultiply { static constexpr auto name = "multiply"; };
54
using FunctionMultiply = BinaryArithmeticOverloadResolver<MultiplyImpl, NameMultiply>;
55

56
REGISTER_FUNCTION(Multiply)
57
{
58
    factory.registerFunction<FunctionMultiply>();
59
}
60

61
}
62

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

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

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

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