Sun Jul 26 17:09:23 2020 UTC ()
make(1): save a few bytes and cycles by comparing against 0

The comparison against ac - 1 could have been optimized by the compilers
as well, but both GCC 5 and Clang produce smaller code for the comparison
against 0.


(rillig)
diff -r1.317 -r1.318 src/usr.bin/make/var.c

cvs diff -r1.317 -r1.318 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2020/07/26 16:59:08 1.317
+++ src/usr.bin/make/var.c 2020/07/26 17:09:23 1.318
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.317 2020/07/26 16:59:08 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.318 2020/07/26 17:09:23 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -59,34 +59,34 @@ @@ -59,34 +59,34 @@
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE. 68 * SUCH DAMAGE.
69 */ 69 */
70 70
71#ifndef MAKE_NATIVE 71#ifndef MAKE_NATIVE
72static char rcsid[] = "$NetBSD: var.c,v 1.317 2020/07/26 16:59:08 rillig Exp $"; 72static char rcsid[] = "$NetBSD: var.c,v 1.318 2020/07/26 17:09:23 rillig Exp $";
73#else 73#else
74#include <sys/cdefs.h> 74#include <sys/cdefs.h>
75#ifndef lint 75#ifndef lint
76#if 0 76#if 0
77static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; 77static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
78#else 78#else
79__RCSID("$NetBSD: var.c,v 1.317 2020/07/26 16:59:08 rillig Exp $"); 79__RCSID("$NetBSD: var.c,v 1.318 2020/07/26 17:09:23 rillig Exp $");
80#endif 80#endif
81#endif /* not lint */ 81#endif /* not lint */
82#endif 82#endif
83 83
84/*- 84/*-
85 * var.c -- 85 * var.c --
86 * Variable-handling functions 86 * Variable-handling functions
87 * 87 *
88 * Interface: 88 * Interface:
89 * Var_Set Set the value of a variable in the given 89 * Var_Set Set the value of a variable in the given
90 * context. The variable is created if it doesn't 90 * context. The variable is created if it doesn't
91 * yet exist. 91 * yet exist.
92 * 92 *
@@ -1689,29 +1689,29 @@ VarOrder(const char *str, const char oty @@ -1689,29 +1689,29 @@ VarOrder(const char *str, const char oty
1689 * reasonable value for mod factor is 2 (the mod 1 will produce 1689 * reasonable value for mod factor is 2 (the mod 1 will produce
1690 * 0 with probability 1). 1690 * 0 with probability 1).
1691 */ 1691 */
1692 for (i = ac - 1; i > 0; i--) { 1692 for (i = ac - 1; i > 0; i--) {
1693 int rndidx = random() % (i + 1); 1693 int rndidx = random() % (i + 1);
1694 char *t = av[i]; 1694 char *t = av[i];
1695 av[i] = av[rndidx]; 1695 av[i] = av[rndidx];
1696 av[rndidx] = t; 1696 av[rndidx] = t;
1697 } 1697 }
1698 } 1698 }
1699 } 1699 }
1700 1700
1701 for (i = 0; i < ac; i++) { 1701 for (i = 0; i < ac; i++) {
1702 Buf_AddStr(&buf, av[i]); 1702 if (i != 0)
1703 if (i != ac - 1) 
1704 Buf_AddByte(&buf, ' '); 1703 Buf_AddByte(&buf, ' ');
 1704 Buf_AddStr(&buf, av[i]);
1705 } 1705 }
1706 1706
1707 free(as); 1707 free(as);
1708 free(av); 1708 free(av);
1709 1709
1710 return Buf_Destroy(&buf, FALSE); 1710 return Buf_Destroy(&buf, FALSE);
1711} 1711}
1712 1712
1713 1713
1714/*- 1714/*-
1715 *----------------------------------------------------------------------- 1715 *-----------------------------------------------------------------------
1716 * VarUniq -- 1716 * VarUniq --
1717 * Remove adjacent duplicate words. 1717 * Remove adjacent duplicate words.
@@ -1736,29 +1736,29 @@ VarUniq(const char *str) @@ -1736,29 +1736,29 @@ VarUniq(const char *str)
1736 int ac, i, j; 1736 int ac, i, j;
1737 1737
1738 Buf_Init(&buf, 0); 1738 Buf_Init(&buf, 0);
1739 av = brk_string(str, &ac, FALSE, &as); 1739 av = brk_string(str, &ac, FALSE, &as);
1740 1740
1741 if (ac > 1) { 1741 if (ac > 1) {
1742 for (j = 0, i = 1; i < ac; i++) 1742 for (j = 0, i = 1; i < ac; i++)
1743 if (strcmp(av[i], av[j]) != 0 && (++j != i)) 1743 if (strcmp(av[i], av[j]) != 0 && (++j != i))
1744 av[j] = av[i]; 1744 av[j] = av[i];
1745 ac = j + 1; 1745 ac = j + 1;
1746 } 1746 }
1747 1747
1748 for (i = 0; i < ac; i++) { 1748 for (i = 0; i < ac; i++) {
1749 Buf_AddStr(&buf, av[i]); 1749 if (i != 0)
1750 if (i != ac - 1) 
1751 Buf_AddByte(&buf, ' '); 1750 Buf_AddByte(&buf, ' ');
 1751 Buf_AddStr(&buf, av[i]);
1752 } 1752 }
1753 1753
1754 free(as); 1754 free(as);
1755 free(av); 1755 free(av);
1756 1756
1757 return Buf_Destroy(&buf, FALSE); 1757 return Buf_Destroy(&buf, FALSE);
1758} 1758}
1759 1759
1760/*- 1760/*-
1761 *----------------------------------------------------------------------- 1761 *-----------------------------------------------------------------------
1762 * VarRange -- 1762 * VarRange --
1763 * Return an integer sequence 1763 * Return an integer sequence
1764 * 1764 *
@@ -1777,29 +1777,29 @@ VarRange(const char *str, int ac) @@ -1777,29 +1777,29 @@ VarRange(const char *str, int ac)
1777 Buffer buf; /* Buffer for new string */ 1777 Buffer buf; /* Buffer for new string */
1778 char **av; /* List of words to affect */ 1778 char **av; /* List of words to affect */
1779 char *as; /* Word list memory */ 1779 char *as; /* Word list memory */
1780 int i; 1780 int i;
1781 1781
1782 Buf_Init(&buf, 0); 1782 Buf_Init(&buf, 0);
1783 if (ac > 0) { 1783 if (ac > 0) {
1784 as = NULL; 1784 as = NULL;
1785 av = NULL; 1785 av = NULL;
1786 } else { 1786 } else {
1787 av = brk_string(str, &ac, FALSE, &as); 1787 av = brk_string(str, &ac, FALSE, &as);
1788 } 1788 }
1789 for (i = 0; i < ac; i++) { 1789 for (i = 0; i < ac; i++) {
1790 Buf_AddInt(&buf, 1 + i); 1790 if (i != 0)
1791 if (i != ac - 1) 
1792 Buf_AddByte(&buf, ' '); 1791 Buf_AddByte(&buf, ' ');
 1792 Buf_AddInt(&buf, 1 + i);
1793 } 1793 }
1794 1794
1795 free(as); 1795 free(as);
1796 free(av); 1796 free(av);
1797 1797
1798 return Buf_Destroy(&buf, FALSE); 1798 return Buf_Destroy(&buf, FALSE);
1799} 1799}
1800 1800
1801 1801
1802/*- 1802/*-
1803 * Parse a text part of a modifier such as the "from" and "to" in :S/from/to/ 1803 * Parse a text part of a modifier such as the "from" and "to" in :S/from/to/
1804 * or the :@ modifier. Nested variables in the text are expanded unless 1804 * or the :@ modifier. Nested variables in the text are expanded unless
1805 * VARE_NOSUBST is set. 1805 * VARE_NOSUBST is set.