4 ms·
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. EDI
by benlivengood 5y 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.
- jart 5y agoThat'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.