Skip to content

Commit baea6e2

Browse files
committed
mruby-bigint (mpz_bits): use lzb()
1 parent 061521e commit baea6e2

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

mrbgems/mruby-bigint/core/bigint.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,18 +1251,14 @@ mpz_bits(const mpz_t *x)
12511251
{
12521252
if (x->sz == 0 || x->sn == 0) return 0;
12531253

1254-
// Get the most significant limb (last element in little-endian order)
1254+
size_t limb_bits = sizeof(mp_limb) * 8;
1255+
1256+
// Get the most significant limb
12551257
size_t i = x->sz - 1;
12561258
mp_limb high = x->p[i];
12571259

1258-
// Count the number of bits in the most significant limb
1259-
size_t bits = 0;
1260-
while (high != 0) {
1261-
high >>= 1;
1262-
bits++;
1263-
}
1264-
1265-
return i * (sizeof(mp_limb) * 8) + bits;
1260+
// Number of bits = total full limbs + significant bits in top limb
1261+
return i * limb_bits + (limb_bits - lzb(high));
12661262
}
12671263

12681264
static void

0 commit comments

Comments
 (0)