[ccan] [PATCH v3 2/2] maskn & bitops: add new modules

David Gibson david at gibson.dropbear.id.au
Wed Jul 16 01:40:29 EST 2014


On Thu, Jul 10, 2014 at 08:53:58PM -0400, Cody P Schafer wrote:
> 'maskn' provides generation of n-bit masks from a given range.
> 'bitops' provides some generic bit operations that maskn needs.
> 
> Both operate on 32-bit values only (that's just what I needed at this
> point).
> 
> There is some overlap between ilog and bitops that would
> be good to remove (fls).

[snip]
> diff --git a/ccan/bitops/bitops.h b/ccan/bitops/bitops.h
> new file mode 100644
> index 0000000..7320b35
> --- /dev/null
> +++ b/ccan/bitops/bitops.h
> @@ -0,0 +1,64 @@
> +/* CC0 (Public Domain) - see LICENSE file for details */
> +#ifndef CCAN_BITOPS_H_
> +#define CCAN_BITOPS_H_
> +
> +#include "config.h"
> +#include <ccan/compiler/compiler.h>
> +#include <stdint.h>
> +
> +/**
> + * bit_mask - given a number of bits, returns a right (LSB) justified bit mask
> + *            with that many bits set
> + *
> + * Note that the number of bits must be less than or equal to CHAR_BIT *
> + * sizeof(uintmax_t), ie: the number of bits in a uintmax_t.
> + */
> +CONST_FUNCTION
> +inline uintmax_t bit_mask(unsigned bits);
> +
> +/**
> + * ctz_32 - count trailing zeros
> + *
> + * Returns the number of 'trailing' zeros in v, counting from the least
> + * significant bit until the first set (non-zero, one) bit.
> + *
> + * If no bits are set, returns 32
> + */
> +CONST_FUNCTION
> +inline unsigned clz_32(uint32_t v);
> +
> +/**
> + * clz_32 - count leading zeros
> + *
> + * Returns the number of 'leading' zeros in v, counting from the most
> + * significant bit until the first set (non-zero, one) bit.
> + *
> + * If no bits are set, returns 32 (CHAR_BIT * sizeof(uint32_t))
> + */
> +CONST_FUNCTION
> +inline unsigned ctz_32(uint32_t v);

Your descriptions and prototypes for clz/ctz are swapped with respect
to each other.

> +/**
> + * fls_32 - find last set
> + *
> + * Returns the bit index of the last set bit (last when scanning from LSB to
> + * MSB), where 1 is the least significant bit and 32 is the most significant
> + * bit. If no bits are set, returns 0.
> + */
> +CONST_FUNCTION
> +inline unsigned fls_32(uint32_t v);
> +
> +/**
> + * fls_r1_32 - find last set rotated by 1
> + *
> + * This is the same as fls but rotates the output by 1.

I don't think the meaning of "rotates" is clear in this context.

I believe it amounts to fls_r1_32 == (fls_32 - 1) % 33

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20140716/0796048f/attachment.sig>


More information about the ccan mailing list