News for hackers. Stuff that matters.
Re:Why is this C++ code faster than my hand-written assembly... by heavenlyblue on Tuesday August 22, 2017 @11:25AM (#15073312)
The issue is that division by a constant of 2, when it's highly optimisable - must already be embodied in the algorithm on a higher level.

Essentially you must have a piece of code where that constant is just a parameter to the algorithm that suddenly when set to a value of 2 makes it that much more efficient.

Now here's the bummer: if you had already planned for it, then writing custom assembly for it is not going to make the algorithm that much faster. But when you didn't - you're rewriting the whole of the algorithm anyway.

Re:Why is this C++ code faster than my hand-written assembly... by colejohnson66 on Tuesday August 22, 2017 @01:23PM (#15074512)
But on x86, division by 2 on an unsigned integer is the same as shifting to the right by 1. So instead of

    even:
        mov rbx, 2
        xor rdx, rdx
        div rbx
the OP should've done something like

    even:
        xor rdx, rdx % possible to remove?
        shr rbx, 1
Re:Why is this C++ code faster than my hand-written assembly... by pwdisswordfish on Tuesday August 22, 2017 @05:19PM (#15076497)

    shr rax, 1
div rbx will divide the 128-bit value in (rdx, rax) by rbx, then store the quotient in rax and the remainder in rdx.
Re:Why is this C++ code faster than my hand-written assembly... by hrydgard on Tuesday August 22, 2017 @01:45PM (#15074740)
Your second snippet will just zero rdx - don't confuse the registers (rbx is in the mix in the original :)
Re:Why is this C++ code faster than my hand-written assembly... by colejohnson66 on Tuesday August 22, 2017 @02:26PM (#15075119)
Thanks :) Fixed.