[libcxx-commits] [libcxx] [libc++] Implement std::gcd using the binary version (PR #77747)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 12 02:48:57 PST 2024


================
@@ -48,6 +49,68 @@ constexpr bool test0(int in1, int in2, int out)
     return true;
 }
 
+template <typename T>
+T basic_gcd_(T m, T n) {
+  return n == 0 ? m : basic_gcd_<T>(n, m % n);
+}
+
+template <typename T>
+T basic_gcd(T m, T n) {
+  using Tp = std::make_unsigned_t<T>;
+  if (m < 0 && m != std::numeric_limits<T>::min())
+    m = -m;
+  if (n < 0 && n != std::numeric_limits<T>::min())
+    n = -n;
----------------
AdvenamTacet wrote:

Old `gcd` implementation had no ifs, from what I can see. I think, we should test if the new implementation is 1:1 with the previous one. Or if we change behavior, we should state it explicitly in PR with reasoning. In that case, a comment with explanation would be good here.

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


More information about the libcxx-commits mailing list