pt-three-ways icon indicating copy to clipboard operation
pt-three-ways copied to clipboard

Unpredictable::any doesn't produce efficient code with clang

Open njames93 opened this issue 3 years ago • 0 comments

When building with clang, the Unpredictable::any workaround will still cause multiple branches, playing havoc with the branch predictor. Clang has a way to define a branch as being unpredictable and using that yields much better code and on the suzanne scene, my render time dropped from 81s -> 66s The hacked way i did this was

#if __has_builtin(__builtin_unpredictable)
#define UNPREDICTABLE(x) __builtin_unpredictable(x)
#else
#define UNPREDICTABLE(x) x
#endif
    if (UNPREDICTABLE(u < 0.0) || UNPREDICTABLE(u > 1.0)
        || UNPREDICTABLE(v < 0.0) || UNPREDICTABLE((u + v) > 1))
      continue;

For the case when that isn't available, I tried using this(which works for gcc too), but clang still emitted branches here

#elif __has_builtin(__builtin_expect_with_probability)
#define UNPREDICTABLE(x) __builtin_expect_with_probability(x, 1, 0.5)

njames93 avatar Dec 16 '21 22:12 njames93