mkql_builtins_inc.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "mkql_builtins_decimal.h" // Y_IGNORE
  2. namespace NKikimr {
  3. namespace NMiniKQL {
  4. namespace {
  5. template<typename TInput, typename TOutput>
  6. struct TIncrement : public TSimpleArithmeticUnary<TInput, TOutput, TIncrement<TInput, TOutput>> {
  7. static TOutput Do(TInput val)
  8. {
  9. return ++val;
  10. }
  11. #ifndef MKQL_DISABLE_CODEGEN
  12. static Value* Gen(Value* arg, const TCodegenContext&, BasicBlock*& block)
  13. {
  14. return std::is_integral<TOutput>() ?
  15. BinaryOperator::CreateAdd(arg, ConstantInt::get(arg->getType(), 1), "inc", block):
  16. BinaryOperator::CreateFAdd(arg, ConstantFP::get(arg->getType(), 1.0), "inc", block);
  17. }
  18. #endif
  19. };
  20. template <ui8 Precision>
  21. struct TDecimalInc {
  22. static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& arg) {
  23. auto v = arg.GetInt128();
  24. using namespace NYql::NDecimal;
  25. const auto& bounds = GetBounds<Precision, false, true>();
  26. if (v > bounds.first && v < bounds.second)
  27. return NUdf::TUnboxedValuePod(++v);
  28. return NUdf::TUnboxedValuePod(IsNan(v) ? Nan() : (v > 0 ? +Inf() : -Inf()));
  29. }
  30. #ifndef MKQL_DISABLE_CODEGEN
  31. static Value* Generate(Value* arg, const TCodegenContext& ctx, BasicBlock*& block)
  32. {
  33. auto& context = ctx.Codegen.GetContext();
  34. const auto& bounds = NDecimal::GenBounds<Precision, false, true>(context);
  35. const auto val = GetterForInt128(arg, block);
  36. const auto add = BinaryOperator::CreateAdd(val, ConstantInt::get(val->getType(), 1), "add", block);
  37. const auto gt = CmpInst::Create(Instruction::ICmp, FCmpInst::ICMP_SGT, val, bounds.first, "gt", block);
  38. const auto lt = CmpInst::Create(Instruction::ICmp, FCmpInst::ICMP_SLT, add, bounds.second, "lt", block);
  39. const auto good = BinaryOperator::CreateAnd(lt, gt, "and", block);
  40. const auto nan = NDecimal::GenIsNonComparable(val, context, block);
  41. const auto plus = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, val, ConstantInt::get(val->getType(), 0), "plus", block);
  42. const auto inf = SelectInst::Create(plus, GetDecimalPlusInf(context), GetDecimalMinusInf(context), "inf", block);
  43. const auto bad = SelectInst::Create(nan, GetDecimalNan(context), inf, "bad", block);
  44. const auto inc = SelectInst::Create(good, add, bad, "inc", block);
  45. return SetterForInt128(inc, block);
  46. }
  47. #endif
  48. static_assert(Precision <= NYql::NDecimal::MaxPrecision, "Too large precision!");
  49. };
  50. }
  51. void RegisterIncrement(IBuiltinFunctionRegistry& registry) {
  52. RegisterUnaryNumericFunctionOpt<TIncrement, TUnaryArgsOpt>(registry, "Increment");
  53. NDecimal::RegisterUnaryFunctionForAllPrecisions<TDecimalInc, TUnaryArgsOpt>(registry, "Inc_");
  54. }
  55. } // namespace NMiniKQL
  56. } // namespace NKikimr