We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 061521e commit baea6e2Copy full SHA for baea6e2
1 file changed
mrbgems/mruby-bigint/core/bigint.c
@@ -1251,18 +1251,14 @@ mpz_bits(const mpz_t *x)
1251
{
1252
if (x->sz == 0 || x->sn == 0) return 0;
1253
1254
- // Get the most significant limb (last element in little-endian order)
+ size_t limb_bits = sizeof(mp_limb) * 8;
1255
+
1256
+ // Get the most significant limb
1257
size_t i = x->sz - 1;
1258
mp_limb high = x->p[i];
1259
- // Count the number of bits in the most significant limb
- size_t bits = 0;
1260
- while (high != 0) {
1261
- high >>= 1;
1262
- bits++;
1263
- }
1264
-
1265
- return i * (sizeof(mp_limb) * 8) + bits;
+ // Number of bits = total full limbs + significant bits in top limb
+ return i * limb_bits + (limb_bits - lzb(high));
1266
}
1267
1268
static void
0 commit comments