From: Paul Eggert Date: Wed, 18 May 2022 02:17:12 +0000 (-0700) Subject: maint: simplify comparisons X-Git-Tag: v9.2~193 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5864e8f919c3dae827016ac1a78544db90403b6d;p=thirdparty%2Fcoreutils.git maint: simplify comparisons * src/comm.c (compare_files): * src/join.c (keycmp): * src/ls.c (off_cmp): * src/ptx.c (compare_words, compare_occurs): * src/set-fields.c (compare_ranges): Prefer ((a > b) - (a < b)) to variants like (a < b ? -1 : a > b) as it’s typically faster these days. --- diff --git a/src/comm.c b/src/comm.c index 947463638f..721139cb8d 100644 --- a/src/comm.c +++ b/src/comm.c @@ -321,9 +321,8 @@ compare_files (char **infiles) size_t len = min (thisline[0]->length, thisline[1]->length) - 1; order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len); if (order == 0) - order = (thisline[0]->length < thisline[1]->length - ? -1 - : thisline[0]->length != thisline[1]->length); + order = ((thisline[0]->length > thisline[1]->length) + - (thisline[0]->length < thisline[1]->length)); } } diff --git a/src/join.c b/src/join.c index f2fd1727b3..fde4634f07 100644 --- a/src/join.c +++ b/src/join.c @@ -380,7 +380,7 @@ keycmp (struct line const *line1, struct line const *line2, if (diff) return diff; - return len1 < len2 ? -1 : len1 != len2; + return (len1 > len2) - (len1 < len2); } /* Check that successive input lines PREV and CURRENT from input file diff --git a/src/ls.c b/src/ls.c index d15a103677..d48892be7c 100644 --- a/src/ls.c +++ b/src/ls.c @@ -3881,7 +3881,7 @@ cmp_btime (struct fileinfo const *a, struct fileinfo const *b, static int off_cmp (off_t a, off_t b) { - return a < b ? -1 : a > b; + return (a > b) - (a < b); } static int diff --git a/src/ptx.c b/src/ptx.c index 09b54447df..d7decc2ace 100644 --- a/src/ptx.c +++ b/src/ptx.c @@ -567,7 +567,7 @@ compare_words (const void *void_first, const void *void_second) } } - return first->size < second->size ? -1 : first->size > second->size; + return (first->size > second->size) - (first->size < second->size); #undef first #undef second } @@ -587,8 +587,8 @@ compare_occurs (const void *void_first, const void *void_second) value = compare_words (&first->key, &second->key); return (value ? value - : first->key.start < second->key.start ? -1 - : first->key.start > second->key.start); + : ((first->key.start > second->key.start) + - (first->key.start < second->key.start))); #undef first #undef second } diff --git a/src/set-fields.c b/src/set-fields.c index 575dc2784e..670706f1cb 100644 --- a/src/set-fields.c +++ b/src/set-fields.c @@ -55,13 +55,12 @@ add_range_pair (uintmax_t lo, uintmax_t hi) /* Comparison function for qsort to order the list of - struct range_pairs. */ + struct field_range_pairs. */ static int compare_ranges (const void *a, const void *b) { - int a_start = ((const struct field_range_pair *) a)->lo; - int b_start = ((const struct field_range_pair *) b)->lo; - return a_start < b_start ? -1 : a_start > b_start; + struct field_range_pair const *ap = a, *bp = b; + return (ap->lo > bp->lo) - (ap->lo < bp->lo); } /* Reallocate Range Pair entries, with corresponding