I've been writing a small FAT-8 type filesystem for something and one of the things I need to do is round a size up to a cluster size for example. Say a cluster size of 512, it would take numbers like:
0 --> 0
1 --> 512
2 --> 512
511 --> 512
512 --> 512
513 --> 1024
My first stab at it was this:
But this produced a call to a division function (it does not know that the clustersize is always a power of 2)
Then I tried:
if (AFileSize & (MF_sigstr.clustersize-1)) AFileSize+=MF_sigstr.clustersize; AFileSize&=~(MF_sigstr.clustersize-1);
Which avoids the division successfully.
I wonder how others do this? Is there a better way?