Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Three comparison/branch pairs are not free.

Clang will optimize

  bool swap_if(bool c, 
      int& a, int& b) {
    int ta = a, tb = b;
    a = (-c & tb)|((c-1) & ta);
    b = (-c & ta)|((c-1) & tb);
    return c;
  }
into cmov instructions. Used in a quicksort partition loop as

  l += swap_if(
    *r <= pv, *l, *r);
It makes quicksort fully 2x as fast as the usual branch.


Quicksort comparisons are obviously not predictable




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: