1
votes

If I have two large integers with thousands of digits, which instructions would be useful to multiply them? I'm assuming I will have to loop over the digits, multiply and add, like you do manual multiplication.

I don't expect any solutions here, just point me to the right instructions that would best accomplish the job. It can be any instruction from the MMX, SSE, x87 fpu etc...

mul should do the job just fine. It is likely useful to vectorise this in which case pmuldq will be useful. MMX and the x87 FPU will likely not be useful. - fuz
How are they stored? Like fully packed binary with 64 value bits per 8-byte chunk? Or unpacked decimal digits with one digit per byte? Or Python-internals style with 30 bits per 32-bit integer to allow software carry techniques for languages with adc? See also BigInt class in C++ for a survey of some ways of doing BigInteger stuff. - Peter Cordes
Generally even AVX2 is not useful because it doesn't provide carry across digits, and more importantly only has 32x32 => 64-bit multiply. SIMD signed with unsigned multiplication for 64-bit * 64-bit to 128-bit. But there is stuff you can do with SIMD: Can long integer routines benefit from SSE? - for large-enough numbers, FFT-based multiplication is apparently worth doing. - Peter Cordes
Also, if you just need the job done efficiently, consider using something like libgmp. If you want to learn how to do it yourself, still read libgmp documentation. It discusses the algorithms used. That's a good starting point. - Margaret Bloom
You can do this more efficiently using FFTs - Paul R