/* * Tested with: * gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) * * Without the mem_align() macro, the "if ((size_t)pt & 0xff)" is optimized * away by the compiler (even using -O0) and unaligned pointers are seen * but not printed as such. * * The scope of this test program was to see if even the mem_align() macro * may be optimized away, but that does not seem to be the case. */ #include #include #ifdef _OPENMP #include #endif #define mem_align(a,b) (void*)(((char*)(a))+(((b)-1)-(((size_t)((char*)(a))-1)&((b)-1)))) int main(int argc, char **argv) { int i; #ifdef _OPENMP #pragma omp parallel for #endif for (i = 0; i < argc; i++) { #if 1 __attribute__ ((aligned(256))) char pt[1024]; #else __attribute__ ((aligned(256))) char _pt[1024]; char *pt = mem_align(_pt, 256); #endif sprintf(pt, "%p ", pt); strcat(pt, argv[i]); if ((size_t)pt & 0xff) printf("%s unaligned!\n", pt); else puts(pt); } return 0; }