Alignment v0.1.0

Source
Testing addresses for alignment and rounding them to a boundary.

IsAligned

pub func IsAligned(address: uint, alignment: uint) -> bool

Whether address is a multiple of alignment. address and alignment are read by value. An alignment of zero reports false rather than dividing by it, and an alignment that is not a power of two gives an answer that is arithmetically true but rarely meaningful, since hardware alignment is always a power of two. Cannot fail. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
addressuintthe address or offset to test
alignmentuintthe boundary to test against; zero reports false

Returns: whether address is a multiple of alignment

IsPointerAligned

pub func IsPointerAligned(
    pointer: *opaque,
    alignment: uint
) -> bool

Whether pointer is aligned for alignment. pointer is read by value and never dereferenced. A null pointer reports true for every alignment, since zero is a multiple of everything; a caller that means "usable" has to ask that separately. Cannot fail. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
pointer*opaquethe address to test, never dereferenced
alignmentuintthe boundary to test against

Returns: whether pointer is a multiple of alignment

AlignUp

pub func AlignUp(value: uint, alignment: uint) -> uint

The next multiple of alignment at or above value. value and alignment are read by value. An alignment of zero or one returns value unchanged, since every value is already a multiple of both. Returns value itself when it is already aligned, so this is idempotent. A value within alignment - 1 of the maximum has no next multiple, and this wraps to a small number rather than reporting it. A caller sizing an allocation must check for that before trusting the result; AlignUpChecked does the checking.

Source

Parameters

NameTypeDescription
valueuintthe value to round up
alignmentuintthe boundary to round to; zero or one changes nothing

Returns: the next multiple of alignment at or above value

AlignUpChecked

pub func AlignUpChecked(
    value: uint,
    alignment: uint,
    result: *var uint
) -> bool

The next multiple of alignment at or above value, reporting whether it was representable. value and alignment are read by value; result receives the aligned value when this returns false, and is left unwritten when it returns true. result must not be null. Returns true when rounding up would pass the maximum, which is the case an allocation size has to notice: a wrapped size is an allocation far smaller than the caller asked for. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
valueuintthe value to round up
alignmentuintthe boundary to round to; zero or one changes nothing
result*var uintreceives the aligned value when this returns false

Returns: true when rounding up would pass the maximum

AlignDown

pub func AlignDown(value: uint, alignment: uint) -> uint

The previous multiple of alignment at or below value. value and alignment are read by value. An alignment of zero or one returns value unchanged. Cannot fail: rounding down never leaves the range.

Source

Parameters

NameTypeDescription
valueuintthe value to round down
alignmentuintthe boundary to round to; zero or one changes nothing

Returns: the previous multiple of alignment at or below value