mkql_builtins_shiftright.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "mkql_builtins_impl.h" // Y_IGNORE
  2. namespace NKikimr {
  3. namespace NMiniKQL {
  4. namespace {
  5. template<typename TInput, typename TOutput>
  6. struct TShiftRight : public TShiftArithmeticBinary<TInput, TOutput, TShiftRight<TInput, TOutput>> {
  7. static TOutput Do(TInput arg, ui8 bits)
  8. {
  9. return (bits < sizeof(arg) * CHAR_BIT) ? (arg >> bits) : 0;
  10. }
  11. #ifndef MKQL_DISABLE_CODEGEN
  12. static Value* Gen(Value* arg, Value* bits, const TCodegenContext&, BasicBlock*& block)
  13. {
  14. const auto zero = ConstantInt::get(arg->getType(), 0);
  15. const auto maxb = ConstantInt::get(bits->getType(), sizeof(TInput) * CHAR_BIT);
  16. const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, bits, maxb, "check", block);
  17. const auto zext = arg->getType() == bits->getType() ? bits : new ZExtInst(bits, arg->getType(), "zext", block);
  18. const auto lshr = BinaryOperator::CreateLShr(arg, zext, "lshr", block);
  19. const auto res = SelectInst::Create(check, lshr, zero, "res", block);
  20. return res;
  21. }
  22. #endif
  23. };
  24. }
  25. void RegisterShiftRight(IBuiltinFunctionRegistry& registry) {
  26. RegisterUnsignedShiftFunctionOpt<TShiftRight, TBinaryShiftArgsOpt>(registry, "ShiftRight");
  27. }
  28. } // namespace NMiniKQL
  29. } // namespace NKikimr