[libc-commits] [libc] [llvm] [llvm] Add KnownBits implementations for avgFloor and avgCeil (PR #86445)

Jay Foad via libc-commits libc-commits at lists.llvm.org
Wed May 15 03:05:08 PDT 2024


================
@@ -774,6 +774,62 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
+KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
+  KnownBits Known = LHS;
+  KnownBits Known2 = RHS;
+  bool IsCeil = false;
+  bool IsSigned = true;
+  unsigned BitWidth = Known.getBitWidth();
+  Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
+  Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+  KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
+  Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
+  Known = Known.extractBits(BitWidth, 1);
----------------
jayfoad wrote:

Put this in a helper function like `KnownBits avg(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil, bool IsSigned)`? It is weird to have this code duplicated four times.

https://github.com/llvm/llvm-project/pull/86445


More information about the libc-commits mailing list