1 | #include <Functions/FunctionFactory.h> |
---|---|
2 | #include <Functions/FunctionUnaryArithmetic.h> |
3 | #include <DataTypes/NumberTraits.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | template <typename A> |
9 | struct NegateImpl |
10 | { |
11 | using ResultType = std::conditional_t<IsDecimalNumber<A>, A, typename NumberTraits::ResultOfNegate<A>::Type>; |
12 | |
13 | static inline NO_SANITIZE_UNDEFINED ResultType apply(A a) |
14 | { |
15 | return -static_cast<ResultType>(a); |
16 | } |
17 | |
18 | #if USE_EMBEDDED_COMPILER |
19 | static constexpr bool compilable = true; |
20 | |
21 | static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * arg, bool) |
22 | { |
23 | return arg->getType()->isIntegerTy() ? b.CreateNeg(arg) : b.CreateFNeg(arg); |
24 | } |
25 | #endif |
26 | }; |
27 | |
28 | struct NameNegate { static constexpr auto name = "negate"; }; |
29 | using FunctionNegate = FunctionUnaryArithmetic<NegateImpl, NameNegate, true>; |
30 | |
31 | template <> struct FunctionUnaryArithmeticMonotonicity<NameNegate> |
32 | { |
33 | static bool has() { return true; } |
34 | static IFunction::Monotonicity get(const Field &, const Field &) |
35 | { |
36 | return { true, false }; |
37 | } |
38 | }; |
39 | |
40 | void registerFunctionNegate(FunctionFactory & factory) |
41 | { |
42 | factory.registerFunction<FunctionNegate>(); |
43 | } |
44 | |
45 | } |
46 |