This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

if statement and Branch Penalty

(Sorry for my limited English skill and technical skill.)

Around one year ago, I was working on LPC1343/LPC1313 with SPI-Flash, and forced to use 12MHz IRC/Main Clock. So performance is a critical issue.

void SPI_Flash_ReadData( uint32_t Address, uint8_t Buffer[], uint32_t Length )
{
    uint32_t i;

    /*
        Branch Penalty?
        1 to 256 -> Valid
            0        -> 0x100
            1 to 255 -> 1 to 255
            256      -> 0x100
    */
    Length--;
    Length &= 0xFF;
    Length++;

    SPI_Flash_WaitDone();

    SPI_FLASH_CS_LOW();
    /* ------------- */
    SPI_TxRx_Byte(CMD_FAST_READ);

I need to check the Length variable, and since SPI_Flash_ReadData() is a frequently used function, I tend to avoid the Branch Penalty.

But I really don't know how to do it properly. I guess:

1. Branch Penalty is a more serious problem on X86, due to its long pipeline.
2. In most cases, I can use if statement as I like.
3. In some rare cases, I should consider how to avoid Branch Penalty, but Branch is just something we have to live with.

Any comments are welcome.

0