Skip to content

intrinsic "_udiv128" Impl #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/BuiltinsX86_64.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TARGET_HEADER_BUILTIN(__mulh, "LLiLLiLLi", "nch", INTRIN_H, ALL_MS_LANGUAGES
TARGET_HEADER_BUILTIN(__umulh, "ULLiULLiULLi", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
TARGET_HEADER_BUILTIN(_mul128, "LLiLLiLLiLLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
TARGET_HEADER_BUILTIN(_umul128, "ULLiULLiULLiULLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
TARGET_HEADER_BUILTIN(_udiv128, "ULLiULLiULLiULLiULLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")

TARGET_HEADER_BUILTIN(__faststorefence, "v", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
TARGET_HEADER_BUILTIN(__shiftleft128, "ULLiULLiULLiUc", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
Expand Down
31 changes: 31 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16024,6 +16024,37 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
return Builder.CreateIntCast(MulResult, ResType, IsSigned);
}

case X86::BI_udiv128: {
llvm::Type *ResType = ConvertType(E->getType());
llvm::Type *Int128Ty = llvm::IntegerType::get(getLLVMContext(), 128);
llvm::Type *Int64Ty = llvm::IntegerType::get(getLLVMContext(), 64);

// Retrieve operands
Value *HighDividend = Builder.CreateIntCast(Ops[0], Int128Ty, false);
Value *LowDividend = Builder.CreateZExt(Ops[1], Int128Ty);
Value *Divisor = Ops[2];

// Combine HighDividend and LowDividend into a 128-bit dividend
Value *Dividend = Builder.CreateShl(HighDividend, 64);
Dividend = Builder.CreateOr(Dividend, LowDividend);

// Create Divisor and extend it to 128-bit
Value *DivisorExt = Builder.CreateZExt(Divisor, Int128Ty);

// Perform division
Value *Quotient = Builder.CreateUDiv(Dividend, DivisorExt);
Value *Remainder = Builder.CreateURem(Dividend, DivisorExt);

// Truncate results to 64 bits
Quotient = Builder.CreateTrunc(Quotient, Int64Ty);
Remainder = Builder.CreateTrunc(Remainder, Int64Ty);

// Store remainder
Address RemainderAddr = EmitPointerWithAlignment(E->getArg(3));
Builder.CreateStore(Remainder, RemainderAddr);

return Quotient; // Return the quotient as the result
}
case X86::BI__faststorefence: {
return Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent,
llvm::SyncScope::System);
Expand Down
Loading