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

Add sum methods for BigInts and BigFloats #3065

Merged
merged 3 commits into from
May 9, 2013

Conversation

andrioni
Copy link
Member

@andrioni andrioni commented May 9, 2013

As due to a pointer issue mpfr_sum isn't wrap-able, I've decided to write a slightly more efficient method for BigFloats (and another one for BigInts with the same structure) reusing the summation variable.
In my tests with arrays with 10^6 numbers, it seems to be about 5-10x faster than the generic version.

@stevengj
Copy link
Member

stevengj commented May 9, 2013

Couldn't you implement + and * of multiple arguments in the same way (to avoid the extra temporary vars)?

Note that you can use mpfr_sum once #2818 lands. (Actually, there is a hackish way to use it now, by using jl_value_ptr combined with & get C-compatible struct pointers. Probably better to just wait for 2818, though.)

If the above functions are called with up to five arguments BigType
, the only allocation that will happen is of the return variable,
without any other temp variables.

This happens with +,* for BigFloats, and with +,*,$,|,& for BigInts.
@andrioni
Copy link
Member Author

andrioni commented May 9, 2013

Just did with also $, | and & for good measure, as they are the other commutative and associative operators we have in BigInt.

JeffBezanson added a commit that referenced this pull request May 9, 2013
Add `sum` methods for BigInts and BigFloats
@JeffBezanson JeffBezanson merged commit 4978a52 into JuliaLang:master May 9, 2013
@stevengj
Copy link
Member

Instead of defining + etcetera for 2, 3, 4, and 5 arguments separately, can't you just define function +(xs::BigInt...) to handle an arbitrary number of arguments?

@andrioni
Copy link
Member Author

But we can't typecheck then, as these new methods are defined when all arguments are BigInt. Well, I could write a generic varargs method, but having to typecheck then kind of makes it moot.

@andrioni
Copy link
Member Author

(maybe a varargs method typechecking to see if it's of one of the possible conversions and then doing it keeping the accumulator variable mutable could be worth it, but it'd have to basically replicate all external constructors.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants