Skip to content

Commit 1127b3c

Browse files
Villemoesgitster
authored andcommitted
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>
1 parent 381aa8e commit 1127b3c

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

shallow.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
389389
* all walked commits.
390390
*/
391391
static void paint_down(struct paint_info *info, const unsigned char *sha1,
392-
int id)
392+
unsigned int id)
393393
{
394394
unsigned int i, nr;
395395
struct commit_list *head = NULL;
@@ -401,7 +401,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
401401
if (!c)
402402
return;
403403
memset(bitmap, 0, bitmap_size);
404-
bitmap[id / 32] |= (1 << (id % 32));
404+
bitmap[id / 32] |= (1U << (id % 32));
405405
commit_list_insert(c, &head);
406406
while (head) {
407407
struct commit_list *p;
@@ -575,11 +575,11 @@ static int add_ref(const char *refname, const struct object_id *oid,
575575

576576
static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
577577
{
578-
int i;
578+
unsigned int i;
579579
if (!ref_status)
580580
return;
581581
for (i = 0; i < nr; i++)
582-
if (bitmap[i / 32] & (1 << (i % 32)))
582+
if (bitmap[i / 32] & (1U << (i % 32)))
583583
ref_status[i]++;
584584
}
585585

0 commit comments

Comments
 (0)