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 9175777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:19+00:00 2026-06-17T16:58:19+00:00

How do I perform an (almost-)branch-less binary search on arbitrary sorted arrays in a

  • 0

How do I perform an (almost-)branch-less binary search on arbitrary sorted arrays in a preferably portable fashion? (e.g. code that helps compilers generate the CMOV instruction would be great for this.)

By “almost” I mean “containing as few branches as possible”.

  • 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-17T16:58:20+00:00Added an answer on June 17, 2026 at 4:58 pm

    Here’s a version of std::lower_bound which had only 1 branch (namely, the begin != end test) when I tested it with Visual C++ 2012 (64-bit):

    template<class FwdIt, class T, class P>
    FwdIt branchless_lower_bound(FwdIt begin, FwdIt end, T const &val, P pred)
    {
        while (begin != end)
        {
            FwdIt middle(begin);
            std::advance(middle, std::distance(begin, end) >> 1);
            FwdIt middle_plus_one(middle);
            ++middle_plus_one;
            bool const b = pred(*middle, val);
            begin = b ? middle_plus_one : begin;
            end = b ? end : middle;
        }
        return begin;
    }
    

    32-bit with SSE2 support would probably be able to use the Conditional-Move instruction as well, to gain similar speed.

    Now the speed should be competitive with linear search for small arrays… but it might be worth checking.


    Interestingly, I found that for a vector<int> up to size (approximately) 45 on my CPU, a linear search is still faster! Not sure why though, or if my measurement was accurate…


    Also turns out that this isn’t any faster than a branching binary search on my i5 CPU.

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

Sidebar

Related Questions

I have a code that has been working for almost 4 years (since boost
The following code is a complete XNA 3.1 program almost unaltered to that code
I have to implement a search feature which is able to quickly perform arbitrary
I have three C# methods that almost perform identically. More specifically there is a
In visual studio when we perform a search in our source code it works
I perform this kind of query in my Python code to conn = rdbms.connect(instance=_INSTANCE_NAME,
I need to implement a grid in asp.net that behaves almost exactly like MS
I have an app with a widget that loads an activity to perform an
I was under the impression that using setjmp() and longjmp() in C++ was almost
I have a a number of jar files that perform rmi. These are all

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.