I am in the process of rewriting a big integer class. the older version is fast but makes some non-portable assumptions (inline assembly). the new class takes an additional Traits template parameter that allows customization of the internal adding functionality, etc.
what I'm working on right now is a generic function that will work with any unsigned type to use with the default Traits.
here's what I've come up with:
my concern is the efficiency of the algorithm. is there a better/faster way to do this?Code:
template <typename Unsigned>
adc(Unsigned * dst, Unsigned src, Unsigned carry = 0)
one = 1,
half = (std::numeric_limits<Unsigned>::max)() >> one;
tmp = *dst;
*dst += src + carry;
(src >> one) + (tmp >> one) + (((src & one) + (tmp & one) + carry) >> one) > half;