The C memcmp
function (and strcmp
) does a comparison similar to the function below for comparing integers:
int compare(const int a, const int b) {
if (a > b) return 1;
if (b > a) return -1;
return 0;
}
Microsoft information for their strcmp
says:
The strcmp function performs an ordinal comparison of string1 and string2 and returns a value that indicates their relationship.
The manual of GNU's libc describes the comparison in this way:
Your comparison function should return a value the way
strcmp
does: negative if the first argument is "less" than the second, zero if they are "equal", and positive if the first argument is "greater".
But doesn't define any name for this comparison type.
What is the name for this comparison algorithm?
And why doesn't memcmp
simply return say 0 if a and b the same and 1 otherwise?
<
and>
instead of just!=
?" Isn't the answer obvious? $\endgroup$memcmp
indicate which operand is "higher" is worth the cost. For many usage scenarios, it would be more helpful to have a variation ofmemcmp
which reported the location of the first mismatch, and a pass-fail version for situations where the blocks are expected to match (implying that vectorization is likely to be worthwhile, and that if examining a later part before an earlier part would be convenient, and it is found not to match, there's no need to examine the earlier part at all). $\endgroup$