2018-11-26 16:20:40 +00:00
|
|
|
#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_decimal = true;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
2018-12-26 23:55:09 +00:00
|
|
|
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
return static_cast<Result>(a) * b;
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:56:53 +00:00
|
|
|
/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
|
2018-11-26 16:20:40 +00:00
|
|
|
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 = FunctionBinaryArithmetic<MultiplyImpl, NameMultiply>;
|
|
|
|
|
|
|
|
void registerFunctionMultiply(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionMultiply>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|