[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Finding the correct alignment for all architectures



* Christian Seiler <christian@iwakd.de>, 2016-10-13, 00:20:
On 10/12/2016 11:51 PM, Thomas Weber wrote:
I am maintaining lcms2. In #749975, I received a patch to ensure correct alignment for doubles von MIPS. I have forwarded the patch upstream[1], but in the latest release, upstream has chosen a different way. It is now possible to configure the alignment via a preprocessor variable CMS_PTR_ALIGNMENT[2]:
[...]
I would like to drop the Debian-specific patch. But what value for CMS_PTR_ALIGNMENT would be good/sufficient on all arches?

Use _Alignof(type), that will always be correct. :-)

For example:

#define POINTER_ALIGNMENT    _Alignof(void *)
#define DOUBLE_ALIGNMENT     _Alignof(double)

If you are not delighted with leading underscores, you can #include <stdalign.h> and then do s/_Alignof/alignof/.

Technically, this was introduced in C11/C++11, so if you need to support really old compilers, this may be problematic, but gcc/clang have supported that for a while. (A quick test tells me that gcc and clang from Jessie already support it.)

gcc in wheezy supports it too.

Alternatively, you may want to use the (gcc-specific) __BIGGEST_ALIGNMENT__ macro, but that kinda wasteful. For example, on i386 it's 16, even though alignof(t) is <= 4 for most t you can think of.

--
Jakub Wilk


Reply to: