mkql_builtins_decimal.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "mkql_builtins_decimal.h" // Y_IGNORE
  2. namespace NKikimr {
  3. namespace NMiniKQL {
  4. namespace NDecimal {
  5. #ifndef MKQL_DISABLE_CODEGEN
  6. ConstantInt* GenConstant(NYql::NDecimal::TInt128 value, LLVMContext &context) {
  7. const auto& pair = NYql::NDecimal::MakePair(value);
  8. const uint64_t init[] = {pair.first, pair.second};
  9. return ConstantInt::get(context, APInt(128, 2, init));
  10. }
  11. template<bool IncludeBounds>
  12. Value* GenInBounds(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block) {
  13. const auto lt = CmpInst::Create(Instruction::ICmp, IncludeBounds ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, val, high, "lt", block);
  14. const auto gt = CmpInst::Create(Instruction::ICmp, IncludeBounds ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, val, low, "gt", block);
  15. const auto good = BinaryOperator::CreateAnd(lt, gt, "and", block);
  16. return good;
  17. }
  18. template<bool IncludeBounds>
  19. Value* GenOutOfBounds(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block) {
  20. const auto lt = CmpInst::Create(Instruction::ICmp, IncludeBounds ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, val, low, "lt", block);
  21. const auto gt = CmpInst::Create(Instruction::ICmp, IncludeBounds ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, val, high, "gt", block);
  22. const auto bad = BinaryOperator::CreateOr(lt, gt, "or", block);
  23. return bad;
  24. }
  25. template Value* GenInBounds<true>(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  26. template Value* GenInBounds<false>(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  27. template Value* GenOutOfBounds<true>(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  28. template Value* GenOutOfBounds<false>(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  29. Value* GenIsError(Value* val, LLVMContext &context, BasicBlock* block) {
  30. const auto gt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, val, GetDecimalNan(context), "gt", block);
  31. const auto lt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, GetDecimalMinusNan(context), "lt", block);
  32. const auto bad = BinaryOperator::CreateOr(lt, gt, "or", block);
  33. return bad;
  34. }
  35. Value* GenIsNormal(Value* val, LLVMContext &context, BasicBlock* block) {
  36. const auto lt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, GetDecimalPlusInf(context), "lt", block);
  37. const auto gt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, val, GetDecimalMinusInf(context), "gt", block);
  38. const auto good = BinaryOperator::CreateAnd(lt, gt, "and", block);
  39. return good;
  40. }
  41. Value* GenIsAbnormal(Value* val, LLVMContext &context, BasicBlock* block) {
  42. const auto le = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, val, GetDecimalMinusInf(context), "le", block);
  43. const auto ge = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, val, GetDecimalPlusInf(context), "ge", block);
  44. const auto bad = BinaryOperator::CreateOr(le, ge, "or", block);
  45. return bad;
  46. }
  47. Value* GenIsComparable(Value* val, LLVMContext &context, BasicBlock* block) {
  48. const auto le = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, val, GetDecimalPlusInf(context), "le", block);
  49. const auto ge = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, val, GetDecimalMinusInf(context), "ge", block);
  50. const auto good = BinaryOperator::CreateAnd(le, ge, "and", block);
  51. return good;
  52. }
  53. Value* GenIsNonComparable(Value* val, LLVMContext &context, BasicBlock* block) {
  54. const auto gt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, val, GetDecimalPlusInf(context), "gt", block);
  55. const auto lt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, GetDecimalMinusInf(context), "lt", block);
  56. const auto bad = BinaryOperator::CreateOr(gt, lt, "or", block);
  57. return bad;
  58. }
  59. #endif
  60. }
  61. }
  62. }