| 1 | #include <Functions/FunctionFactory.h> | 
|---|
| 2 | #include <Functions/FunctionBinaryArithmetic.h> | 
|---|
| 3 | #include <Core/AccurateComparison.h> | 
|---|
| 4 |  | 
|---|
| 5 | namespace DB | 
|---|
| 6 | { | 
|---|
| 7 |  | 
|---|
| 8 | template <typename A, typename B> | 
|---|
| 9 | struct GreatestBaseImpl | 
|---|
| 10 | { | 
|---|
| 11 | using ResultType = NumberTraits::ResultOfGreatest<A, B>; | 
|---|
| 12 |  | 
|---|
| 13 | template <typename Result = ResultType> | 
|---|
| 14 | static inline Result apply(A a, B b) | 
|---|
| 15 | { | 
|---|
| 16 | return static_cast<Result>(a) > static_cast<Result>(b) ? static_cast<Result>(a) : static_cast<Result>(b); | 
|---|
| 17 | } | 
|---|
| 18 |  | 
|---|
| 19 | #if USE_EMBEDDED_COMPILER | 
|---|
| 20 | static constexpr bool compilable = true; | 
|---|
| 21 |  | 
|---|
| 22 | static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool is_signed) | 
|---|
| 23 | { | 
|---|
| 24 | if (!left->getType()->isIntegerTy()) | 
|---|
| 25 | /// XXX maxnum is basically fmax(), it may or may not match whatever apply() does | 
|---|
| 26 | /// XXX CreateMaxNum is broken on LLVM 5.0 and 6.0 (generates minnum instead; fixed in 7) | 
|---|
| 27 | return b.CreateBinaryIntrinsic(llvm::Intrinsic::maxnum, left, right); | 
|---|
| 28 | return b.CreateSelect(is_signed ? b.CreateICmpSGT(left, right) : b.CreateICmpUGT(left, right), left, right); | 
|---|
| 29 | } | 
|---|
| 30 | #endif | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | template <typename A, typename B> | 
|---|
| 34 | struct GreatestSpecialImpl | 
|---|
| 35 | { | 
|---|
| 36 | using ResultType = std::make_unsigned_t<A>; | 
|---|
| 37 |  | 
|---|
| 38 | template <typename Result = ResultType> | 
|---|
| 39 | static inline Result apply(A a, B b) | 
|---|
| 40 | { | 
|---|
| 41 | static_assert(std::is_same_v<Result, ResultType>, "ResultType != Result"); | 
|---|
| 42 | return accurate::greaterOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | #if USE_EMBEDDED_COMPILER | 
|---|
| 46 | static constexpr bool compilable = false; /// ??? | 
|---|
| 47 | #endif | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | template <typename A, typename B> | 
|---|
| 51 | using GreatestImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>, GreatestBaseImpl<A, B>, GreatestSpecialImpl<A, B>>; | 
|---|
| 52 |  | 
|---|
| 53 | struct NameGreatest { static constexpr auto name = "greatest"; }; | 
|---|
| 54 | using FunctionGreatest = FunctionBinaryArithmetic<GreatestImpl, NameGreatest>; | 
|---|
| 55 |  | 
|---|
| 56 | void registerFunctionGreatest(FunctionFactory & factory) | 
|---|
| 57 | { | 
|---|
| 58 | factory.registerFunction<FunctionGreatest>(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|