how to determine if an address is cache aligned?

c++, c++11

Solution

If you have a C++11-compliant compiler, then its documentation tells you.

As already mentioned by Igor in the comments, the rules for `reinterpret_cast` include:

A pointer can be explicitly converted to any integral type large enough to hold it. The mapping function is implementation-defined.

That term doesn't just mean "non-portable", it adds specific requirements, found in 1.3.10:

implementation-defied behavior

behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents

If your compiler does not document whether a pointer converted to integer is actually a memory address, then it is not a C++ compiler.

Problem

Is there a C++11 standards compliant (or if not compliant, at least generally acceptable) way to determine if an address is aligned with a cache line boundary? E.g. something like this: ``` T* p = SOMETHING; bool aligned = reinterpret_cast< std::uintptr_t > (p) % CACHE_LINE_SIZE == 0; ```

Original source

Related problems