ClickHouse

Форк
0
54 строки · 1.7 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionBinaryArithmetic.h>
3
#include <base/arithmeticOverflow.h>
4

5
namespace DB
6
{
7

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

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

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

29
    /// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
30
    template <typename Result = ResultType>
31
    static inline bool apply(A a, B b, Result & c)
32
    {
33
        return common::subOverflow(static_cast<Result>(a), b, c);
34
    }
35

36
#if USE_EMBEDDED_COMPILER
37
    static constexpr bool compilable = true;
38

39
    static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
40
    {
41
        return left->getType()->isIntegerTy() ? b.CreateSub(left, right) : b.CreateFSub(left, right);
42
    }
43
#endif
44
};
45

46
struct NameMinus { static constexpr auto name = "minus"; };
47
using FunctionMinus = BinaryArithmeticOverloadResolver<MinusImpl, NameMinus>;
48

49
REGISTER_FUNCTION(Minus)
50
{
51
    factory.registerFunction<FunctionMinus>();
52
}
53

54
}
55

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

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

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

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