Skip to content

Commit f0424cd

Browse files
authored
Merge pull request #119 from hzqst/dev
intrinsic "_udiv128" Impl
2 parents 16d5607 + a2d6a83 commit f0424cd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: clang/include/clang/Basic/BuiltinsX86_64.def

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ TARGET_HEADER_BUILTIN(__mulh, "LLiLLiLLi", "nch", INTRIN_H, ALL_MS_LANGUAGES
2828
TARGET_HEADER_BUILTIN(__umulh, "ULLiULLiULLi", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
2929
TARGET_HEADER_BUILTIN(_mul128, "LLiLLiLLiLLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
3030
TARGET_HEADER_BUILTIN(_umul128, "ULLiULLiULLiULLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
31+
TARGET_HEADER_BUILTIN(_udiv128, "ULLiULLiULLiULLiULLi*", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")
3132

3233
TARGET_HEADER_BUILTIN(__faststorefence, "v", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
3334
TARGET_HEADER_BUILTIN(__shiftleft128, "ULLiULLiULLiUc", "nch", INTRIN_H, ALL_MS_LANGUAGES, "")

Diff for: clang/lib/CodeGen/CGBuiltin.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -16024,6 +16024,37 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
1602416024
return Builder.CreateIntCast(MulResult, ResType, IsSigned);
1602516025
}
1602616026

16027+
case X86::BI_udiv128: {
16028+
llvm::Type *ResType = ConvertType(E->getType());
16029+
llvm::Type *Int128Ty = llvm::IntegerType::get(getLLVMContext(), 128);
16030+
llvm::Type *Int64Ty = llvm::IntegerType::get(getLLVMContext(), 64);
16031+
16032+
// Retrieve operands
16033+
Value *HighDividend = Builder.CreateIntCast(Ops[0], Int128Ty, false);
16034+
Value *LowDividend = Builder.CreateZExt(Ops[1], Int128Ty);
16035+
Value *Divisor = Ops[2];
16036+
16037+
// Combine HighDividend and LowDividend into a 128-bit dividend
16038+
Value *Dividend = Builder.CreateShl(HighDividend, 64);
16039+
Dividend = Builder.CreateOr(Dividend, LowDividend);
16040+
16041+
// Create Divisor and extend it to 128-bit
16042+
Value *DivisorExt = Builder.CreateZExt(Divisor, Int128Ty);
16043+
16044+
// Perform division
16045+
Value *Quotient = Builder.CreateUDiv(Dividend, DivisorExt);
16046+
Value *Remainder = Builder.CreateURem(Dividend, DivisorExt);
16047+
16048+
// Truncate results to 64 bits
16049+
Quotient = Builder.CreateTrunc(Quotient, Int64Ty);
16050+
Remainder = Builder.CreateTrunc(Remainder, Int64Ty);
16051+
16052+
// Store remainder
16053+
Address RemainderAddr = EmitPointerWithAlignment(E->getArg(3));
16054+
Builder.CreateStore(Remainder, RemainderAddr);
16055+
16056+
return Quotient; // Return the quotient as the result
16057+
}
1602716058
case X86::BI__faststorefence: {
1602816059
return Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent,
1602916060
llvm::SyncScope::System);

0 commit comments

Comments
 (0)