Fix non-SIMD alignment if void* bigger than double

When building without the SIMD extensions, memory allocations are
currently aligned to sizeof(double).  However, this may be insufficient
on architectures such as Arm Morello or 64-bit CHERI-RISC-V where
pointers require 16-byte rather than 8-byte alignment.  This patch
causes memory allocations to be aligned to
MAX(sizeof(void *), sizeof(double)) when building without the SIMD
extensions.

(NOTE: With C11 we could instead use alignof(max_align_t), but C89
compatibility is still necessary in libjpeg-turbo.)

Closes #587
This commit is contained in:
Alex Richardson
2022-03-28 22:30:54 +00:00
committed by DRC
parent 67cb059046
commit dfc63d42ee

View File

@@ -68,10 +68,13 @@ round_up_pow2(size_t a, size_t b)
* There isn't any really portable way to determine the worst-case alignment * There isn't any really portable way to determine the worst-case alignment
* requirement. This module assumes that the alignment requirement is * requirement. This module assumes that the alignment requirement is
* multiples of ALIGN_SIZE. * multiples of ALIGN_SIZE.
* By default, we define ALIGN_SIZE as sizeof(double). This is necessary on * By default, we define ALIGN_SIZE as the maximum of sizeof(double) and
* some workstations (where doubles really do need 8-byte alignment) and will * sizeof(void *). This is necessary on some workstations (where doubles
* work fine on nearly everything. If your machine has lesser alignment needs, * really do need 8-byte alignment) and will work fine on nearly everything.
* you can save a few bytes by making ALIGN_SIZE smaller. * We use the maximum of sizeof(double) and sizeof(void *) since sizeof(double)
* may be insufficient, for example, on CHERI-enabled platforms with 16-byte
* pointers and a 16-byte alignment requirement. If your machine has lesser
* alignment needs, you can save a few bytes by making ALIGN_SIZE smaller.
* The only place I know of where this will NOT work is certain Macintosh * The only place I know of where this will NOT work is certain Macintosh
* 680x0 compilers that define double as a 10-byte IEEE extended float. * 680x0 compilers that define double as a 10-byte IEEE extended float.
* Doing 10-byte alignment is counterproductive because longwords won't be * Doing 10-byte alignment is counterproductive because longwords won't be
@@ -81,7 +84,7 @@ round_up_pow2(size_t a, size_t b)
#ifndef ALIGN_SIZE /* so can override from jconfig.h */ #ifndef ALIGN_SIZE /* so can override from jconfig.h */
#ifndef WITH_SIMD #ifndef WITH_SIMD
#define ALIGN_SIZE sizeof(double) #define ALIGN_SIZE MAX(sizeof(void *), sizeof(double))
#else #else
#define ALIGN_SIZE 32 /* Most of the SIMD instructions we support require #define ALIGN_SIZE 32 /* Most of the SIMD instructions we support require
16-byte (128-bit) alignment, but AVX2 requires 16-byte (128-bit) alignment, but AVX2 requires