From acd37a322d793b47c4a9d06408153e042aadc704 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Tue, 31 Dec 2024 14:06:39 +0800 Subject: [PATCH 1/2] Update BuiltinsX86_64.def ``` __MACHINEX64(unsigned __int64 __cdecl _udiv128(unsigned __int64 _HighDividend, unsigned __int64 _LowDividend, unsigned __int64 _Divisor, unsigned __int64* _Remainder)) ``` or ``` extern unsigned __int64 __cdecl _udiv128(unsigned __int64 /* highdividend */, unsigned __int64 /* lowdividend */, unsigned __int64 /* divisor */, unsigned __int64* /* remainder */); ``` --- clang/include/clang/Basic/BuiltinsX86_64.def | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/include/clang/Basic/BuiltinsX86_64.def b/clang/include/clang/Basic/BuiltinsX86_64.def index 5e00916d4b25..d0637a75a07a 100644 --- a/clang/include/clang/Basic/BuiltinsX86_64.def +++ b/clang/include/clang/Basic/BuiltinsX86_64.def @@ -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, "") From a2d6a835feec58a41e7af77bf1357963b4b830f9 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Tue, 31 Dec 2024 14:08:11 +0800 Subject: [PATCH 2/2] Update CGBuiltin.cpp for _udiv128 not tested yet though --- clang/lib/CodeGen/CGBuiltin.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index c4cad097e799..25ac8d078975 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -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);