Skip to content

Commit fcaad75

Browse files
fixup! [libc++] Fix sub-overflow in std::gcd implementation
1 parent 1f04c92 commit fcaad75

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

libcxx/include/__numeric/gcd_lcm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ template <class _Tp>
5555
constexpr _LIBCPP_HIDDEN _Tp __gcd(_Tp __a, _Tp __b) {
5656
static_assert(!is_signed<_Tp>::value, "");
5757

58-
// From: https://lemire.me/blog/2024/04/13/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
58+
// Using Binary GCD algorithm https://en.wikipedia.org/wiki/Binary_GCD_algorithm, based on an implementation
59+
// from https://lemire.me/blog/2024/04/13/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
5960
//
6061
// If power of two divides both numbers, we can push it out.
6162
// - gcd( 2^x * a, 2^x * b) = 2^x * gcd(a, b)

libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,7 @@ constexpr struct {
2727
int x;
2828
int y;
2929
int expect;
30-
} Cases[] = {
31-
{0, 0, 0},
32-
{1, 0, 1},
33-
{0, 1, 1},
34-
{1, 1, 1},
35-
{2, 3, 1},
36-
{2, 4, 2},
37-
{36, 17, 1},
38-
{36, 18, 18}
39-
};
40-
30+
} Cases[] = {{0, 0, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, {2, 3, 1}, {2, 4, 2}, {11, 9, 1}, {36, 17, 1}, {36, 18, 18}};
4131

4232
template <typename Input1, typename Input2, typename Output>
4333
constexpr bool test0(int in1, int in2, int out)

0 commit comments

Comments
 (0)