Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1272750 > unrolled thread

[PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero}

Started byJia He <hejianet@gmail.com>
First post2015-11-19 03:40 +0100
Last post2015-11-20 15:00 +0100
Articles 3 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero} Jia He <hejianet@gmail.com> - 2015-11-19 03:40 +0100
    Re: [PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero} Geert Uytterhoeven <geert@linux-m68k.org> - 2015-11-20 10:00 +0100
    Re: [PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero} Michal Nazarewicz <mina86@mina86.com> - 2015-11-20 15:00 +0100

#1272750 — [PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero}

FromJia He <hejianet@gmail.com>
Date2015-11-19 03:40 +0100
Subject[PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero}
Message-ID<qwniF-3dU-1@gated-at.bofh.it>
This patch introduces 2 lightweight bit api.
all_bit_is_zero return 1 if the bit string is all zero.
The addr is the start address, the size is the bit size of the bit string.
all_bit_is_one is the opposite.

Signed-off-by: Jia He <hejianet@gmail.com>
---
 lib/find_bit.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/lib/find_bit.c b/lib/find_bit.c
index 18072ea..1d56d8d 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -131,6 +131,56 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
 EXPORT_SYMBOL(find_last_bit);
 #endif
 
+#ifndef all_bit_is_zero
+/*
+ * return val: 1 means all bit is zero
+ */
+unsigned int all_bit_is_zero(const unsigned long *addr, unsigned size)
+{
+	unsigned long idx;
+	unsigned long mask = size;
+
+	if (unlikely(size == 0))
+		return 1;
+
+	if (size > BITS_PER_LONG) {
+		for (idx = 0; idx * BITS_PER_LONG < size; idx++)
+			if (addr[idx])
+				return 0;
+
+		mask = size - (idx - 1) * BITS_PER_LONG;
+	}
+
+	return !(*addr & BITMAP_LAST_WORD_MASK(mask));
+}
+EXPORT_SYMBOL(all_bit_is_zero);
+#endif
+
+#ifndef all_bit_is_one
+/*
+ * return val: 1 means all bit is one
+ */
+unsigned int all_bit_is_one(const unsigned long *addr, unsigned size)
+{
+	unsigned long idx;
+	unsigned long mask = size;
+
+	if (unlikely(size == 0))
+		return 1;
+
+	if (size > BITS_PER_LONG) {
+		for (idx = 0; idx * BITS_PER_LONG < size; idx++)
+			if (~addr[idx])
+				return 0;
+
+		mask = size - (idx - 1) * BITS_PER_LONG;
+	}
+
+	return !(~(*addr) & BITMAP_LAST_WORD_MASK(mask));
+}
+EXPORT_SYMBOL(all_bit_is_one);
+#endif
+
 #ifdef __BIG_ENDIAN
 
 /* include/linux/byteorder does not support "unsigned long" type */
-- 
2.5.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1273869

FromGeert Uytterhoeven <geert@linux-m68k.org>
Date2015-11-20 10:00 +0100
Message-ID<qwPHZ-4To-41@gated-at.bofh.it>
In reply to#1272750
On Thu, Nov 19, 2015 at 3:31 AM, Jia He <hejianet@gmail.com> wrote:
> --- a/lib/find_bit.c
> +++ b/lib/find_bit.c
> @@ -131,6 +131,56 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
>  EXPORT_SYMBOL(find_last_bit);
>  #endif
>
> +#ifndef all_bit_is_zero
> +/*
> + * return val: 1 means all bit is zero
> + */
> +unsigned int all_bit_is_zero(const unsigned long *addr, unsigned size)
> +{
> +       unsigned long idx;
> +       unsigned long mask = size;
> +
> +       if (unlikely(size == 0))
> +               return 1;
> +
> +       if (size > BITS_PER_LONG) {
> +               for (idx = 0; idx * BITS_PER_LONG < size; idx++)

Please move the multiplication (yes, a shift) out of the loop. Not all CPUs
have barrel shifters.

> +                       if (addr[idx])
> +                               return 0;
> +
> +               mask = size - (idx - 1) * BITS_PER_LONG;
> +       }
> +
> +       return !(*addr & BITMAP_LAST_WORD_MASK(mask));
> +}
> +EXPORT_SYMBOL(all_bit_is_zero);

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1274072

FromMichal Nazarewicz <mina86@mina86.com>
Date2015-11-20 15:00 +0100
Message-ID<qwUoh-7Vs-3@gated-at.bofh.it>
In reply to#1272750
On Thu, Nov 19 2015, Jia He wrote:
> This patch introduces 2 lightweight bit api.
> all_bit_is_zero return 1 if the bit string is all zero.
> The addr is the start address, the size is the bit size of the bit string.
> all_bit_is_one is the opposite.
>
> Signed-off-by: Jia He <hejianet@gmail.com>
> ---
>  lib/find_bit.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 50 insertions(+)
>
> diff --git a/lib/find_bit.c b/lib/find_bit.c
> index 18072ea..1d56d8d 100644
> --- a/lib/find_bit.c
> +++ b/lib/find_bit.c
> @@ -131,6 +131,56 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
>  EXPORT_SYMBOL(find_last_bit);
>  #endif
>  
> +#ifndef all_bit_is_zero
> +/*
> + * return val: 1 means all bit is zero
> + */
> +unsigned int all_bit_is_zero(const unsigned long *addr, unsigned
> size)

Why does it return unsigned int, not bool?

> +{
> +	unsigned long idx;
> +	unsigned long mask = size;
> +
> +	if (unlikely(size == 0))
> +		return 1;
> +
> +	if (size > BITS_PER_LONG) {
> +		for (idx = 0; idx * BITS_PER_LONG < size; idx++)
> +			if (addr[idx])
> +				return 0;
> +
> +		mask = size - (idx - 1) * BITS_PER_LONG;

Uh?

> +	}
> +
> +	return !(*addr & BITMAP_LAST_WORD_MASK(mask));

BITMAP_LAST_WORD_MASK takes size not mask.

> +}

The whole implementation seems weird, this seems much better to me:

unsigned int all_bit_is_zero(const unsigned long *addr, unsigned size)
{
	for (; size > BITS_PER_LONG; size -= BITS_PER_LONG, ++addr)
		if (*addr)
			return 0;
	return !size || !(*addr & BITMAP_LAST_WORD_MASK(size));
}

But to be honest I’m not entirely sure this is worth the effort.
find_next_bit and find_next_zero_bit may do a little bit more work but
some architectures have specialised optimised versions which will be
lost if all_bit_is_zero and all_bit_is_one are introduced.

> +EXPORT_SYMBOL(all_bit_is_zero);
> +#endif
> +
> +#ifndef all_bit_is_one
> +/*
> + * return val: 1 means all bit is one
> + */
> +unsigned int all_bit_is_one(const unsigned long *addr, unsigned size)
> +{
> +	unsigned long idx;
> +	unsigned long mask = size;
> +
> +	if (unlikely(size == 0))
> +		return 1;
> +
> +	if (size > BITS_PER_LONG) {
> +		for (idx = 0; idx * BITS_PER_LONG < size; idx++)
> +			if (~addr[idx])
> +				return 0;
> +
> +		mask = size - (idx - 1) * BITS_PER_LONG;
> +	}
> +
> +	return !(~(*addr) & BITMAP_LAST_WORD_MASK(mask));
> +}
> +EXPORT_SYMBOL(all_bit_is_one);
> +#endif
> +
>  #ifdef __BIG_ENDIAN
>  
>  /* include/linux/byteorder does not support "unsigned long" type */
> -- 
> 2.5.0
>

-- 
Best regards,                                            _     _
.o. | Liege of Serenely Enlightened Majesty of         o' \,=./ `o
..o | Computer Science,  ミハウ “mina86” ナザレヴイツ  (o o)
ooo +--<mpn@google.com>--<xmpp:mina86@jabber.org>-----ooO--(_)--Ooo--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web