[llvm] [APInt] Introduce signed-gcd APIntOps (PR #217269)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 04:04:33 PDT 2026


================
@@ -865,6 +865,17 @@ APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) {
   return A;
 }
 
+APInt llvm::APIntOps::SGreatestCommonDivisor(APInt A, APInt B) {
+  if (A.isNegative()) {
+    if (B.isNegative())
+      return GreatestCommonDivisor(-A, -B);
+    return -GreatestCommonDivisor(-A, B);
+  }
+  if (B.isNegative())
+    return -GreatestCommonDivisor(A, -B);
+  return GreatestCommonDivisor(A, B);
----------------
nikic wrote:

```suggestion
  return GreatestCommonDivisor(A, B.abs());
```

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


More information about the llvm-commits mailing list