5

I just started with C++ and i think the best way is to look at source codes. I have code as follows in the header file.

#ifdef _MSC_VER
#define MYAPP_CACHE_ALIGNED_RETURN /* not supported */
#else
#define MYAPP_CACHE_ALIGNED_RETURN __attribute__((assume_aligned(64)))
#endif

I am using gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) and its quite old. I get this warning during compilation:

 warning: 'assume_aligned' attribute directiv e ignored [-Wattributes] –

How can I make the if statement more specific to fix the the warning during compilation?

user1066886
  • 202
  • 3
  • 12
user2650277
  • 6,289
  • 17
  • 63
  • 132
  • What warning do you see? – yugr Jan 17 '17 at 11:34
  • @yugr `warning: 'assume_aligned' attribute directiv e ignored [-Wattributes]` – user2650277 Jan 17 '17 at 12:03
  • Interesting [link]http://stackoverflow.com/questions/9608171/how-to-tell-gcc-that-a-pointer-argument-is-always-double-word-aligned reports `__builtin_assume_aligned` already supported 4 years ago. While your GCC is from 2015... it should work. – JHBonarius Feb 10 '17 at 14:00

1 Answers1

3

It seems that assume_aligned is not supported in RHEL's GCC (it hasn't been backported to upstream gcc-4_8-branch and also not available in Ubuntu 14.04's GCC 4.8.4 so that wouldn't be surprising).

To emit a more user-friendly diagnostics you can do an explicit check for GCC version in one of your headers:

#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)
#  warning "Your version of GCC does not support 'assume_aligned' attribute"
#endif

But this may not work if your distro vendor has back-ported assume_aligned from upstream (which is not the case for RedHat and Ubuntu but who knows about other distros). The most robust way to check this would be to do a build-time test in configure script or in Makefile:

CFLAGS += $(shell echo 'void* my_alloc1() __attribute__((assume_aligned(16)));' | gcc -x c - -c -o /dev/null -Werror && echo -DHAS_ASSUME_ALIGNED)

This will add HAS_ASSUME_ALIGNED to predefined macro if the attribute is supported by compiler.

Note that you can achieve similar effect with __builtin_assume_aligned function:

void foo() {
  double *p = func_that_misses_assume_align();
  p = __builtin_assume_aligned(p, 128);
  ...
}
yugr
  • 19,769
  • 3
  • 51
  • 96