mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: linux-kernel@vger.kernel.org
Cc: matthew@wil.cx
Subject: When should we use likely() / unlikely() / get_unaligned() ?
Date: Fri, 06 Feb 2004 11:06:19 +0000	[thread overview]
Message-ID: <1076065578.16147.72.camel@hades.cambridge.redhat.com> (raw)

There seems to be no coherent answer to the above questions. On some
architectures likely() might bypass dynamic branch prediction, so we
shouldn't use it unless there's at _least_ a 95% probability; on others
it may simply affect code ordering and we gain a tiny benefit from it if
the probabilities aren't precisely 50/50.

Likewise for using get_unaligned() to inline the alignment fixups --
what is the ratio between the costs of inlining the fixup and of
potentially taking the exception? If the pointer is expected to be
unaligned 25% of the time, should we use get_unaligned? What if it's
50%? 75%?

These are all very arch-specific, and sometimes compiler-specific. The
likely()/unlikely()/get_unaligned() functions as they currently stand
make little sense.

I think we need to include a probability, in order for use of these
functions in _generic_ code to make any sense. So we replace
likely(condition) with probable(condition, percentage), and likewise
with get_unaligned()...


#define ARCH_PROBABILITY_HIGH 75    // These actually defined by arch code
#define ARCH_PROBABILITY_LOW 25
#define ARCH_ALIGNMENT_COST_THRESHOLD 50

#define probable(condition, pc)					\
	(__builtin_constant_p(pc)?				\
	 (((pc) > ARCH_PROBABILITY_HIGH)?			\
	      __builtin_expect((condition),1):			\
	      (((pc) < ARCH_PROBABILITY_LOW)?			\
	           __builtin_expect((condition),0):		\
		  (condition)))					\
	 :(condition))


#define get_unaligned_p(ptr, pc)						\
	((__builtin_constant_p(pc)&&(pc) < ARCH_ALIGNMENT_COST_THRESHOLD)?	\
	 (*(ptr)):get_unaligned(ptr))


In fact some uClinux architectures _cannot_ fix up alignment, and would
set ARCH_ALIGNMENT_COST_THRESHOLD to zero. 

-- 
dwmw2


             reply	other threads:[~2004-02-06 11:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-06 11:06 David Woodhouse [this message]
2004-02-08 10:43 ` Rusty Russell
2004-02-08 11:13   ` David Woodhouse
2004-02-08 23:00     ` Rusty Russell
2004-02-08 23:06       ` David Woodhouse
2004-02-08 23:11       ` David S. Miller
2004-02-08 23:14         ` David Woodhouse
2004-02-16 11:39     ` Pavel Machek

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=1076065578.16147.72.camel@hades.cambridge.redhat.com \
    --to=dwmw2@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew@wil.cx \
    /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

all inboxes | Powered by JetHome®