by danlark1 · 6y ago · 0 comments
Yet, I was not expecting it to be here. Overall, I put some knowledge hidden in Hacker's Delight book, Knuth, GMP and GNU in the article with my knowledge of low level optimizations. In the end it turned out to be a good thing to write and to submit into LLVM
if (dd.all >= dr.all) {
dd.all -= dr.all;
quo.s.low |= 1;
}
could be bool c = dd.all >= dr.all;
dd.all -= (-c & dr.all);
quo.s.low |= c;
Clang might implement this with `cmov` instructions, coded either way.I faced a similar problem implemented 64/64 division on 32-bit x86. I cribbed some code off the internet and came up with this:
https://github.com/titzer/virgil/blob/2c674dbe3512da23c4a644...
This is the code that my compiler generates for a 64/64 divide when it doesn't know that the divisor is max 32 bits. It works by using the x86 64/32 div instruction. The same trick would work on x86-64 by using the 128/64 divisions.
I am not sure how good the machine code you get out of your C++ is in the end, but it's hard to beat this handwritten assembly. Of course, you can do better on x86-64 because there are more registers and you don't have to spill on to the stack.
https://github.com/llvm/llvm-project/blob/master/compiler-rt...
Is it possible you can make a Boost licensed version of it so we can add it to D?
If you want to workaround this for now, I suggest looking into libdivide (https://github.com/ridiculousfish/libdivide), it is published with the boost license and the library contains all the needed artifacts I described in the article (unfortunately, not combined).