I am looking for a signed integer comparison function cmp(x: Int, y: Int) -> Int
which does not use any comparison operators (<
, <=
, >
, >=
, <=>
, etc.), does not use widening to a larger size of integer, and ideally only uses addition, subtraction, bitwise operators and the equality and inequality operators (==
, !=
).
I have tested the comparison operators here, here and here, but all of them fail for certain input values (here is a test program in Rust). The first two seem the most promising, and they fail for the same values, probably because of underflow in the subtraction.