Skip to content

Commit

Permalink
libkern: avoid local var in order_base_2()
Browse files Browse the repository at this point in the history
order_base_2(n) is implemented with a variable, which keeps it from
being used at file scope. Implement it instead as ilog2(2*n-1), which
produces a different result when 2*n overflows, which appears unlikely
in practice.

Reviewed by:	bz
Differential Revision:	https://reviews.freebsd.org/D46826
  • Loading branch information
Doug Moore authored and bsdjhb committed Jan 11, 2025
2 parents d87e05f + b7cbf74 commit ded1d28
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions sys/sys/libkern.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ ilog2_long_long(long long n)

#define ilog2(n) (__builtin_constant_p(n) ? ilog2_const(n) : ilog2_var(n))
#define rounddown_pow_of_two(n) ((__typeof(n))1 << ilog2(n))
#define order_base_2(n) ({ \
__typeof(n) _n = (n); \
_n == 1 ? 0 : 1 + ilog2(_n - 1); \
})
#define order_base_2(n) ilog2(2*(n)-1)
#define roundup_pow_of_two(n) ((__typeof(n))1 << order_base_2(n))

#define bitcount64(x) __bitcount64((uint64_t)(x))
Expand Down

0 comments on commit ded1d28

Please sign in to comment.