You should use Russian Peasant multiplication. It uses repeated doubling to compute all the values (b*2^i)%m
, and adds them in if the i
th bit of a
is set.
uint64_t mulmod(uint64_t a, uint64_t b, uint64_t m) {
int64_t res = 0;
while (a != 0) {
if (a & 1) res = (res + b) % m;
a >>= 1;
b = (b << 1) % m;
}
return res;
}
It improves upon your algorithm because it takes O(log(a))
time, not O(a)
time.
Caveats: unsigned, and works only if m
is 63 bits or less.