Skip to content

Commit a2d6a83

Browse files
authored
Update CGBuiltin.cpp for _udiv128
not tested yet though
1 parent acd37a3 commit a2d6a83

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 31 additions & 0 deletions
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)