]> git.neil.brown.name Git - git.git/commitdiff
use DIV_ROUND_UP
authorRené Scharfe <l.s.r@web.de>
Sat, 8 Jul 2017 10:35:35 +0000 (12:35 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Jul 2017 21:24:36 +0000 (14:24 -0700)
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 <l.s.r@web.de>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/gc.c
builtin/grep.c
builtin/log.c
builtin/receive-pack.c
diff.c
ewah/ewah_bitmap.c
imap-send.c
sha1_name.c
shallow.c

index 91f7696a85ec974d0ff5a1591cc404ed99efb7d7..0477c2451f8e47bb57d99b6ef3b8709c486d4bf8 100644 (file)
@@ -148,7 +148,7 @@ static int too_many_loose_objects(void)
        if (!dir)
                return 0;
 
-       auto_threshold = (gc_auto_threshold + 255) / 256;
+       auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
        while ((ent = readdir(dir)) != NULL) {
                if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
                    ent->d_name[38] != '\0')
index 254c1c7849152884d9abd0b603f1c75f87e085b5..dc001da562322e3b9b7133fcf939195040a7a8f5 100644 (file)
@@ -524,7 +524,7 @@ static void compile_submodule_options(const struct grep_opt *opt,
         * submodule process has its own thread pool.
         */
        argv_array_pushf(&submodule_options, "--threads=%d",
-                        (num_threads + 1) / 2);
+                        DIV_ROUND_UP(num_threads, 2));
 
        /* Add Pathspecs */
        argv_array_push(&submodule_options, "--");
index 57ce470f5094c238ea4d5390f358b5cdd9499a56..96c050b450056205ea971340e824d1e3d87e0d6a 100644 (file)
@@ -1302,7 +1302,7 @@ static struct commit *get_base_commit(const char *base_commit,
 
                if (rev_nr % 2)
                        rev[i] = rev[2 * i];
-               rev_nr = (rev_nr + 1) / 2;
+               rev_nr = DIV_ROUND_UP(rev_nr, 2);
        }
 
        if (!in_merge_bases(base, rev[0]))
index da9a3a2c9dc271af3e5d842e2483e81dddbe9a77..90d58c2fedca4e86d817d9c4350d043b6249fa41 100644 (file)
@@ -1805,7 +1805,7 @@ static const char *unpack_with_sideband(struct shallow_info *si)
 static void prepare_shallow_update(struct command *commands,
                                   struct shallow_info *si)
 {
-       int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
+       int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
 
        ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
        assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
diff --git a/diff.c b/diff.c
index 74283d9001fbc48d11ba5c449ea7c16692791c88..63ff66bf6d0b611c8badeb68f5f7295b23ebd7bf 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -2095,7 +2095,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
                         * bytes per "line".
                         * This is stupid and ugly, but very cheap...
                         */
-                       damage = (damage + 63) / 64;
+                       damage = DIV_ROUND_UP(damage, 64);
                ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
                dir.files[dir.nr].name = file->name;
                dir.files[dir.nr].changed = damage;
index 2dc9c82ecf513ed721f290926f042b9f592221c2..06c479f70a8eef4d2c9537be2723d7ccc10ae845 100644 (file)
@@ -210,8 +210,8 @@ size_t ewah_add(struct ewah_bitmap *self, eword_t word)
 void ewah_set(struct ewah_bitmap *self, size_t i)
 {
        const size_t dist =
-               (i + BITS_IN_EWORD) / BITS_IN_EWORD -
-               (self->bit_size + BITS_IN_EWORD - 1) / BITS_IN_EWORD;
+               DIV_ROUND_UP(i + 1, BITS_IN_EWORD) -
+               DIV_ROUND_UP(self->bit_size, BITS_IN_EWORD);
 
        assert(i >= self->bit_size);
 
index 857591660f485d65dec3390b22dd2083c94d7a44..37388f142d035f51284ed30eacac792177713697 100644 (file)
@@ -861,7 +861,7 @@ static char hexchar(unsigned int b)
        return b < 10 ? '0' + b : 'a' + (b - 10);
 }
 
-#define ENCODED_SIZE(n) (4*((n+2)/3))
+#define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
 static char *cram(const char *challenge_64, const char *user, const char *pass)
 {
        int i, resp_len, encoded_len, decoded_len;
index 5e2ec37b6557f59970ed2b2dd69d4e0b151d1fd0..f7b6242914be6306f74e038c3638ef98ac24f7e9 100644 (file)
@@ -479,10 +479,9 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
                 * We now know we have on the order of 2^len objects, which
                 * expects a collision at 2^(len/2). But we also care about hex
                 * chars, not bits, and there are 4 bits per hex. So all
-                * together we need to divide by 2; but we also want to round
-                * odd numbers up, hence adding one before dividing.
+                * together we need to divide by 2 and round up.
                 */
-               len = (len + 1) / 2;
+               len = DIV_ROUND_UP(len, 2);
                /*
                 * For very small repos, we stick with our regular fallback.
                 */
index f9370961f99f5e68c1bfe84d215513536fccbdaf..1aee070c4da64f7f03473bc812c6c1e942944a37 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -443,7 +443,7 @@ struct paint_info {
 
 static uint32_t *paint_alloc(struct paint_info *info)
 {
-       unsigned nr = (info->nr_bits + 31) / 32;
+       unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
        unsigned size = nr * sizeof(uint32_t);
        void *p;
        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,
 {
        unsigned int i, nr;
        struct commit_list *head = NULL;
-       int bitmap_nr = (info->nr_bits + 31) / 32;
+       int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
        size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
        struct commit *c = lookup_commit_reference_gently(sha1, 1);
        uint32_t *tmp; /* to be freed before return */
@@ -611,7 +611,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
                paint_down(&pi, ref->oid[i].hash, i);
 
        if (used) {
-               int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
+               int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
                memset(used, 0, sizeof(*used) * info->shallow->nr);
                for (i = 0; i < nr_shallow; i++) {
                        const struct commit *c = lookup_commit(oid[shallow[i]].hash);
@@ -672,7 +672,7 @@ static void post_assign_shallow(struct shallow_info *info,
        struct commit *c;
        uint32_t **bitmap;
        int dst, i, j;
-       int bitmap_nr = (info->ref->nr + 31) / 32;
+       int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
        struct commit_array ca;
 
        trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");