From ed501b7734ed402f0d296160146af509fe7825f8 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 18 Jan 2022 18:41:39 +0900 Subject: [PATCH] [gamecode] Specify the alignment for progs types And provide a table for such for qfcc and the like. With this, using pr_double_t (for example) in C will cause the double value to always be 8-byte aligned and thus structures shared between gcc and qfcc will be consistent (with a little fuss to take care of the warts). --- include/QF/progs/pr_comp.h | 31 ++++++++++++++++--------------- libs/gamecode/pr_opcode.c | 6 ++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/QF/progs/pr_comp.h b/include/QF/progs/pr_comp.h index 7f52cb21d..5d66fc323 100644 --- a/include/QF/progs/pr_comp.h +++ b/include/QF/progs/pr_comp.h @@ -23,21 +23,21 @@ #include "QF/qtypes.h" -typedef int32_t pr_string_t; -typedef float pr_float_t; -typedef float pr_vector_t[3]; -typedef uint32_t pr_entity_t; -typedef uint32_t pr_field_t; -typedef uint32_t pr_func_t; -typedef uint32_t pr_ptr_t; -typedef float pr_quaternion_t[4]; -typedef int32_t pr_int_t; -typedef uint32_t pr_uint_t; -typedef int16_t pr_short_t; -typedef double pr_double_t; -typedef int64_t pr_long_t; -typedef uint64_t pr_ulong_t; -typedef uint16_t pr_ushort_t; +typedef int32_t pr_string_t __attribute__((aligned(4))); +typedef float pr_float_t __attribute__((aligned(4))); +typedef float pr_vector_t[3] __attribute__((aligned(4))); +typedef uint32_t pr_entity_t __attribute__((aligned(4))); +typedef uint32_t pr_field_t __attribute__((aligned(4))); +typedef uint32_t pr_func_t __attribute__((aligned(4))); +typedef uint32_t pr_ptr_t __attribute__((aligned(4))); +typedef float pr_quaternion_t[4] __attribute__((aligned(4))); +typedef int32_t pr_int_t __attribute__((aligned(4))); +typedef uint32_t pr_uint_t __attribute__((aligned(4))); +typedef int16_t pr_short_t __attribute__((aligned(2))); +typedef double pr_double_t __attribute__((aligned(8))); +typedef int64_t pr_long_t __attribute__((aligned(8))); +typedef uint64_t pr_ulong_t __attribute__((aligned(8))); +typedef uint16_t pr_ushort_t __attribute__((aligned(2)));; #define PR_VEC_TYPE(t,n,s) \ typedef t n __attribute__ ((vector_size (s*sizeof (t)))) @@ -63,6 +63,7 @@ typedef enum { } etype_t; extern const pr_ushort_t pr_type_size[ev_type_count]; +extern const pr_ushort_t pr_type_alignment[ev_type_count]; extern const char * const pr_type_name[ev_type_count]; #define OFS_NULL 0 diff --git a/libs/gamecode/pr_opcode.c b/libs/gamecode/pr_opcode.c index 8fb79e851..c056297a9 100644 --- a/libs/gamecode/pr_opcode.c +++ b/libs/gamecode/pr_opcode.c @@ -41,6 +41,12 @@ VISIBLE const pr_ushort_t pr_type_size[ev_type_count] = { 0, // ev_invalid not a valid/simple type }; +#define EV_TYPE(type) (__alignof__ (pr_##type##_t) / __alignof__ (pr_int_t)), +VISIBLE const pr_ushort_t pr_type_alignment[ev_type_count] = { +#include "QF/progs/pr_type_names.h" + 0, // ev_invalid not a valid/simple type +}; + #define EV_TYPE(type) #type, VISIBLE const char * const pr_type_name[ev_type_count] = { #include "QF/progs/pr_type_names.h"