Skip to content

Commit 4e232ca

Browse files
authored
[APInt] Implement average functions without sign/zero-extension. NFC. (#85212)
Removing the extension to FullWidth should make them much more efficient in the 64-bit case, because 65-bit APInts use a separate allocation for their bits.
1 parent 15788e8 commit 4e232ca

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

llvm/lib/Support/APInt.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,37 +3096,21 @@ void llvm::LoadIntFromMemory(APInt &IntVal, const uint8_t *Src,
30963096
}
30973097

30983098
APInt APIntOps::avgFloorS(const APInt &C1, const APInt &C2) {
3099-
// Return floor((C1 + C2)/2)
3100-
assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
3101-
unsigned FullWidth = C1.getBitWidth() + 1;
3102-
APInt C1Ext = C1.sext(FullWidth);
3103-
APInt C2Ext = C2.sext(FullWidth);
3104-
return (C1Ext + C2Ext).extractBits(C1.getBitWidth(), 1);
3099+
// Return floor((C1 + C2) / 2)
3100+
return (C1 & C2) + (C1 ^ C2).ashr(1);
31053101
}
31063102

31073103
APInt APIntOps::avgFloorU(const APInt &C1, const APInt &C2) {
3108-
// Return floor((C1 + C2)/2)
3109-
assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
3110-
unsigned FullWidth = C1.getBitWidth() + 1;
3111-
APInt C1Ext = C1.zext(FullWidth);
3112-
APInt C2Ext = C2.zext(FullWidth);
3113-
return (C1Ext + C2Ext).extractBits(C1.getBitWidth(), 1);
3104+
// Return floor((C1 + C2) / 2)
3105+
return (C1 & C2) + (C1 ^ C2).lshr(1);
31143106
}
31153107

31163108
APInt APIntOps::avgCeilS(const APInt &C1, const APInt &C2) {
3117-
// Return ceil((C1 + C2)/2)
3118-
assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
3119-
unsigned FullWidth = C1.getBitWidth() + 1;
3120-
APInt C1Ext = C1.sext(FullWidth);
3121-
APInt C2Ext = C2.sext(FullWidth);
3122-
return (C1Ext + C2Ext + 1).extractBits(C1.getBitWidth(), 1);
3109+
// Return ceil((C1 + C2) / 2)
3110+
return (C1 | C2) - (C1 ^ C2).ashr(1);
31233111
}
31243112

31253113
APInt APIntOps::avgCeilU(const APInt &C1, const APInt &C2) {
3126-
// Return ceil((C1 + C2)/2)
3127-
assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
3128-
unsigned FullWidth = C1.getBitWidth() + 1;
3129-
APInt C1Ext = C1.zext(FullWidth);
3130-
APInt C2Ext = C2.zext(FullWidth);
3131-
return (C1Ext + C2Ext + 1).extractBits(C1.getBitWidth(), 1);
3114+
// Return ceil((C1 + C2) / 2)
3115+
return (C1 | C2) - (C1 ^ C2).lshr(1);
31323116
}

0 commit comments

Comments
 (0)