Unstandard

Additions to std.math.

License
Boost License 1.0.
Authors
Denis Shelomovskij

pure nothrow @safe bool  isPowerOf2(in size_t n);

Returns true iff n is a power of 2.

Preconditions:
n must be non-zero.

pure nothrow @safe size_t  roundDownToPowerOf2(in size_t n);

Returns largest power of 2 which <= n.

Preconditions:
n must be non-zero.

pure nothrow @safe size_t  roundUpToPowerOf2(in size_t n);

Returns smallest power of 2 which >= n.

Preconditions:
n must be non-zero.

pure nothrow @safe ubyte  log2RoundedDown(in size_t n);

Returns the base-2 logarithm of largest power of 2 which <= n.

Preconditions:
n must be non-zero.

pure nothrow @safe ubyte  log2RoundedUp(in size_t n);

Returns the base-2 logarithm of smallest power of 2 which >= n.

Preconditions:
n must be non-zero.

pure nothrow @safe size_t  alignDown(in size_t alignment, in size_t n);
size_t  alignDown(size_t alignment)(in size_t n) if (isPowerOf2(alignment));
pure nothrow @safe size_t  alignUp(in size_t alignment, in size_t n);
size_t  alignUp(size_t alignment)(in size_t n) if (isPowerOf2(alignment));

Aligns n up or down.

Preconditions:
alignment must be power of 2.

pure nothrow @safe bool  isAligned(in size_t alignment, in size_t n);
bool  isAligned(size_t alignment)(in size_t n) if (isPowerOf2(alignment));

Aligns n up or down.

Preconditions:
alignment must be power of 2.