From f6ed016fe91141ccc346d53afe55fe0ebe06e11c Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Mon, 7 Nov 2022 11:34:26 +0800 Subject: [PATCH] util: Replace the usage of ALIGN16 with alignas(16) and them remove ALIGN16 macro Signed-off-by: Yonggang Luo Reviewed-by: Erik Faye-Lund Part-of: --- src/freedreno/vulkan/tu_device.h | 2 +- src/gallium/auxiliary/tgsi/tgsi_exec.c | 13 +++++++++++-- src/gallium/auxiliary/tgsi/tgsi_exec.h | 10 ++++++---- src/mesa/math/m_matrix.h | 4 ++-- src/util/macros.h | 24 ++++++++++++++++++------ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/freedreno/vulkan/tu_device.h b/src/freedreno/vulkan/tu_device.h index 1b6399175c8..4f6b2a3c494 100644 --- a/src/freedreno/vulkan/tu_device.h +++ b/src/freedreno/vulkan/tu_device.h @@ -184,7 +184,7 @@ struct tu6_global uint32_t pad[7]; } flush_base[4]; - ALIGN16 uint32_t cs_indirect_xyz[3]; + alignas(16) uint32_t cs_indirect_xyz[3]; volatile uint32_t vtx_stats_query_not_running; diff --git a/src/gallium/auxiliary/tgsi/tgsi_exec.c b/src/gallium/auxiliary/tgsi/tgsi_exec.c index 5e440353e36..8486668ca6b 100644 --- a/src/gallium/auxiliary/tgsi/tgsi_exec.c +++ b/src/gallium/auxiliary/tgsi/tgsi_exec.c @@ -73,18 +73,27 @@ #define TILE_BOTTOM_LEFT 2 #define TILE_BOTTOM_RIGHT 3 +static_assert(alignof(union tgsi_exec_channel) == 16, ""); +static_assert(alignof(struct tgsi_exec_vector) == 16, ""); +static_assert(alignof(struct tgsi_exec_machine) == 16, ""); + union tgsi_double_channel { + alignas(16) double d[TGSI_QUAD_SIZE]; unsigned u[TGSI_QUAD_SIZE][2]; uint64_t u64[TGSI_QUAD_SIZE]; int64_t i64[TGSI_QUAD_SIZE]; -} ALIGN16; +}; -struct ALIGN16 tgsi_double_vector { +struct tgsi_double_vector { + alignas(16) union tgsi_double_channel xy; union tgsi_double_channel zw; }; +static_assert(alignof(union tgsi_double_channel) == 16, ""); +static_assert(alignof(struct tgsi_double_vector) == 16, ""); + static void micro_abs(union tgsi_exec_channel *dst, const union tgsi_exec_channel *src) diff --git a/src/gallium/auxiliary/tgsi/tgsi_exec.h b/src/gallium/auxiliary/tgsi/tgsi_exec.h index 80acf635946..9604d7390bd 100644 --- a/src/gallium/auxiliary/tgsi/tgsi_exec.h +++ b/src/gallium/auxiliary/tgsi/tgsi_exec.h @@ -73,17 +73,18 @@ extern "C" { */ union tgsi_exec_channel { + alignas(16) float f[TGSI_QUAD_SIZE]; int i[TGSI_QUAD_SIZE]; unsigned u[TGSI_QUAD_SIZE]; -} ALIGN16; +}; /** * A vector[RGBA] of channels[4 pixels] */ -struct ALIGN16 tgsi_exec_vector +struct tgsi_exec_vector { - union tgsi_exec_channel xyzw[TGSI_NUM_CHANNELS]; + alignas(16) union tgsi_exec_channel xyzw[TGSI_NUM_CHANNELS]; }; /** @@ -286,10 +287,11 @@ typedef void (* apply_sample_offset_func)( /** * Run-time virtual machine state for executing TGSI shader. */ -struct ALIGN16 tgsi_exec_machine +struct tgsi_exec_machine { /* Total = program temporaries + internal temporaries */ + alignas(16) struct tgsi_exec_vector Temps[TGSI_EXEC_NUM_TEMPS]; unsigned ImmsReserved; diff --git a/src/mesa/math/m_matrix.h b/src/mesa/math/m_matrix.h index 905357374ca..1c832eee532 100644 --- a/src/mesa/math/m_matrix.h +++ b/src/mesa/math/m_matrix.h @@ -79,8 +79,8 @@ enum GLmatrixtype { * Matrix type to represent 4x4 transformation matrices. */ typedef struct { - ALIGN16 GLfloat m[16]; /**< 16 matrix elements (16-byte aligned) */ - ALIGN16 GLfloat inv[16]; /**< 16-element inverse (16-byte aligned) */ + alignas(16) GLfloat m[16]; /**< 16 matrix elements (16-byte aligned) */ + alignas(16) GLfloat inv[16]; /**< 16-element inverse (16-byte aligned) */ GLuint flags; /**< possible values determined by (of \link * MatFlags MAT_FLAG_* flags\endlink) */ diff --git a/src/util/macros.h b/src/util/macros.h index 4175a795a8f..267fab4952b 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -215,12 +215,6 @@ do { \ # endif #endif -#ifdef _MSC_VER -#define ALIGN16 __declspec(align(16)) -#else -#define ALIGN16 __attribute__((aligned(16))) -#endif - #ifdef __cplusplus /** * Macro function that evaluates to true if T is a trivially @@ -413,6 +407,24 @@ u_uintN_max(unsigned bit_size) return UINT64_MAX >> (64 - bit_size); } +/* alignas usage + * For struct or union, use alignas(align_size) on any member + * of it will make it aligned to align_size. + * See https://en.cppreference.com/w/c/language/_Alignas for + * details. We can use static_assert and alignof to check if + * the alignment result of alignas(align_size) on struct or + * union is valid. + * For example: + * static_assert(alignof(struct tgsi_exec_machine) == 16, "") + * Also, we can use special code to see the size of the aligned + * struct or union at the compile time with GCC, Clang or MSVC. + * So we can see if the size of union or struct are as expected + * when using alignas(align_size) on its member. + * For example: + * char (*__kaboom)[sizeof(struct tgsi_exec_machine)] = 1; + * can show us the size of struct tgsi_exec_machine at compile + * time. + */ #ifndef __cplusplus #ifdef _MSC_VER #define alignof _Alignof