Celeritas
Celeritas

Reputation: 15091

Is xor the fastest operation the ALU can do?

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?

Upvotes: 1

Views: 3928

Answers (3)

willglynn
willglynn

Reputation: 11520

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.

Upvotes: 6

Rudolf Mühlbauer
Rudolf Mühlbauer

Reputation: 2531

I doubt that argumentation; the ALU has several operations implemented. Firstly, it clearly depends on the actual CPU, secondly it depends on the overall CPU architecture (Load-store, etc).

One plausible argument, however, I quote from Wikipedia:

On some computer architectures, it is more efficient to store a zero in a register by xor-ing the register with itself (bits xor-ed with themselves are always zero) instead of loading and storing the value zero.

This is, because no additional operands are required.

Upvotes: 1

CrazyCasta
CrazyCasta

Reputation: 28362

Actually NAND and NOR are the simplest binary operations due to their CMOS implementation (roughly speaking they have a low number of gates). NOT is the simplest operation of course because all it does is invert the bits. In terms of an actual CPU however, even the add/subtract operations are likely to only take a single cycle.

Upvotes: 5

Related Questions