shallow.c: bit manipulation tweaks

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> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rasmus Villemoes committed Dec 6, 2016 at 19:53 UTC 1127b3ced55b97229c55ff0c7585b284e3551a9e
1 file changed +4 -4
shallow.c
+4 -4
@@ -389,7 +389,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
389 * all walked commits.
390 */
391 static void paint_down(struct paint_info *info, const unsigned char *sha1,
392 - int id)
392 + unsigned int id)
393 {
394 unsigned int i, nr;
395 struct commit_list *head = NULL;
@@ -401,7 +401,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
401 if (!c)
402 return;
403 memset(bitmap, 0, bitmap_size);
404 - bitmap[id / 32] |= (1 << (id % 32));
404 + bitmap[id / 32] |= (1U << (id % 32));
405 commit_list_insert(c, &head);
406 while (head) {
407 struct commit_list *p;
@@ -575,11 +575,11 @@ static int add_ref(const char *refname, const struct object_id *oid,
575
576 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
577 {
578 - int i;
578 + unsigned int i;
579 if (!ref_status)
580 return;
581 for (i = 0; i < nr; i++)
582 - if (bitmap[i / 32] & (1 << (i % 32)))
582 + if (bitmap[i / 32] & (1U << (i % 32)))
583 ref_status[i]++;
584 }
585