Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you add two 64-bit integers natively in x86?

Tags:

x86

assembly

sse

The "regular" registers in x86 are only 32-bit in size, so you can't use them to add two 64-bit integers (unless you do the addition in multiple steps).

But can you add two 64-bit integers natively using another way, using SSE for example?

like image 645
Tom Avatar asked Dec 22 '25 21:12

Tom


1 Answers

In 32 bit modes, there are four ways to do this:

  • the most recommended way is to do an addition in two steps with an add and then an adc on general purpose registers
  • if your CPU has an FPU, you can also use the x87 FPU to do 64 bit arithmetic. Since the x87 FPU holds a 64 bit mantissa, computations on 64 bit integers are exact as long as you don't exceed the 64 bit range.
  • if your CPU supports at least SSE2, you can do 64 bit arithmetic on MMX registers
  • if your CPU supports at least SSE2, you can also do 64 bit arithmetic on XMM registers

The fastest of these for a single 64 bit operation is probably the add/adc approach. For multiple operations, SSE2 is going to be the fastest, then MMX (if you can live with the transition penalty and being unable to use the x87 FPU while in MMX state) and lastly x87.

In 64 bit mode (long mode), you can additionally simply do 64 bit arithmetic on 64 bit general purpose registers.

Let me know if you want more details or examples.

like image 80
fuz Avatar answered Dec 24 '25 11:12

fuz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!