Hacker News new | ask | show | jobs
by benlivengood 1592 days ago
Before reading the article: In x86 assembly, add ax, bx ; rcr ax, 1 works. I guess technically that is with overflow, but using overflow bits as intended.

EDIT: it's included in the collection of methods in the article as expected.

1 comments

That's lovely. I missed it when reading the article. It's also the winner on AMD Zen architecture based on MCA analysis.

    unsigned midpoint(unsigned a, unsigned b) {
      asm("add\t%1,%0\n\t"
          "rcr\t%0"
          : "+r"(a)
          : "r"(b));
      return a;
    }
Although `(a & b) + (a ^ b) / 2` is probably the more conservative choice.