gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
How to align void *
referencing entity with no declared type to a cache line size in a conforming way?
I'm interested to explain the legitimacy of the common alignment technique with Standard.
My attempt
void *ptr = //some valid pointer;
void *aligned_ptr = (void *) ((intptr_t) ptr & -64);
The (intptr_t) ptr & -64
part is conforming since any 7.20.1.4 N2310
:
an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void , and the result will compare equal to the original pointer
Converting (intptr_t) ptr & -64
back to void *
is not specified to have well-defined behavior. 6.3.2.3/5
provides some blurred information (emp. mine):
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
If I correctly understand the concept of the "trap representation" then it is not possible to have it in such case since there is not declared type of the object.
I'm not sure if (void *) ((intptr_t) ptr & -64)
is correctly aligned from the Standard standpoint or the result is not an implementation defined.
UPD: 6.2.8
Describes
Every valid alignment value shall be a nonnegative integral power of two.
Since _Alignof (max_align_t)
is 16
on my implementation then I suppose the 64 byte alignment is not obligated to be supported by gcc.
__attribute__((aligned(64))
or similar compiler directives. Needless to say, this cannot be made in a generic non compiler-specific way. There is no standard-defined way to "conformingly align" something to cache. – Marco Bonellivoid *
may be a trap representation of thevoid *
type. – Eric Postpischil