mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 08:02:02 +00:00
97f2a2213e
* Move some code outside dbms/src folder * Fix paths
64 lines
2.0 KiB
C++
64 lines
2.0 KiB
C++
#include <Functions/FunctionFactory.h>
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
#include <Core/AccurateComparison.h>
|
|
|
|
namespace DB
|
|
{
|
|
|
|
template <typename A, typename B>
|
|
struct LeastBaseImpl
|
|
{
|
|
using ResultType = NumberTraits::ResultOfLeast<A, B>;
|
|
static const constexpr bool allow_fixed_string = false;
|
|
|
|
template <typename Result = ResultType>
|
|
static inline Result apply(A a, B b)
|
|
{
|
|
/** gcc 4.9.2 successfully vectorizes a loop from this function. */
|
|
return static_cast<Result>(a) < static_cast<Result>(b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
}
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
static constexpr bool compilable = true;
|
|
|
|
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool is_signed)
|
|
{
|
|
if (!left->getType()->isIntegerTy())
|
|
/// XXX minnum is basically fmin(), it may or may not match whatever apply() does
|
|
return b.CreateMinNum(left, right);
|
|
return b.CreateSelect(is_signed ? b.CreateICmpSLT(left, right) : b.CreateICmpULT(left, right), left, right);
|
|
}
|
|
#endif
|
|
};
|
|
|
|
template <typename A, typename B>
|
|
struct LeastSpecialImpl
|
|
{
|
|
using ResultType = std::make_signed_t<A>;
|
|
static const constexpr bool allow_fixed_string = false;
|
|
|
|
template <typename Result = ResultType>
|
|
static inline Result apply(A a, B b)
|
|
{
|
|
static_assert(std::is_same_v<Result, ResultType>, "ResultType != Result");
|
|
return accurate::lessOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
}
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
static constexpr bool compilable = false; /// ???
|
|
#endif
|
|
};
|
|
|
|
template <typename A, typename B>
|
|
using LeastImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>, LeastBaseImpl<A, B>, LeastSpecialImpl<A, B>>;
|
|
|
|
struct NameLeast { static constexpr auto name = "least"; };
|
|
using FunctionLeast = FunctionBinaryArithmetic<LeastImpl, NameLeast>;
|
|
|
|
void registerFunctionLeast(FunctionFactory & factory)
|
|
{
|
|
factory.registerFunction<FunctionLeast>();
|
|
}
|
|
|
|
}
|