ClickHouse
61 строка · 2.0 Кб
1#include <type_traits>
2#include <Functions/FunctionFactory.h>
3#include <Functions/FunctionBinaryArithmetic.h>
4#include <base/arithmeticOverflow.h>
5
6namespace DB
7{
8
9template <typename A, typename B>
10struct MultiplyImpl
11{
12using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
13static const constexpr bool allow_fixed_string = false;
14static const constexpr bool allow_string_integer = false;
15
16template <typename Result = ResultType>
17static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
18{
19if constexpr (is_big_int_v<A> || is_big_int_v<B>)
20{
21using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;
22using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;
23
24return static_cast<Result>(static_cast<CastA>(a)) * static_cast<Result>(static_cast<CastB>(b));
25}
26else
27return 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.
31template <typename Result = ResultType>
32static inline bool apply(A a, B b, Result & c)
33{
34if constexpr (std::is_same_v<Result, float> || std::is_same_v<Result, double>)
35{
36c = static_cast<Result>(a) * b;
37return false;
38}
39else
40return common::mulOverflow(static_cast<Result>(a), b, c);
41}
42
43#if USE_EMBEDDED_COMPILER
44static constexpr bool compilable = true;
45
46static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
47{
48return left->getType()->isIntegerTy() ? b.CreateMul(left, right) : b.CreateFMul(left, right);
49}
50#endif
51};
52
53struct NameMultiply { static constexpr auto name = "multiply"; };
54using FunctionMultiply = BinaryArithmeticOverloadResolver<MultiplyImpl, NameMultiply>;
55
56REGISTER_FUNCTION(Multiply)
57{
58factory.registerFunction<FunctionMultiply>();
59}
60
61}
62