Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:28:37+00:00 2026-06-09T20:28:37+00:00

I have some critical branching code inside a loop that’s run about 2^26 times.

  • 0

I have some critical branching code inside a loop that’s run about 2^26 times. Branch prediction is not optimal because m is random. How would I remove the branching, possibly using bitwise operators?

bool m;
unsigned int a;
const unsigned int k = ...; // k >= 7
if(a == 0)
    a = (m ? (a+1) : (k));
else if(a == k)
    a = (m ?     0 : (a-1));
else
    a = (m ? (a+1) : (a-1));

And here is the relevant assembly generated by gcc -O3:

.cfi_startproc
movl    4(%esp), %edx
movb    8(%esp), %cl
movl    (%edx), %eax
testl   %eax, %eax
jne L15
cmpb    $1, %cl
sbbl    %eax, %eax
andl    $638, %eax
incl    %eax
movl    %eax, (%edx)
ret
L15:
cmpl    $639, %eax
je  L23
testb   %cl, %cl
jne L24
decl    %eax
movl    %eax, (%edx)
ret
L23:
cmpb    $1, %cl
sbbl    %eax, %eax
andl    $638, %eax
movl    %eax, (%edx)
ret
L24:
incl    %eax
movl    %eax, (%edx)
ret
.cfi_endproc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:28:38+00:00Added an answer on June 9, 2026 at 8:28 pm

    The branch-free division-free modulo could have been useful, but testing shows that in practice, it isn’t.

    const unsigned int k = 639;
    void f(bool m, unsigned int &a)
    {
        a += m * 2 - 1;
        if (a == -1u)
            a = k;
        else if (a == k + 1)
            a = 0;
    }
    

    Testcase:

    unsigned a = 0;
    f(false, a);
    assert(a == 639);
    f(false, a);
    assert(a == 638);
    f(true, a);
    assert(a == 639);
    f(true, a);
    assert(a == 0);
    f(true, a);
    assert(a == 1);
    f(false, a);
    assert(a == 0);
    

    Actually timing this, using a test program:

    int main()
    {
        for (int i = 0; i != 10000; i++)
        {
            unsigned int a = k / 2;
            while (a != 0) f(rand() & 1, a);
        }
    }
    

    (Note: there’s no srand, so results are deterministic.)

    My original answer: 5.3s

    The code in the question: 4.8s

    Lookup table: 4.5s (static unsigned lookup[2][k+1];)

    Lookup table: 4.3s (static unsigned lookup[k+1][2];)

    Eric’s answer: 4.2s

    This version: 4.0s

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok.. here is some background on the issue. I have some 'critical' code that
C#, .NET4. We have some performance critical code that is causing some problems. It
I have some (php) code that performs critical updates on an InnoDB table. I'm
I have some code that will change the background color of a specific label
I have some code here that uses bitsets to store many 1 bit values
I have some parsing code that allows for escape sequences to be entered into
I have some performance-heavy code that performs bit manipulations. It can be reduced to
I have some image processing code that loops through 2 multi-dimensional byte arrays (of
I have some arbitrary pixel data that I want to save as a PNG.
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.