Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

assembly - What is the difference between the ADC and ADCX instructions on ia32/ia64?

I was looking through the Intel software developer manual when I encountered the ADCX instruction, which was previously unknown to me; its encoding is 66 0F 38 F6. It seems to be almost identical to the ADC instruction, so why would you use ADCX when:

  • it is only supported in modern CPUs
  • instruction encoding takes more space (4 bytes versus 1 for ADC)

Is there some other side effect, or special case, where ADCX proves advantageous over ADC? There must have been some good reason why this was added to the instruction repertoire.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These instructions are used to speed up large integer arithmetic.

Before these instructions adding large numbers often resulted in a code-sequence that looked like this:

  add  
  adc  
  adc  
  adc  
  adc  

The important part here to note is, that if the result of an addition would not fit into a machine word, the carry flag gets set and is 'carried over' to the next higher machine word. All these instructions depend on each other because they take the previous addition carry flag into account and generate a new carry flag value after execution.

Since x86 processors are capable to execute several instructions at once this became a huge bottleneck. The dependency chain just made it impossible for the processor to execute any of the arithmetic in parallel. (to be correct in practice you'll find a loads and stores between the add/adc sequences, but the performance was still limited by the carry dependency).

To improve upon this Intel added a second carry chain by reinterpreting the overflow flag.

The adc instruction got two news variants: adcx and adox

adcx is the same as adc, except that it does not modify the OF (overflow) flag anymore.

adox is the same as adc, but it stores the carry information in the OF flag. It also does not modify the carry-flag anymore.

As you can see the two new adc variants don't influence each other with regards to the flag usage. This allows you to run two long integer additions in parallel by interleaving the instructions and use adcx for one sequence and adox for the other sequence.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...