when should one go for likely and unlikely

Discussion in 'C Programming' started by Nitin Tripathi, Sep 10, 2013.

  1. When should one go for likely and unlikely in c?

    Thanks,
    Nitin Tripathi.
     
    Nitin Tripathi, Sep 10, 2013
    #1
    1. Advertisements

  2. Nitin Tripathi

    Jorgen Grahn Guest

    You mean the likely() and unlikely() macros which e.g. the Linux
    kernel sources have. They expand to gcc-specific things which are
    supposed to help generate better code, by telling the compiler which
    case it should optimize for:

    if(unlikely(weird_error_case)) {
    error_handling();
    }

    I suspect the answer is "rarely, and mostly when profiling tells you
    it makes a difference in your target environment".

    But I cannot say for sure. I certainly don't want to see them in
    /every/ if and while. And of course you cannot implement them in a
    portable way, except as do-nothing macros.

    /Jorgen
     
    Jorgen Grahn, Sep 10, 2013
    #2
    1. Advertisements

  3. (snip)
    The original Fortran had a FREQUENCY statement that allowed one
    to specify the relative frequency (ratio) of choices for IF
    and computed-GOTO statements, and also an estimate for the number
    of iterations of a DO loop. That allowed the compiler to optimize
    some a little better than it might otherwise.

    -- glen
     
    glen herrmannsfeldt, Sep 10, 2013
    #3
  4. I believe he's referring to macros named "likely" and "unlikely".
    He may not be aware that they're specific to the Linux kernel and
    not standard features of C. Given that assumption, there's nothing
    *seriously* wrong with the question, though it certainly could have
    been phrased more clearly.

    Nitin: There are no features in the standard C language or library
    called "likely" or "unlikely".
     
    Keith Thompson, Sep 11, 2013
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.