fls() should find the most significant bit of an int faster than a

linear search can, so use it to avoid a linear search in isqrt.

Approved by: kib (mentor), markj (mentor)
Differential Revision: https://reviews.freebsd.org/D20102
This commit is contained in:
Doug Moore 2019-05-03 02:55:54 +00:00
parent 8c74ade848
commit 64f8d2575a

View File

@ -928,9 +928,7 @@ isqrt(u_int num)
{
u_int bit, root, tmp;
bit = 1u << ((NBBY * sizeof(u_int)) - 2);
while (bit > num)
bit >>= 2;
bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0;
root = 0;
while (bit != 0) {
tmp = root + bit;