From a5530ea9edb89814b41dc0d3dfb56785637d756c Mon Sep 17 00:00:00 2001 From: alexp616 Date: Fri, 4 Apr 2025 02:17:45 -0700 Subject: [PATCH 1/5] Fixed incorrect results for invmod(<:Signed, <:Unsigned) --- base/intfuncs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index fccd8fcb9accc..f56b739ff1b0e 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -286,7 +286,7 @@ function invmod(n::Integer, m::Integer) g, x, y = gcdx(n, m) g != 1 && throw(DomainError((n, m), LazyString("Greatest common divisor is ", g, "."))) # Note that m might be negative here. - if n isa Unsigned && hastypemax(typeof(n)) && x > typemax(n)>>1 + if promote_type(typeof(n), typeof(m))<:Unsigned && hastypemax(typeof(n)) && x > typemax(n)>>1 # x might have wrapped if it would have been negative # adding back m forces a correction x += m From 77333317280d90ee779562d57197664259999886 Mon Sep 17 00:00:00 2001 From: alexp616 Date: Fri, 4 Apr 2025 02:18:00 -0700 Subject: [PATCH 2/5] Added test cases for invmod(<:Signed, <:Unsigned) --- test/intfuncs.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index adcfee2a050dd..81fb496a62df3 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -258,6 +258,10 @@ end @test invmod(T(3), T(124))::T == 83 end + for T in (Int8, Int16, Int32, Int64, Int128) + @test invmod(T(3), unsigned(T)(124)) == 83 + end + for T in (Int8, UInt8) for x in typemin(T):typemax(T) for m in typemin(T):typemax(T) From 5982b932e920540c0e704131477f9d7c1d178463 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 8 May 2025 02:10:43 +0200 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Lilith Orion Hafner --- base/intfuncs.jl | 2 +- test/intfuncs.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index f56b739ff1b0e..cf5bef0ea502e 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -286,7 +286,7 @@ function invmod(n::Integer, m::Integer) g, x, y = gcdx(n, m) g != 1 && throw(DomainError((n, m), LazyString("Greatest common divisor is ", g, "."))) # Note that m might be negative here. - if promote_type(typeof(n), typeof(m))<:Unsigned && hastypemax(typeof(n)) && x > typemax(n)>>1 + if x isa Unsigned && hastypemax(typeof(x)) && x > typemax(x)>>1 # x might have wrapped if it would have been negative # adding back m forces a correction x += m diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 81fb496a62df3..3c0518bcafcf7 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -261,6 +261,7 @@ end for T in (Int8, Int16, Int32, Int64, Int128) @test invmod(T(3), unsigned(T)(124)) == 83 end +@test invmod(UInt8(3), UInt16(50000)) === 0x411b for T in (Int8, UInt8) for x in typemin(T):typemax(T) From dd652f475d475b00ed803e2f01d9a6bada8eac28 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 8 May 2025 02:11:37 +0200 Subject: [PATCH 4/5] Update test/intfuncs.jl --- test/intfuncs.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 3c0518bcafcf7..c15cc81101128 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -261,7 +261,9 @@ end for T in (Int8, Int16, Int32, Int64, Int128) @test invmod(T(3), unsigned(T)(124)) == 83 end -@test invmod(UInt8(3), UInt16(50000)) === 0x411b + + # For issue 58010 + @test invmod(UInt8(3), UInt16(50000)) === 0x411b for T in (Int8, UInt8) for x in typemin(T):typemax(T) From d17424e2f93cadef86693ab94e1667504e41855d Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 8 May 2025 02:12:30 +0200 Subject: [PATCH 5/5] Update test/intfuncs.jl --- test/intfuncs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index c15cc81101128..d100dd0817f44 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -262,8 +262,8 @@ end @test invmod(T(3), unsigned(T)(124)) == 83 end - # For issue 58010 - @test invmod(UInt8(3), UInt16(50000)) === 0x411b + # Verify issue described in PR 58010 is fixed + @test invmod(UInt8(3), UInt16(50000)) === 0x411b for T in (Int8, UInt8) for x in typemin(T):typemax(T)