aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/minikql/invoke_builtins/mkql_builtins_abs.cpp
blob: ba308a35c67749cfa5313283507c48696833c239 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "mkql_builtins_decimal.h"

#include <cmath>

namespace NKikimr {
namespace NMiniKQL {

namespace {
template <typename T, std::enable_if_t<std::is_unsigned<T>::value>* = nullptr>
inline T Abs(T v) {
    return v;
}

template <typename T, std::enable_if_t<std::is_floating_point<T>::value>* = nullptr>
inline T Abs(T v) {
    return std::fabs(v);
}

template <typename T, std::enable_if_t<std::is_signed<T>::value && std::is_integral<T>::value>* = nullptr>
inline T Abs(T v) {
    return std::abs(v);
}

template<typename TInput, typename TOutput>
struct TAbs : public TSimpleArithmeticUnary<TInput, TOutput, TAbs<TInput, TOutput>> {
    static TOutput Do(TInput val)
    {
        return Abs<TInput>(val);
    }

#ifndef MKQL_DISABLE_CODEGEN
    static Value* Gen(Value* arg, const TCodegenContext& ctx, BasicBlock*& block)
    {
        if (std::is_unsigned<TInput>())
            return arg;

        if (std::is_floating_point<TInput>()) {
            auto& module = ctx.Codegen->GetModule();
            const auto fnType = FunctionType::get(arg->getType(), {arg->getType()}, false);
            const auto& name = GetFuncNameForType<TInput>("llvm.fabs");
            const auto func = module.getOrInsertFunction(name, fnType).getCallee();
            const auto res = CallInst::Create(func, {arg}, "fabs", block);
            return res;
        } else {
            const auto zero = ConstantInt::get(arg->getType(), 0);
            const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, arg, zero, "check", block);
            const auto neg = BinaryOperator::CreateNeg(arg, "neg", block);
            const auto res = SelectInst::Create(check, neg, arg, "result", block);
            return res;
        }
    }
#endif
};

struct TDecimalAbs {
    static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& arg) {
        const auto a = arg.GetInt128();
        return a < 0 ? NUdf::TUnboxedValuePod(-a) : arg;
    }

#ifndef MKQL_DISABLE_CODEGEN
    static Value* Generate(Value* arg, const TCodegenContext&, BasicBlock*& block)
    {
        const auto val = GetterForInt128(arg, block);
        const auto zero = ConstantInt::get(val->getType(), 0);
        const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, zero, "check", block);
        const auto neg = BinaryOperator::CreateNeg(val, "neg", block);
        const auto res = SelectInst::Create(check, SetterForInt128(neg, block), arg, "result", block);
        return res;
    }
#endif
};

}

void RegisterAbs(IBuiltinFunctionRegistry& registry) {
    RegisterUnaryNumericFunctionOpt<TAbs, TUnaryArgsOpt>(registry, "Abs");
    RegisterFunctionUnOpt<NUdf::TDataType<NUdf::TInterval>, NUdf::TDataType<NUdf::TInterval>, TAbs, TUnaryArgsOpt>(registry, "Abs");
    NDecimal::RegisterUnaryFunction<TDecimalAbs, TUnaryArgsOpt>(registry, "Abs");
}

} // namespace NMiniKQL
} // namespace NKikimr