Skip to content

Commit 96a70e3

Browse files
committed
improve concrete-foldability of intfuncs
1 parent 2b48e8e commit 96a70e3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

base/intfuncs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ julia> gcdx(240, 46)
183183
their `typemax`, and the identity then holds only via the unsigned
184184
integers' modulo arithmetic.
185185
"""
186-
function gcdx(a::Integer, b::Integer)
186+
Base.@assume_effects :terminates_locally function gcdx(a::Integer, b::Integer)
187187
T = promote_type(typeof(a), typeof(b))
188188
# a0, b0 = a, b
189189
s0, s1 = oneunit(T), zero(T)
@@ -233,7 +233,7 @@ function invmod(n::Integer, m::Integer)
233233
n == typeof(n)(-1) && m == typemin(typeof(n)) && return T(-1)
234234
end
235235
g, x, y = gcdx(n, m)
236-
g != 1 && throw(DomainError((n, m), "Greatest common divisor is $g."))
236+
g != 1 && throw(DomainError((n, m), LazyString("Greatest common divisor is ", g, ".")))
237237
# Note that m might be negative here.
238238
if n isa Unsigned && hastypemax(typeof(n)) && x > typemax(n)>>1
239239
# x might have wrapped if it would have been negative
@@ -1057,7 +1057,7 @@ julia> binomial(-5, 3)
10571057
# External links
10581058
* [Binomial coefficient](https://en.wikipedia.org/wiki/Binomial_coefficient) on Wikipedia.
10591059
"""
1060-
function binomial(n::T, k::T) where T<:Integer
1060+
Base.@assume_effects :terminates_locally function binomial(n::T, k::T) where T<:Integer
10611061
n0, k0 = n, k
10621062
k < 0 && return zero(T)
10631063
sgn = one(T)
@@ -1079,7 +1079,7 @@ function binomial(n::T, k::T) where T<:Integer
10791079
while rr <= k
10801080
xt = div(widemul(x, nn), rr)
10811081
x = xt % T
1082-
x == xt || throw(OverflowError("binomial($n0, $k0) overflows"))
1082+
x == xt || throw(OverflowError(LazyString("binomial(", n0, ", ", k0, " overflows")))
10831083
rr += one(T)
10841084
nn += one(T)
10851085
end

test/intfuncs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,9 @@ end
519519
@test binomial(x...) == (x != (false,true))
520520
end
521521
end
522+
523+
# concrete-foldability
524+
@test Base.infer_effects(gcd, (Int,Int)) |> Core.Compiler.is_foldable
525+
@test Base.infer_effects(gcdx, (Int,Int)) |> Core.Compiler.is_foldable
526+
@test Base.infer_effects(invmod, (Int,Int)) |> Core.Compiler.is_foldable
527+
@test Base.infer_effects(binomial, (Int,Int)) |> Core.Compiler.is_foldable

0 commit comments

Comments
 (0)