Skip to content

[APInt] Implement average functions without sign/zero-extension. NFC. #85212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2024

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Mar 14, 2024

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Mar 14, 2024

@llvm/pr-subscribers-llvm-support

Author: Jay Foad (jayfoad)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/85212.diff

1 Files Affected:

  • (modified) llvm/lib/Support/APInt.cpp (+8-24)
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 7053f3b87682f8..371d42b221ed62 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3096,37 +3096,21 @@ void llvm::LoadIntFromMemory(APInt &IntVal, const uint8_t *Src,
 }
 
 APInt APIntOps::avgFloorS(const APInt &C1, const APInt &C2) {
-  // Return floor((C1 + C2)/2)
-  assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
-  unsigned FullWidth = C1.getBitWidth() + 1;
-  APInt C1Ext = C1.sext(FullWidth);
-  APInt C2Ext = C2.sext(FullWidth);
-  return (C1Ext + C2Ext).extractBits(C1.getBitWidth(), 1);
+  // Return floor((C1 + C2) / 2)
+  return (C1 & C2) + (C1 ^ C2).ashr(1);
 }
 
 APInt APIntOps::avgFloorU(const APInt &C1, const APInt &C2) {
-  // Return floor((C1 + C2)/2)
-  assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
-  unsigned FullWidth = C1.getBitWidth() + 1;
-  APInt C1Ext = C1.zext(FullWidth);
-  APInt C2Ext = C2.zext(FullWidth);
-  return (C1Ext + C2Ext).extractBits(C1.getBitWidth(), 1);
+  // Return floor((C1 + C2) / 2)
+  return (C1 & C2) + (C1 ^ C2).lshr(1);
 }
 
 APInt APIntOps::avgCeilS(const APInt &C1, const APInt &C2) {
-  // Return ceil((C1 + C2)/2)
-  assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
-  unsigned FullWidth = C1.getBitWidth() + 1;
-  APInt C1Ext = C1.sext(FullWidth);
-  APInt C2Ext = C2.sext(FullWidth);
-  return (C1Ext + C2Ext + 1).extractBits(C1.getBitWidth(), 1);
+  // Return ceil((C1 + C2) / 2)
+  return (C1 | C2) - (C1 ^ C2).ashr(1);
 }
 
 APInt APIntOps::avgCeilU(const APInt &C1, const APInt &C2) {
-  // Return ceil((C1 + C2)/2)
-  assert(C1.getBitWidth() == C2.getBitWidth() && "Unequal bitwidths");
-  unsigned FullWidth = C1.getBitWidth() + 1;
-  APInt C1Ext = C1.zext(FullWidth);
-  APInt C2Ext = C2.zext(FullWidth);
-  return (C1Ext + C2Ext + 1).extractBits(C1.getBitWidth(), 1);
+  // Return ceil((C1 + C2) / 2)
+  return (C1 | C2) - (C1 ^ C2).ashr(1);
 }

@jayfoad jayfoad requested review from RKSimon and kuhar March 14, 2024 11:58
@jayfoad
Copy link
Contributor Author

jayfoad commented Mar 14, 2024

  • @Atousa (for some reason I cannot add you as a reviewer).

@RKSimon
Copy link
Collaborator

RKSimon commented Mar 14, 2024

  • @Atousa (for some reason I cannot add you as a reviewer).

Only people with commit access can be tagged as a reviewer (and can accept PRs) - but comments on the patch are still encouraged

@RKSimon
Copy link
Collaborator

RKSimon commented Mar 14, 2024

Can you update the title to be more descriptive? "Avoid extension by using overflow-free variants, something something"?

@jayfoad jayfoad changed the title [APInt] Simplify APIntOps::avgFloorS etc. NFC. [APInt] Implement average functions without sign/zero-extension. NFC. Mar 14, 2024
@Atousa
Copy link
Contributor

Atousa commented Mar 14, 2024

  • @Atousa (for some reason I cannot add you as a reviewer).

Only people with commit access can be tagged as a reviewer (and can accept PRs) - but comments on the patch are still encouraged

@RKSimon Not sure if I have commit access or not yet?

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - cheers

@jayfoad jayfoad merged commit 4e232ca into llvm:main Mar 14, 2024
@jayfoad jayfoad deleted the simplify-avg branch March 14, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants