Fastest Branchless Binary Search
You’re a busy person so I’ll first jump right to it. Here it is, the fastest general (and simple) binary search C++ implementation:template <class ForwardIt, class T, class Compare>
constexpr ForwardIt sb_lower_bound(
ForwardIt first, ForwardIt last, const T& value, Compare comp) {
auto length = last - first;
while (length > 0) {
auto rem = length % 2;
length /= 2;
if (comp(first[length], value)) {
first += length + rem;
}
}
return first;
}
Same function interface as std::lower_bound, but 2x fas...
Read more at mhdm.dev