On Tuesday, 23 February 2021 at 03:53:00 UTC, tsbockman wrote:
size_t alignedSize(size_t typeSize, size_t typeAlignment) pure
@safe nothrow @nogc {
version(assert) {
import core.bitop : bsr;
assert(typeAlignment == (size_t(1) <<
bsr(typeAlignment)));
}
size_t ret = typeSize & ~(typeAlignment - 1);
ret += (ret < typeSize)? typeAlignment : 0;
return ret;
}
Better:
size_t alignedSize(size_t typeSize, size_t typeAlignment) pure
@safe nothrow @nogc {
version(assert) {
import core.bitop : bsr;
assert(typeAlignment == (size_t(1) <<
bsr(typeAlignment)));
}
const alignMask = typeAlignment - 1;
return (typeSize + alignMask) & ~alignMask;
}