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

Re: Finding the correct alignment for all architectures


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
> // Alignment to memory pointer
> // (Ultra)SPARC with gcc requires ptr alignment of 8 bytes
> // even though sizeof(void *) is only four: for greatest flexibility
> // allow the build to specify ptr alignment.
> # define CMS_PTR_ALIGNMENT sizeof(void *)
> #endif
> #define _cmsALIGNMEM(x)  (((x)+(CMS_PTR_ALIGNMENT - 1)) & ~(CMS_PTR_ALIGNMENT - 1))
> 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)

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.)


Reply to: