boostorg / multiprecision

Boost.Multiprecision
Boost Software License 1.0
186 stars 112 forks source link

conversion of big int to string #617

Open afabri opened 2 months ago

afabri commented 2 months ago

We use i.convert_to<std::string> for a multi precision integer i and observe a factor of 10 difference for the cpp_int backend compared to the gmp_int backend. Is there a better way than using convert_to() ?

NAThompson commented 2 months ago

@afabri : Could you post a reproducer? Sounds like a bug that needs fixed instead of something that needs a workaround.

jzmaddock commented 2 months ago

Indeed, plus is there any particular reason not to just call the .str() method?

afabri commented 2 months ago

I have put a self contained program here on gist.github.com. Calling .str() has the same difference in performance.

afabri commented 2 months ago

Under vtune I can see that it is the divide_unsigned_helper() which is time consuming.

jzmaddock commented 2 months ago

Ah performance, not value of result, I misunderstood, apologies.

Yes we know that cpp_int division is terribly slow compared to GMP, but GMP is a tour-de-force of optimisation and we're not really trying to compete on that front (which is not to say we couldn't do better).

afabri commented 2 months ago

In our use case (a multiprecision float with mantissa and exponent being arbitrary precision ints) we are only interested in the leading N characters of the string representing the number.

ckormanyos commented 2 months ago

In our use case (a multiprecision float with mantissa and exponent being arbitrary precision ints) we are only interested in the leading N characters of the string representing the number.

Cound you do something a bit non-elegant like get the MSB (for $2^n$), then get the highest order limb and subsequently perform a table-based addition for the leading bits? It does not really sound like a lot of fun, but might work?

afabri commented 1 month ago

Ah performance, not value of result, I misunderstood, apologies.

Yes we know that cpp_int division is terribly slow compared to GMP, but GMP is a tour-de-force of optimisation and we're not really trying to compete on that front (which is not to say we couldn't do better).

@jzmaddock Can you give me a pointer to a publication that tells what has to be implemented to catch up?

ckormanyos commented 1 month ago

to a publication that tells what has to be implemented to catch up?

I'll put out a few details. cpp_int division is known to be slow. What could be done?

@jzmaddock might add more.

P.S. I like the algorithm descriptions in:

[1] R. Brent and P. Zimmermann, "Modern Computer Arithmetic", Cambridge University Press, 2011. [2] J. Arndt, "Matters Computational", Springer Verlag, 2011. [3] and of course the Knuth's volume 2.