Alignment Library Functions

From Software Engineers Wiki
Jump to: navigation, search

Write align_test(addr, align), align_floor(addr, align) and align_ceil(addr, align). align is 2^n

  • align_test(addr, align) : tests that addr is on align boundary.
  • align_floor(addr, align) : finds address that is on align boundary and lower or equal to addr with minimum differences.
  • align_ceil(addr, align) : finds address that is on align boundary and higher or equal to addr with minimum differences.

Answer

int align_test(unsigned int addr, unsigned int align)
{
        return (addr & (align - 1)) == 0);
}

unsigned int align_floor(unsigned int addr, unsigned int align)
{
        return (addr & (align - 1));
}

unsigned int align_ceil(unsigned int addr, unsigned int align)
{
        return ((addr + align - 1) & (align - 1));
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox