diff mbox series

[v1,3/3] tools: add API to work with sevaral bits at once

Message ID 20201209155452.28376-3-olaf@aepfle.de (mailing list archive)
State New, archived
Headers show
Series [v1,1/3] tools: allocate bitmaps in units of unsigned long | expand

Commit Message

Olaf Hering Dec. 9, 2020, 3:54 p.m. UTC
Introduce new API to test if a fixed number of bits is clear or set,
and clear or set them all at once.

The caller has to make sure the input bitnumber is a multiply of BITS_PER_LONG.

This API avoids the loop over each bit in a known range just to see
if all of them are either clear or set.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
---
 tools/libs/ctrl/xc_bitops.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Wei Liu Dec. 15, 2020, 4:22 p.m. UTC | #1
On Wed, Dec 09, 2020 at 04:54:51PM +0100, Olaf Hering wrote:
> Introduce new API to test if a fixed number of bits is clear or set,
> and clear or set them all at once.
> 
> The caller has to make sure the input bitnumber is a multiply of BITS_PER_LONG.
> 
> This API avoids the loop over each bit in a known range just to see
> if all of them are either clear or set.
> 
> Signed-off-by: Olaf Hering <olaf@aepfle.de>

I would rather these be introduced along side their callers.

> ---
>  tools/libs/ctrl/xc_bitops.h | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/tools/libs/ctrl/xc_bitops.h b/tools/libs/ctrl/xc_bitops.h
> index f0bac4a071..92f38872fb 100644
> --- a/tools/libs/ctrl/xc_bitops.h
> +++ b/tools/libs/ctrl/xc_bitops.h
> @@ -77,4 +77,29 @@ static inline void bitmap_or(void *_dst, const void *_other,
>          dst[i] |= other[i];
>  }
>  
> +static inline int test_bit_long_set(unsigned long nr_base, const void *_addr)

What's wrong with requiring the input addr be const unsigned long *?
Olaf Hering Dec. 15, 2020, 4:29 p.m. UTC | #2
Am Tue, 15 Dec 2020 16:22:44 +0000
schrieb Wei Liu <wl@xen.org>:

> What's wrong with requiring the input addr be const unsigned long *?

Probably nothing. In the end I just borrowed the prototypes from the other functions in this file.

I will resend with this change once I have the consumers ready.

Olaf
Wei Liu Dec. 15, 2020, 4:30 p.m. UTC | #3
On Tue, Dec 15, 2020 at 05:29:17PM +0100, Olaf Hering wrote:
> Am Tue, 15 Dec 2020 16:22:44 +0000
> schrieb Wei Liu <wl@xen.org>:
> 
> > What's wrong with requiring the input addr be const unsigned long *?
> 
> Probably nothing. In the end I just borrowed the prototypes from the other functions in this file.
> 
> I will resend with this change once I have the consumers ready.

Okay.

I will push the first two shortly.

Wei.

> 
> Olaf
diff mbox series

Patch

diff --git a/tools/libs/ctrl/xc_bitops.h b/tools/libs/ctrl/xc_bitops.h
index f0bac4a071..92f38872fb 100644
--- a/tools/libs/ctrl/xc_bitops.h
+++ b/tools/libs/ctrl/xc_bitops.h
@@ -77,4 +77,29 @@  static inline void bitmap_or(void *_dst, const void *_other,
         dst[i] |= other[i];
 }
 
+static inline int test_bit_long_set(unsigned long nr_base, const void *_addr)
+{
+    const unsigned long *addr = _addr;
+    unsigned long val = addr[nr_base / BITS_PER_LONG];
+    return val == ~0;
+}
+
+static inline int test_bit_long_clear(unsigned long nr_base, const void *_addr)
+{
+    const unsigned long *addr = _addr;
+    unsigned long val = addr[nr_base / BITS_PER_LONG];
+    return val == 0;
+}
+
+static inline void clear_bit_long(unsigned long nr_base, void *_addr)
+{
+    unsigned long *addr = _addr;
+    addr[nr_base / BITS_PER_LONG] = 0;
+}
+
+static inline void set_bit_long(unsigned long nr_base, void *_addr)
+{
+    unsigned long *addr = _addr;
+    addr[nr_base / BITS_PER_LONG] = ~0;
+}
 #endif  /* XC_BITOPS_H */