ClickHouse/src/Functions/multiply.cpp
2020-09-13 21:55:12 +08:00

54 lines
1.7 KiB
C++

#include <Functions/FunctionFactory.h>
#include <Functions/FunctionBinaryArithmetic.h>
#include <common/arithmeticOverflow.h>
namespace DB
{
template <typename A, typename B>
struct MultiplyImpl
{
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
static const constexpr bool allow_fixed_string = false;
template <typename Result = ResultType>
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
{
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
{
using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;
using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;
return bigint_cast<Result>(bigint_cast<CastA>(a)) * bigint_cast<Result>(bigint_cast<CastB>(b));
}
else
return static_cast<Result>(a) * b;
}
/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
template <typename Result = ResultType>
static inline bool apply(A a, B b, Result & c)
{
return common::mulOverflow(static_cast<Result>(a), b, c);
}
#if USE_EMBEDDED_COMPILER
static constexpr bool compilable = true;
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
{
return left->getType()->isIntegerTy() ? b.CreateMul(left, right) : b.CreateFMul(left, right);
}
#endif
};
struct NameMultiply { static constexpr auto name = "multiply"; };
using FunctionMultiply = BinaryArithmeticOverloadResolver<MultiplyImpl, NameMultiply>;
void registerFunctionMultiply(FunctionFactory & factory)
{
factory.registerFunction<FunctionMultiply>();
}
}