[ovs-dev] [PATCH v3 1/2] lib/util: Add clz() and clz64().

Ben Pfaff blp at nicira.com
Thu Nov 21 22:48:12 UTC 2013


On Thu, Nov 21, 2013 at 02:25:30PM -0800, Jarno Rajahalme wrote:
> Count leading zeroes using builtins if available.
> 
> Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>

Could we rename raw_clz() to raw_clz64()?  clz is a function that
doesn't make sense without a defined length.

> +static inline int
> +raw_clz(uint64_t n)
> +{
> +    /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
> +     * a plain __builtin_ctzll() here always generates an out-of-line function
> +     * call.  The test below helps it to emit a single 'bsf' instruction. */
> +    return (__builtin_constant_p(n <= UINT32_MAX) && n <= UINT32_MAX
> +            ? __builtin_clz(n) + 32
> +            : __builtin_clzll(n));

In my tests the above trick isn't necessary for clz.  Just writing
__builtin_clzll() generates good code.  (But the comment would need an
update anyway: s/ctz/clz/ and s/bsf/bsr/.)



More information about the dev mailing list