mkql_builtins_dec.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 TDecrement : public TSimpleArithmeticUnary<TInput, TOutput, TDecrement<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::CreateSub(arg, ConstantInt::get(arg->getType(), 1), "dec", block):
  16. BinaryOperator::CreateFSub(arg, ConstantFP::get(arg->getType(), 1.0), "dec", block);
  17. }
  18. #endif
  19. };
  20. template <ui8 Precision>
  21. struct TDecimalDec {
  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, true, false>();
  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, true, false>(context);
  35. const auto val = GetterForInt128(arg, block);
  36. const auto sub = BinaryOperator::CreateSub(val, ConstantInt::get(val->getType(), 1), "sub", block);
  37. const auto gt = CmpInst::Create(Instruction::ICmp, FCmpInst::ICMP_SGT, sub, bounds.first, "gt", block);
  38. const auto lt = CmpInst::Create(Instruction::ICmp, FCmpInst::ICMP_SLT, val, 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 dec = SelectInst::Create(good, sub, bad, "dec", block);
  45. return SetterForInt128(dec, block);
  46. }
  47. #endif
  48. static_assert(Precision <= NYql::NDecimal::MaxPrecision, "Too large precision!");
  49. };
  50. }
  51. void RegisterDecrement(IBuiltinFunctionRegistry& registry) {
  52. RegisterUnaryNumericFunctionOpt<TDecrement, TUnaryArgsOpt>(registry, "Decrement");
  53. NDecimal::RegisterUnaryFunctionForAllPrecisions<TDecimalDec, TUnaryArgsOpt>(registry, "Dec_");
  54. }
  55. } // namespace NMiniKQL
  56. } // namespace NKikimr