From: Tim Deegan <tim@xen.org>
To: xen-devel@lists.xen.org
Subject: [PATCH 4/4] bitmaps/bitops: Clarify tests for small constant size.
Date: Thu, 28 Nov 2013 16:37:45 +0000 [thread overview]
Message-ID: <1385656665-12551-5-git-send-email-tim@xen.org> (raw)
In-Reply-To: <1385656665-12551-1-git-send-email-tim@xen.org>
No semantic changes, just makes the control flow a bit clearer.
I was looking at this bcause the (-!__builtin_constant_p(x) | x__)
formula is too clever for Coverity, but in fact it always takes me a
minute or two to understand it too. :)
Signed-off-by: Tim Deegan <tim@xen.org>
---
xen/include/asm-x86/bitops.h | 60 +++++++++++++++++++-------------------------
xen/include/xen/bitmap.h | 30 +++++++++++++---------
2 files changed, 44 insertions(+), 46 deletions(-)
diff --git a/xen/include/asm-x86/bitops.h b/xen/include/asm-x86/bitops.h
index ab21d92..810beb1 100644
--- a/xen/include/asm-x86/bitops.h
+++ b/xen/include/asm-x86/bitops.h
@@ -335,23 +335,19 @@ static inline unsigned int __scanbit(unsigned long val, unsigned long max)
* @offset: The bitnumber to start searching at
* @size: The maximum size to search
*/
-#define find_next_bit(addr, size, off) ({ \
- unsigned int r__ = (size); \
- unsigned int o__ = (off); \
- switch ( -!__builtin_constant_p(size) | r__ ) \
- { \
- case 0: (void)(addr); break; \
- case 1 ... BITS_PER_LONG: \
- r__ = o__ + __scanbit(*(const unsigned long *)(addr) >> o__, r__); \
- break; \
- default: \
- if ( __builtin_constant_p(off) && !o__ ) \
- r__ = __find_first_bit(addr, r__); \
- else \
- r__ = __find_next_bit(addr, r__, o__); \
- break; \
- } \
- r__; \
+#define find_next_bit(addr, size, off) ({ \
+ unsigned int r__; \
+ unsigned int s__ = (size); \
+ unsigned int o__ = (off); \
+ if ( __builtin_constant_p(size) && s__ == 0 ) \
+ r__ = s__; \
+ else if ( __builtin_constant_p(size) && s__ <= BITS_PER_LONG ) \
+ r__ = o__ + __scanbit(*(const unsigned long *)(addr) >> o__, s__); \
+ else if ( __builtin_constant_p(off) && !o__ ) \
+ r__ = __find_first_bit(addr, s__); \
+ else \
+ r__ = __find_next_bit(addr, s__, o__); \
+ r__; \
})
/**
@@ -370,23 +366,19 @@ static inline unsigned int __scanbit(unsigned long val, unsigned long max)
* @offset: The bitnumber to start searching at
* @size: The maximum size to search
*/
-#define find_next_zero_bit(addr, size, off) ({ \
- unsigned int r__ = (size); \
- unsigned int o__ = (off); \
- switch ( -!__builtin_constant_p(size) | r__ ) \
- { \
- case 0: (void)(addr); break; \
- case 1 ... BITS_PER_LONG: \
- r__ = o__ + __scanbit(~*(const unsigned long *)(addr) >> o__, r__); \
- break; \
- default: \
- if ( __builtin_constant_p(off) && !o__ ) \
- r__ = __find_first_zero_bit(addr, r__); \
- else \
- r__ = __find_next_zero_bit(addr, r__, o__); \
- break; \
- } \
- r__; \
+#define find_next_zero_bit(addr, size, off) ({ \
+ unsigned int r__; \
+ unsigned int s__ = (size); \
+ unsigned int o__ = (off); \
+ if ( __builtin_constant_p(size) && s__ == 0 ) \
+ r__ = s__; \
+ else if ( __builtin_constant_p(size) && s__ <= BITS_PER_LONG ) \
+ r__ = o__ + __scanbit(~*(const unsigned long *)(addr) >> o__, s__); \
+ else if ( __builtin_constant_p(off) && !o__ ) \
+ r__ = __find_first_zero_bit(addr, s__); \
+ else \
+ r__ = __find_next_zero_bit(addr, s__, o__); \
+ r__; \
})
/**
diff --git a/xen/include/xen/bitmap.h b/xen/include/xen/bitmap.h
index b5ec455..166e1a0 100644
--- a/xen/include/xen/bitmap.h
+++ b/xen/include/xen/bitmap.h
@@ -110,13 +110,14 @@ extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
#define bitmap_bytes(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
-#define bitmap_switch(nbits, zero_ret, small, large) \
- switch (-!__builtin_constant_p(nbits) | (nbits)) { \
- case 0: return zero_ret; \
- case 1 ... BITS_PER_LONG: \
- small; break; \
- default: \
- large; break; \
+#define bitmap_switch(nbits, zero, small, large) \
+ unsigned int n__ = (nbits); \
+ if (__builtin_constant_p(nbits) && n__ == 0) { \
+ zero; \
+ } else if (__builtin_constant_p(nbits) && n__ <= BITS_PER_LONG) { \
+ small; \
+ } else { \
+ large; \
}
static inline void bitmap_zero(unsigned long *dst, int nbits)
@@ -191,7 +192,8 @@ static inline void bitmap_complement(unsigned long *dst, const unsigned long *sr
static inline int bitmap_equal(const unsigned long *src1,
const unsigned long *src2, int nbits)
{
- bitmap_switch(nbits, -1,
+ bitmap_switch(nbits,
+ return -1,
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)),
return __bitmap_equal(src1, src2, nbits));
}
@@ -199,7 +201,8 @@ static inline int bitmap_equal(const unsigned long *src1,
static inline int bitmap_intersects(const unsigned long *src1,
const unsigned long *src2, int nbits)
{
- bitmap_switch(nbits, -1,
+ bitmap_switch(nbits,
+ return -1,
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0,
return __bitmap_intersects(src1, src2, nbits));
}
@@ -207,21 +210,24 @@ static inline int bitmap_intersects(const unsigned long *src1,
static inline int bitmap_subset(const unsigned long *src1,
const unsigned long *src2, int nbits)
{
- bitmap_switch(nbits, -1,
+ bitmap_switch(nbits,
+ return -1,
return !((*src1 & ~*src2) & BITMAP_LAST_WORD_MASK(nbits)),
return __bitmap_subset(src1, src2, nbits));
}
static inline int bitmap_empty(const unsigned long *src, int nbits)
{
- bitmap_switch(nbits, -1,
+ bitmap_switch(nbits,
+ return -1,
return !(*src & BITMAP_LAST_WORD_MASK(nbits)),
return __bitmap_empty(src, nbits));
}
static inline int bitmap_full(const unsigned long *src, int nbits)
{
- bitmap_switch(nbits, -1,
+ bitmap_switch(nbits,
+ return -1,
return !(~*src & BITMAP_LAST_WORD_MASK(nbits)),
return __bitmap_full(src, nbits));
}
--
1.8.4.rc3
next prev parent reply other threads:[~2013-11-28 16:37 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-28 16:37 [PATCH 0/4] xen: more Coverity-inspired tidying Tim Deegan
2013-11-28 16:37 ` [PATCH 1/4] common/vsprintf: Explicitly treat negative lengths as 'unlimited' Tim Deegan
2013-11-28 16:48 ` Andrew Cooper
2013-11-29 10:45 ` Ian Campbell
2013-11-28 16:37 ` [PATCH 2/4] x86/shadow: Drop shadow_mode_trap_reads() Tim Deegan
2013-11-28 16:37 ` [PATCH 3/4] x86/mem_sharing: drop unused variable Tim Deegan
2013-11-28 16:50 ` Andrew Cooper
2013-11-28 16:37 ` Tim Deegan [this message]
2013-11-29 10:07 ` [PATCH 4/4] bitmaps/bitops: Clarify tests for small constant size Jan Beulich
2013-11-29 10:37 ` Tim Deegan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1385656665-12551-5-git-send-email-tim@xen.org \
--to=tim@xen.org \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).