bc: rewrite bc_num_compare() to be readable
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 17 Dec 2018 23:39:24 +0000 (00:39 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 18 Dec 2018 02:30:23 +0000 (03:30 +0100)
function                                             old     new   delta
bc_num_compare                                        59      51      -8

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c

index e5ad0ed8653234e8c8a2350558100a7db8175db5..eaab6cee633e891d50fa0320c97c570d2f9c2da6 100644 (file)
@@ -1475,10 +1475,20 @@ static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
 
 static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
 {
-       size_t i;
-       int c = 0;
-       for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
-       return BC_NUM_NEG(i + 1, c < 0);
+       size_t i = len;
+       for (;;) {
+               int c;
+               if (i == 0)
+                       return 0;
+               i--;
+               c = a[i] - b[i];
+               if (c != 0) {
+                       i++;
+                       if (c < 0)
+                               return -i;
+                       return i;
+               }
+       }
 }
 
 static ssize_t bc_num_cmp(BcNum *a, BcNum *b)