git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/6] shallow.c: bit manipulation tweaks

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 6, 2016, 12:53 UTC
Message-ID
<20161206125339.16803-6-pclouds@gmail.com>
In-Reply-To
<20161206125339.16803-1-pclouds@gmail.com>
From: Rasmus Villemoes <rv@rasmusvillemoes.dk>

First of all, 1 << 31 is technically undefined behaviour, so let's just use an unsigned literal.

If i is 'signed int' and gcc doesn't know that i is positive, gcc generates code to compute the C99-mandated values of "i / 32" and "i % 32", which is a lot more complicated than simple a simple shifts/mask.

The only caller of paint_down actually passes an "unsigned int" value, but the prototype of paint_down causes (completely well-defined) conversion to signed int, and gcc has no way of knowing that the converted value is non-negative. Just make the id parameter unsigned.

In update_refstatus, the change in generated code is much smaller, presumably because gcc is smart enough to see that i starts as 0 and is only incremented, so it is allowed (per the UD of signed overflow) to assume that i is always non-negative. But let's just help less smart compilers generate good code anyway.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 shallow.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/shallow.c b/shallow.c
index 719f699..beb967e 100644
--- a/shallow.c
+++ b/shallow.c
@@ -467,7 +467,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
  * all walked commits.
  */
 static void paint_down(struct paint_info *info, const unsigned char *sha1,
-		       int id)
+		       unsigned int id)
 {
 	unsigned int i, nr;
 	struct commit_list *head = NULL;
@@ -479,7 +479,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
 	if (!c)
 		return;
 	memset(bitmap, 0, bitmap_size);
-	bitmap[id / 32] |= (1 << (id % 32));
+	bitmap[id / 32] |= (1U << (id % 32));
 	commit_list_insert(c, &head);
 	while (head) {
 		struct commit_list *p;
@@ -653,11 +653,11 @@ static int add_ref(const char *refname, const struct object_id *oid,
 
 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
 {
-	int i;
+	unsigned int i;
 	if (!ref_status)
 		return;
 	for (i = 0; i < nr; i++)
-		if (bitmap[i / 32] & (1 << (i % 32)))
+		if (bitmap[i / 32] & (1U << (i % 32)))
 			ref_status[i]++;
 }
 
-- 
2.8.2.524.g6ff3d78
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 13 of 20 in “shallow.c: make paint_alloc slightly more robust”
  1. 1/4 shallow.c: make paint_alloc slightly more robustRasmus Villemoes, Dec 2, 2016
  2. 2/4 shallow.c: avoid theoretical pointer wrap-aroundRasmus Villemoes, Dec 2, 2016
  3. Jeff KingDec 3, 2016
  4. 3/4 shallow.c: bit manipulation tweaksRasmus Villemoes, Dec 2, 2016
  5. Jeff KingDec 3, 2016
  6. 4/4 shallow.c: remove useless testRasmus Villemoes, Dec 2, 2016
  7. Jeff KingDec 3, 2016
  8. Duy NguyenDec 5, 2016
  9. Jeff KingDec 3, 2016
  10. Duy NguyenDec 5, 2016
  11. 0/6 shallow.c improvementsNguyễn Thái Ngọc Duy, Dec 6, 2016
  12. 1/6 shallow.c: rename fields in paint_info to better express their purposesNguyễn Thái Ngọc Duy, Dec 6, 2016
  13. 5/6 shallow.c: bit manipulation tweaksNguyễn Thái Ngọc Duy, Dec 6, 2016
  14. 4/6 shallow.c: avoid theoretical pointer wrap-aroundNguyễn Thái Ngọc Duy, Dec 6, 2016
  15. 6/6 shallow.c: remove useless codeNguyễn Thái Ngọc Duy, Dec 6, 2016
  16. 3/6 shallow.c: make paint_alloc slightly more robustNguyễn Thái Ngọc Duy, Dec 6, 2016
  17. 2/6 shallow.c: stop abusing COMMIT_SLAB_SIZE for paint_info's memory poolsNguyễn Thái Ngọc Duy, Dec 6, 2016
  18. Jeff KingDec 6, 2016
  19. Duy NguyenDec 6, 2016
  20. Junio C HamanoDec 7, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.