User: Password:
Subscribe / Log in / New account

Re: When should we use likely() / unlikely() / get_unaligned() ?

From:  Rusty Russell <>
To:  David Woodhouse <>
Subject:  Re: When should we use likely() / unlikely() / get_unaligned() ?
Date:  Sun, 8 Feb 2004 21:43:35 +1100

On Fri, 06 Feb 2004 11:06:19 +0000
David Woodhouse <> wrote:

> 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.

Yes, agreed.  But many unlikely() macros are simply there because gcc isn't
smart enough yet:

eg. fs/read_write.c:

		if (unlikely(put_user(pos, offset)))
			return -EFAULT;

It'd be better to have gcc know that this function was unlikely to return
a -ve value, and derive all the error paths itself.

It'd also be nice to be able to mark eg. printk() and BUG() as fundamentally

Sometimes, unlikely()/likely() help code readability.  But generally it
should be considered the register keyword of the 2000's: if the case isn't
ABSOLUTELY CRYSTAL CLEAR, or doesn't show up on benchmarks, distain is

   there are those who do and those who hang on and you don't see too
   many doers quoting their contemporaries.  -- Larry McVoy
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

(Log in to post comments)

Copyright © 2004, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds