flâneur — a map of the web's best reading

Beautiful Branchless Binary Search | Probably Dance

probablydance.com · 4,920 words · saved by 1 readers

I read a blog post by Alex Muscar, “Beautiful Binary Search in D“. It describes a binary search called “Shar’s algorithm”. I’d never heard of it and it’s impossible to google, but looking at the algorithm I couldn’t help but think “this is branchless.” And who knew that there could be a branchless binary search? So I did the work to translate it into a algorithm for C++ iterators, no longer requiring one-based indexing or fixed-size arrays. In GCC it is more than twice as fast as std::lower_bound, which is already a very high quality binary search. The search loop is simple and the generated assembly is beautiful. I’m astonished that this exists and nobody seems to be using it… Lets start with the code: I said the search loop is simple, but unfortunately the setup in lines 4 to 15 is not. Lets skip it for now. Most of the work happens in the loop in lines 16 to 20. The loop may not look branchless because I clearly have a loop conditional and an if-statement in the loop body. Let me de

Beautiful Branchless Binary Search | Probably Dance Probably Dance I can program and like games Beautiful Branchless Binary Search by Malte Skarupke I read a blog post by Alex Muscar, “ Beautiful Binary Search in D “. It describes a binary search called “Shar’s algorithm”. I’d never heard of it and it’s impossible to google, but looking at the algorithm I couldn’t help but think “this is branchless.” And who knew that there could be a branchless binary search? So I did the work to translate it into a algorithm for C++ iterators, no lo

Explore this link on the map →

saved by

related reading