X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=bisect.c;h=bd1b7b5dac81ada592a16630214d1f2cbb655b88;hb=9798f7e5f948b2586c33d850f6a00d4fc0537d75;hp=1aad49b1a642a308f449001960958ccab79347d1;hpb=e034d1bb927c17a3406a2bdbb8ccf710677a139d;p=git.git diff --git a/bisect.c b/bisect.c index 1aad49b1a..bd1b7b5da 100644 --- a/bisect.c +++ b/bisect.c @@ -956,3 +956,41 @@ int bisect_next_all(const char *prefix, int no_checkout) return bisect_checkout(bisect_rev_hex, no_checkout); } +static inline int log2i(int n) +{ + int log2 = 0; + + for (; n > 1; n >>= 1) + log2++; + + return log2; +} + +static inline int exp2i(int n) +{ + return 1 << n; +} + +/* + * Estimate the number of bisect steps left (after the current step) + * + * For any x between 0 included and 2^n excluded, the probability for + * n - 1 steps left looks like: + * + * P(2^n + x) == (2^n - x) / (2^n + x) + * + * and P(2^n + x) < 0.5 means 2^n < 3x + */ +int estimate_bisect_steps(int all) +{ + int n, x, e; + + if (all < 3) + return 0; + + n = log2i(all); + e = exp2i(n); + x = all - e; + + return (e < 3 * x) ? n : n - 1; +}