0 is not a power of 2

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Tue May 19 13:41:11 PDT 2015


On 05/19/2015 09:56 PM, Andrei Alexandrescu wrote:
> On 5/19/15 11:05 AM, Marco Leise wrote:
>> While you are at it, you might also need "round pointer up to"
>> and "round pointer down to", which can be implemented with bit
>> ops for power-of-2 multiples.
>
> Yah, there are plenty of those in
> https://github.com/andralex/phobos/blob/allocator/std/experimental/allocator/common.d.
> Improvements welcome. -- Andrei

In case the range of s is such that divideRoundUp is actually good 
enough, the following avoids the conditional:

size_t roundUpToMultipleOf(size_t s,uint base){
     auto t=s+base-1;
     return t-t%base;
}

However, both divideRoundUp and this implementation of 
roundUpToMultipleOf do not work for s in [size_t.max-base+2, size_t.max].


More information about the Digitalmars-d mailing list