Skip to content

Commit 42c78a2

Browse files
rscharfegitster
authored andcommitted
use DIV_ROUND_UP
Convert code that divides and rounds up to use DIV_ROUND_UP to make the intent clearer and reduce the number of magic constants. Signed-off-by: Rene Scharfe <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c8e978 commit 42c78a2

File tree

9 files changed

+14
-15
lines changed

9 files changed

+14
-15
lines changed

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static int too_many_loose_objects(void)
148148
if (!dir)
149149
return 0;
150150

151-
auto_threshold = (gc_auto_threshold + 255) / 256;
151+
auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
152152
while ((ent = readdir(dir)) != NULL) {
153153
if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
154154
ent->d_name[38] != '\0')

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ static void compile_submodule_options(const struct grep_opt *opt,
524524
* submodule process has its own thread pool.
525525
*/
526526
argv_array_pushf(&submodule_options, "--threads=%d",
527-
(num_threads + 1) / 2);
527+
DIV_ROUND_UP(num_threads, 2));
528528

529529
/* Add Pathspecs */
530530
argv_array_push(&submodule_options, "--");

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ static struct commit *get_base_commit(const char *base_commit,
13021302

13031303
if (rev_nr % 2)
13041304
rev[i] = rev[2 * i];
1305-
rev_nr = (rev_nr + 1) / 2;
1305+
rev_nr = DIV_ROUND_UP(rev_nr, 2);
13061306
}
13071307

13081308
if (!in_merge_bases(base, rev[0]))

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ static const char *unpack_with_sideband(struct shallow_info *si)
18051805
static void prepare_shallow_update(struct command *commands,
18061806
struct shallow_info *si)
18071807
{
1808-
int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1808+
int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
18091809

18101810
ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
18111811
assign_shallow_commits_to_refs(si, si->used_shallow, NULL);

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
20952095
* bytes per "line".
20962096
* This is stupid and ugly, but very cheap...
20972097
*/
2098-
damage = (damage + 63) / 64;
2098+
damage = DIV_ROUND_UP(damage, 64);
20992099
ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
21002100
dir.files[dir.nr].name = file->name;
21012101
dir.files[dir.nr].changed = damage;

ewah/ewah_bitmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ size_t ewah_add(struct ewah_bitmap *self, eword_t word)
210210
void ewah_set(struct ewah_bitmap *self, size_t i)
211211
{
212212
const size_t dist =
213-
(i + BITS_IN_EWORD) / BITS_IN_EWORD -
214-
(self->bit_size + BITS_IN_EWORD - 1) / BITS_IN_EWORD;
213+
DIV_ROUND_UP(i + 1, BITS_IN_EWORD) -
214+
DIV_ROUND_UP(self->bit_size, BITS_IN_EWORD);
215215

216216
assert(i >= self->bit_size);
217217

imap-send.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static char hexchar(unsigned int b)
861861
return b < 10 ? '0' + b : 'a' + (b - 10);
862862
}
863863

864-
#define ENCODED_SIZE(n) (4*((n+2)/3))
864+
#define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
865865
static char *cram(const char *challenge_64, const char *user, const char *pass)
866866
{
867867
int i, resp_len, encoded_len, decoded_len;

sha1_name.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,9 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
479479
* We now know we have on the order of 2^len objects, which
480480
* expects a collision at 2^(len/2). But we also care about hex
481481
* chars, not bits, and there are 4 bits per hex. So all
482-
* together we need to divide by 2; but we also want to round
483-
* odd numbers up, hence adding one before dividing.
482+
* together we need to divide by 2 and round up.
484483
*/
485-
len = (len + 1) / 2;
484+
len = DIV_ROUND_UP(len, 2);
486485
/*
487486
* For very small repos, we stick with our regular fallback.
488487
*/

shallow.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ struct paint_info {
443443

444444
static uint32_t *paint_alloc(struct paint_info *info)
445445
{
446-
unsigned nr = (info->nr_bits + 31) / 32;
446+
unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
447447
unsigned size = nr * sizeof(uint32_t);
448448
void *p;
449449
if (!info->pool_count || size > info->end - info->free) {
@@ -471,7 +471,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
471471
{
472472
unsigned int i, nr;
473473
struct commit_list *head = NULL;
474-
int bitmap_nr = (info->nr_bits + 31) / 32;
474+
int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
475475
size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
476476
struct commit *c = lookup_commit_reference_gently(sha1, 1);
477477
uint32_t *tmp; /* to be freed before return */
@@ -611,7 +611,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
611611
paint_down(&pi, ref->oid[i].hash, i);
612612

613613
if (used) {
614-
int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
614+
int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
615615
memset(used, 0, sizeof(*used) * info->shallow->nr);
616616
for (i = 0; i < nr_shallow; i++) {
617617
const struct commit *c = lookup_commit(oid[shallow[i]].hash);
@@ -672,7 +672,7 @@ static void post_assign_shallow(struct shallow_info *info,
672672
struct commit *c;
673673
uint32_t **bitmap;
674674
int dst, i, j;
675-
int bitmap_nr = (info->ref->nr + 31) / 32;
675+
int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
676676
struct commit_array ca;
677677

678678
trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");

0 commit comments

Comments
 (0)