[libcxx-commits] [libcxx] [libc++] Implement std::gcd using the binary version (PR #77747)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 11 02:31:23 PST 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 9ef2ac3ad1bd5aa9e589f63047e8abeac11ad1b2 798e74b42b85c045edde6473ddcf305b40ca6a62 -- libcxx/include/__bit/countr.h libcxx/include/__numeric/gcd_lcm.h libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libcxx/include/__numeric/gcd_lcm.h b/libcxx/include/__numeric/gcd_lcm.h
index 55199547fa..bcf2fbe688 100644
--- a/libcxx/include/__numeric/gcd_lcm.h
+++ b/libcxx/include/__numeric/gcd_lcm.h
@@ -53,24 +53,24 @@ struct __ct_abs<_Result, _Source, false> {
template <class _Tp>
_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN _Tp __gcd(_Tp __a, _Tp __b) {
- static_assert((!is_signed<_Tp>::value), "");
- if (__a == 0)
- return __b;
- if (__b == 0)
- return __a;
-
- int __az = std::__countr_zero(__a);
- int __bz = std::__countr_zero(__b);
- int __shift = std::min(__az, __bz);
- __b >>= __bz;
- while (__a != 0) {
- __a >>= __az;
- _Tp __absdiff = __a > __b ? __a - __b : __b - __a;
- __b = std::min(__a, __b);
- __a = __absdiff;
- __az = std::__countr_zero(__absdiff);
- }
- return __b << __shift;
+ static_assert((!is_signed<_Tp>::value), "");
+ if (__a == 0)
+ return __b;
+ if (__b == 0)
+ return __a;
+
+ int __az = std::__countr_zero(__a);
+ int __bz = std::__countr_zero(__b);
+ int __shift = std::min(__az, __bz);
+ __b >>= __bz;
+ while (__a != 0) {
+ __a >>= __az;
+ _Tp __absdiff = __a > __b ? __a - __b : __b - __a;
+ __b = std::min(__a, __b);
+ __a = __absdiff;
+ __az = std::__countr_zero(__absdiff);
+ }
+ return __b << __shift;
}
template <class _Tp, class _Up>
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
index 0435575bd8..acf8986eab 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
@@ -56,20 +56,18 @@ T basic_gcd(T m, T n) {
template <typename Input>
void do_fuzzy_tests() {
-
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<Input> distrib;
constexpr int nb_rounds = 10000;
- for(int i = 0; i < nb_rounds; ++i) {
+ for (int i = 0; i < nb_rounds; ++i) {
Input n = distrib(gen);
Input m = distrib(gen);
assert(std::gcd(n, m) == basic_gcd(n, m));
}
}
-
template <typename Input1, typename Input2 = Input1>
constexpr bool do_test(int = 0)
{
@@ -169,5 +167,5 @@ int main(int argc, char**)
do_fuzzy_tests<std::uint32_t>();
do_fuzzy_tests<std::uint64_t>();
- return 0;
+ return 0;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/77747
More information about the libcxx-commits
mailing list