public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Sebastian Huber <sh@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] bitset: avoid pessimized code when bitset size is not constant
Date: Fri, 24 Jun 2022 05:42:14 +0000 (GMT)	[thread overview]
Message-ID: <20220624054214.539B9385021E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=96c645a0b1e8c136f0d70106b8ab3d0acf621c12

commit 96c645a0b1e8c136f0d70106b8ab3d0acf621c12
Author: Ryan Libby <rlibby@FreeBSD.org>
Date:   Tue Dec 3 17:43:57 2019 +0000

    bitset: avoid pessimized code when bitset size is not constant
    
    We have a couple optimizations for when the bitset is known to be just
    one word.  But with dynamically sized bitsets, it was actually more work
    to determine the size than just to do the necessary computation.  Now,
    only use the optimization when the size is known to be constant.
    
    Reviewed by:    markj
    Discussed with: jeff
    Sponsored by:   Dell EMC Isilon
    Differential Revision:  https://reviews.freebsd.org/D22639

Diff:
---
 newlib/libc/sys/rtems/include/sys/bitset.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/sys/rtems/include/sys/bitset.h b/newlib/libc/sys/rtems/include/sys/bitset.h
index d119c23ca..0334939c9 100644
--- a/newlib/libc/sys/rtems/include/sys/bitset.h
+++ b/newlib/libc/sys/rtems/include/sys/bitset.h
@@ -37,12 +37,19 @@
 #include <sys/_types.h>
 #include <machine/_bitcount.h>
 
+/*
+ * Whether expr is both constant and true.  Result is itself constant.
+ * Used to enable optimizations for sets with a known small size.
+ */
+#define	__constexpr_cond(expr)	(__builtin_constant_p((expr)) && (expr))
+
 #define	__bitset_mask(_s, n)						\
-	(1L << ((__bitset_words((_s)) == 1) ?				\
+	(1L << (__constexpr_cond(__bitset_words((_s)) == 1) ?		\
 	    (__size_t)(n) : ((n) % _BITSET_BITS)))
 
 #define	__bitset_word(_s, n)						\
-	((__bitset_words((_s)) == 1) ? 0 : ((n) / _BITSET_BITS))
+	(__constexpr_cond(__bitset_words((_s)) == 1) ?			\
+	 0 : ((n) / _BITSET_BITS))
 
 #define	BIT_CLR(_s, n, p)						\
 	((p)->__bits[__bitset_word(_s, n)] &= ~__bitset_mask((_s), (n)))


                 reply	other threads:[~2022-06-24  5:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220624054214.539B9385021E@sourceware.org \
    --to=sh@sourceware.org \
    --cc=newlib-cvs@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).