https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122116

            Bug ID: 122116
           Summary: __builtin_expect_with_probability vs
                    __builtin_unpredictable
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rockeet at gmail dot com
  Target Milestone: ---

We expect clang's __builtin_unpredictable can be emulated by
GCC's __builtin_expect_with_probability, but his is not true,
see: https://godbolt.org/z/zf33a95PM

#if !defined(__clang__)
  #define __builtin_unpredictable(x) __builtin_expect_with_probability(x, 1,
0.5)
#endif
size_t binary_search_byte(const byte_t* data, size_t len, byte_t key) {
        size_t lo = 0, hi = len;
        while (lo < hi) {
                size_t mid = (lo + hi) / 2;
                if (__builtin_unpredictable(data[mid] < key))
                        lo = mid + 1;
                else
                        hi = mid;
        }
        return lo;
}

__builtin_unpredictable direct clang generate cmov instead
of branch in the loop, we can not direct GCC to generate
branchless code with cmov.

Reply via email to