Skip to content

Do not use widen for 128-bit ints in MultiplicativeInverses #47995

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

Merged
merged 4 commits into from
Apr 10, 2023

Conversation

sumiya11
Copy link
Contributor

Hello! This PR improves MultiplicativeInverses for Int128 and UInt128. PR consists of 3 tiny points:

  • Previously, division with multiplicative inverses used widen. It widened 128-bit ints to BigInt, which hindered performance. The solution in this PR is to replace widen with higher half multiplication for Int128 and UInt128.
# 1.8.2
using BenchmarkTools
a, b = rand(UInt128), rand(UInt128)
d = Base.MultiplicativeInverses.UnsignedMultiplicativeInverse(b)

@btime div($a, $d)
  645.455 ns (12 allocations: 216 bytes)
# this pr
using BenchmarkTools
a, b = rand(UInt128), rand(UInt128)
d = Base.MultiplicativeInverses.UnsignedMultiplicativeInverse(b)

@btime div($a, $d)
  6.800 ns (0 allocations: 0 bytes)
  • Added tests for MultiplicativeInverses for all bit integer types;
  • Draw attention to the fact that signed division with MultiplicativeInverses does not throw when the result overflows (see tests)

As for the latter, it's alright with me, but I think it would be good to mention this fact somewhere

Also, I've coded the higher half mult. from scratch, so let me know if there were functions for this in Base already 😊

@brenhinkeller brenhinkeller added the maths Mathematical functions label Dec 27, 2022
@vtjnash vtjnash requested a review from oscardssmith February 10, 2023 19:37
@oscardssmith
Copy link
Member

This is a really nice improvement! I think we should consider making mulhi an exported function (e.g. I wouldn't mind using it in Primes.jl).

@oscardssmith
Copy link
Member

Also, do you happen to know if there's an efficient way to compute a*b%n for Int128 without widening to BigInt?

@sumiya11
Copy link
Contributor Author

Also, do you happen to know if there's an efficient way to compute a*b%n for Int128 without widening to BigInt?

Nope :(

I think we should consider making mulhi an exported function (e.g. I wouldn't mind using it in Primes.jl).

Sounds like a good idea. I would also use it somewhere in fast modular arithmetic

@sumiya11
Copy link
Contributor Author

ping !

@oscardssmith
Copy link
Member

Needs a rebase but other than that, I think it's ready.

@oscardssmith
Copy link
Member

I just did the rebase and will merge when tests pass.

@oscardssmith oscardssmith added performance Must go faster merge me PR is reviewed. Merge when all tests are passing labels Apr 10, 2023
@oscardssmith oscardssmith merged commit 844c20d into JuliaLang:master Apr 10, 2023
@giordano giordano removed the merge me PR is reviewed. Merge when all tests are passing label Apr 11, 2023
Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
…aLang#47995)

* Do not use widen for UInt128

---------

Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants