3 ms·
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
by heavenlyblue 9y ago
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.
- colejohnson66 9y agoBut 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
- hrydgard 9y agoYour second snippet will just zero rdx - don't confuse the registers (rbx is in the mix in the original :)
- colejohnson66 9y agoThanks :) Fixed.
- pwdisswordfish 9y agoshr 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.