Let's say that the environment is x86.
How do compilers compile the ">" operator on 32-bit integers. Logically, I mean. Without any knowledge of Assembly.
Let's say that the high level language code is:
int32 x, y;
x = 123;
y = 456;
bool z;
z = x > y;
What does the compiler do for evaluating the expression x > y
?
Does it perform something like (assuming that x and y are positive integers):
w = sign_of(x - y);
if (w == 0)
// expression is 'false'
else if (w == 1)
// expression is 'true'
else
// expression is 'false'
Is there any reference for such information?
I'm fairly sure integer comparisons are implemented at the CPU level. At that level, you are usually looking at something like the the following logic:
x > y if y - x < 0
This looks like a recursive definition, but isn't since the second half can be determined by looking at the sign bit of the result.
To expand on this slightly, from a description of the CMP
instruction in x86 which is what the compiler would compile a >
operator to, but maps more directly to the <=>
operator available in some languages.
Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.
After the CMP instruction, the compiler then needs to do one or more conditional instructions, but this is how the actual comparison works.