I work on AVX2 and need to calculate 64-bit x64-bit -> 128-bit widening multiplication and got 64-bit high part in the fastest manner. Since AVX2 has not such an instruction, is it reasonable for me to use Karatsuba algorithm for efficiency and gaining speed?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- How to use doMC under Windows or alternative paral
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
No. On modern architectures the crossover at which Karatsuba beats schoolbook multiplication is usually somewhere between 8 and 24 machine words (e.g. between 512 and 1536 bits on x86_64). For fixed sizes, the threshold is at the smaller end of that range, and the new ADCX/ADOX instructions likely bring it in somewhat further for scalar code, but 64x64 is still too small to benefit from Karatsuba.
It's hard to tell without trying, but it might me faster to just use the AMD64 MUL instruction, which supports 64x64=128 with the same throughput as most AVX2 instructions (but not vectorized). The drawback is that you need to load to regular registers if the operands were in YMM registers. That would give something like
LOAD + MUL + STORE
for a single 64x64=128.If you can vectorize Karatsuba in AVX2, try both AVX2 and
MUL
and see which is faster. If you can't vectorize, singleMUL
will probably be faster. If you can remove the load and store to regular registers, singleMUL
will be definitely faster.Both
MUL
and AVX2 instructions can have an operand in memory with the same throughput, and it may help to remove one load forMUL
.It's highly unlikely that AVX2 will beat the
mulx
instruction which does 64bx64b to 128b in one instruction. There is one exception I'm aware of large multiplications using floating point FFT.However, if you don't need exactly 64bx64b to 128b you could consider 53bx53b to 106b using double-double arithmetic.
To multiply four 53-bit numbers
a
andb
to get four 106-bit number only needs two instructions:This gives four 106-bit numbers in two instructions compared to one 128-bit number in one instruction using
mulx
.