Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make gcd use divrem #45456

Merged
merged 1 commit into from
Jun 8, 2022
Merged

make gcd use divrem #45456

merged 1 commit into from
Jun 8, 2022

Conversation

oscardssmith
Copy link
Member

No description provided.

@oscardssmith oscardssmith added the domain:maths Mathematical functions label May 25, 2022
@ianatol
Copy link
Member

ianatol commented May 25, 2022

I don't have much context, but should we do something similar here?

julia/base/intfuncs.jl

Lines 769 to 770 in 209a7d9

a[i] = digits[1 + (rem(x, b) % Int)::Int]
x = div(x,b)

@oscardssmith
Copy link
Member Author

oscardssmith commented May 25, 2022

We can actually do better there. Since there are only 60 possible divisors, we could use one of the fancy division by a constant integer algorithms (see https://en.algorithmica.org/hpc/arithmetic/division/)

@oscardssmith
Copy link
Member Author

oscardssmith commented May 25, 2022

And because Julia is awesome, this is just

function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
    (x >= 0) | (base < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
    2 <= abs(base) <= 62 || throw(DomainError(base, "base must satisfy 2 ≤ abs(base) ≤ 62"))
    b = (base % Int)::Int
    b = Base.MultiplicativeInverses.multiplicativeinverse(b)
    digits = abs(b) <= 36 ? base36digits : base62digits
    n = neg + ndigits(x, base=b, pad=pad)
    a = StringVector(n)
    i = n
    @inbounds while i > neg
        if b > 0
            q,r = divrem(x,b)
            a[i] = digits[1 + (r % Int)::Int]
            x = q
        else
            a[i] = digits[1 + (mod(x, -b) % Int)::Int]
            x = cld(x,b)
        end
        i -= 1
    end
    if neg; @inbounds a[1]=0x2d; end
    String(a)
end

@oscardssmith
Copy link
Member Author

merging tomorrow sans objections.

@oscardssmith oscardssmith merged commit 7e53436 into master Jun 8, 2022
@oscardssmith oscardssmith deleted the oscardssmith-make-gcd-use-divrem branch June 8, 2022 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants