coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[INSTALLED 4/5] maint: simplify comparisons


From: Paul Eggert
Subject: [INSTALLED 4/5] maint: simplify comparisons
Date: Tue, 17 May 2022 19:32:24 -0700

* 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.
---
 src/comm.c       | 5 ++---
 src/join.c       | 2 +-
 src/ls.c         | 2 +-
 src/ptx.c        | 6 +++---
 src/set-fields.c | 7 +++----
 5 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/src/comm.c b/src/comm.c
index 947463638..721139cb8 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 f2fd1727b..fde4634f0 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 d15a10367..d48892be7 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 09b54447d..d7decc2ac 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 575dc2784..670706f1c 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
-- 
2.36.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]