I remember reading somewhere a while back that if you are dividing by a constant there is a way to turn it into a multiplication due to the properties of modular arithmetic. I have been working on the following function and it involves performing many divisions with the same divisor. I was wondering if I could speed it up using what I described above. If so how exactly?
int ubi_div_i64(ubigint_t* a, ubi_i64_t b, ubi_i64_t* rem)
{
if(b == 0)
return UBI_MATH_ERR;
ubi_i64_t r = 0;
for(size_t i = a->used; i-- > 0;)
{
ubi_i64_t out;
__asm__("\t"
"div %[d] \n\t"
: "=a"(out), "=d"(r)
: "a"(a->data[i]), "d"(r), [d]"r"(b)
: "cc");
a->data[i] = out;
}
if(rem)
*rem = r;
return ubi_strip_leading_zeros(a);
}
Note: The divisor is 64 bit and the dividend is 128 bit.