Is xor the fastest operation the ALU can do on a byte? My prof was saying it is because there’s nothing simpler than checking to see if two things are the same or not. Is this the right way to think of xor that 1 is returned if the operands are different and 0 if they are the same?
Is xor the fastest operation the ALU can do on a byte? My prof
Share
This all depends on how your CPU works. In practice, ALU operations on modern chips are all one clock cycle — but even that statement is over-generalized, because there’s often more than one way to do arithmetic.
SIMD features allow you to process more than one piece of data per clock cycle, which increases throughput. Certain instructions on certain architectures (like x86’s
LEA) allow you to compose multiple arithmetic operations into a single instruction which is again executed in a single clock cycle, making that faster in certain perspectives.On most architectures, ALUs don’t just return a value, they also modify flags: overflow (carry), zero, etc. The time required to perform an arithmetic operation can change if other instructions depend on these flags, especially if there are conditionals involved. Check the manual.
Also, “fast” in terms of latency is different than “fast” in terms of operations per second. An XOR might take one clock cycle to execute in the ALU, but take another clock cycle until the result becomes available for use in another instruction, flags or otherwise. And even then, out-of-order execution can make it seem like the result is available immediately, but that’s because the chip is shuffling around your instructions to keep itself busy.