diff mbox series

[v20210601,01/38] tools: add API to work with sevaral bits at once

Message ID 20210601161118.18986-2-olaf@aepfle.de (mailing list archive)
State Superseded
Headers show
Series leftover from 2020 | expand

Commit Message

Olaf Hering June 1, 2021, 4:10 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

Juergen Gross June 2, 2021, 6:19 a.m. UTC | #1
On 01.06.21 18:10, 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.

More precise: the new functions check whether BITS_PER_LONG bits are
set or clear.

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

s/multiply/multiple/

> 
> 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(+)
> 
> 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)

Make return type bool (here and below)?

> +{
> +    const unsigned long *addr = _addr;
> +    unsigned long val = addr[nr_base / BITS_PER_LONG];

Add a blank line here (same below).

> +    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 */
> 


Juergen
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 */