Skip to content
Snippets Groups Projects
Commit 393100e0 authored by Avi Kivity's avatar Avi Kivity
Browse files

Add utility for aligning stuff up and down

parent e0869e06
No related branches found
No related tags found
No related merge requests found
align.hh 0 → 100644
#ifndef ALIGN_HH_
#define ALIGN_HH_
#include "types.hh"
template <typename T>
T align_down(T n, T alignment)
{
return n & ~(alignment - 1);
}
template <typename T>
T align_up(T n, T alignment)
{
return align_down(n + alignment - 1, alignment);
}
template <class T>
T* align_down(T* ptr, size_t alignment)
{
auto n = reinterpret_cast<uintptr_t>(ptr);
n = align_down(n, alignment);
return reinterpret_cast<T*>(n);
}
template <class T>
T* align_up(T* ptr, size_t alignment)
{
auto n = reinterpret_cast<uintptr_t>(ptr);
n = align_up(n, alignment);
return reinterpret_cast<T*>(n);
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment