/* ceiling division without needing floating-point operations. */
static size_t ceil_div(size_t n, size_t d)
{
return (n+d-1)/d;
}
這是採取 include/linux/math.h
當中整數除法並向上取整的巨集實作,它的原理是歸納所有可能的餘數,例如
#define INTMAX BBBB
void mpi_set_u64(mpi_t rop, uint64_t op)
{
size_t capacity = ceil_div(64, 31);
mpi_enlarge(rop, capacity);
for (size_t n = 0; n < capacity; ++n) {
rop->data[n] = op & INTMAX;
op >>= 31;
}
for (size_t n = capacity; n < rop->capacity; ++n)
rop->data[n] = 0;
}
這裡帶到了不同精確度的整數實作,以上述的函式來說是 bitops 中的 set 應用在無號64位元的整數。在多精確度整數的實作當中,可以發現它是以
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up