changeset 24:e49e70f7012f

impl/x86: add static assertions for alignment and size
author Paper <paper@tflc.us>
date Sun, 24 Nov 2024 03:32:53 -0500
parents e26874655738
children 92156fe32755
files include/vec/vec.h src/impl/fallback.c src/impl/x86/avx2.c src/impl/x86/avx512f.c src/impl/x86/mmx.c src/impl/x86/sse2.c src/impl/x86/sse41.c
diffstat 7 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/include/vec/vec.h	Sun Nov 24 02:52:40 2024 -0500
+++ b/include/vec/vec.h	Sun Nov 24 03:32:53 2024 -0500
@@ -152,8 +152,9 @@
 #define VUINT64x8_ALIGNMENT  64
 
 //////////////////////////////////////////////////////////////////////////////
-// bit shift
+// portable bit shift
 
+// these functions aren't very necessary :/
 inline vec_uintmax vec_lrshift(vec_uintmax x, unsigned int y)
 {
 	return x >> y;
@@ -240,16 +241,16 @@
 // array alignment
 
 #if (__cplusplus >= 201103L) || (__STDC_VERSION__ >= 202311L)
-# define VEC_ALIGNOF(type) alignof(x)
+# define VEC_ALIGNOF(type) alignof(type)
 #elif (__STDC_VERSION__ >= 201112L)
-# define VEC_ALIGNOF(type) _Alignof(x)
+# define VEC_ALIGNOF(type) _Alignof(type)
 #elif defined(HAVE_STDDEF_H) // already included
 # define VEC_ALIGNOF(type) \
-	(offsetof(struct { char slot1; x slot2; }, slot2))
+	(offsetof(struct { char slot1; type slot2; }, slot2))
 #else
 // inline offsetof
 # define VEC_ALIGNOF(type) \
-	((vec_uintsize)((char *)&((struct { char slot1; x slot2; } *)0)->slot2 - (char *)0))
+	((vec_uintsize)((char *)&((struct { char slot1; type slot2; } *)0)->slot2 - (char *)0))
 #endif
 
 #if (__cplusplus >= 201103L) || (__STDC_VERSION__ >= 202311L)
@@ -266,7 +267,7 @@
 
 // this wart is here because originally vec didn't require that
 // there be compiler support for alignment. now that we *do*,
-// we need to 
+// we should at least keep providing this macro...
 #ifdef VEC_ALIGNAS
 # define VEC_ALIGNED_ARRAY(type, var, length, align) \
 	VEC_ALIGNAS(align) type var[length]
--- a/src/impl/fallback.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/fallback.c	Sun Nov 24 03:32:53 2024 -0500
@@ -7,10 +7,8 @@
 // memory is unknown or yields incorrect results from the generic functions.
 // This is *extremely* unlikely; for x86 the layout is exactly the same in
 // memory as the generic functions (i.e. it is literally stored as an array of
-// integers).
-//
-// These functions can probably be removed if every current implementation is
-// found to have the same 
+// integers). This is likely true for AltiVec and NEON as well, but those
+// aren't tested for now.
 
 #define VEC_FALLBACK_OPERATION(op, sign, csign, bits, size) \
 	do { \
--- a/src/impl/x86/avx2.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/x86/avx2.c	Sun Nov 24 03:32:53 2024 -0500
@@ -110,6 +110,9 @@
 		__m256i avx2; \
 	}; \
 	\
+	VEC_STATIC_ASSERT(VEC_ALIGNOF(__m256i) <= VEC_ALIGNOF(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " alignment needs to be expanded to fit intrinsic type size"); \
+	VEC_STATIC_ASSERT(sizeof(__m256i) <= sizeof(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " needs to be expanded to fit intrinsic type size"); \
+	\
 	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_avx2_load_aligned(const vec_##sign##int##bits in[size]) \
 	{ \
 		union v##sign##int##bits##x##size##_impl_data vec; \
--- a/src/impl/x86/avx512f.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/x86/avx512f.c	Sun Nov 24 03:32:53 2024 -0500
@@ -226,6 +226,9 @@
 		__m512i avx512f; \
 	}; \
 	\
+	VEC_STATIC_ASSERT(VEC_ALIGNOF(__m512i) <= VEC_ALIGNOF(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " alignment needs to be expanded to fit intrinsic type size"); \
+	VEC_STATIC_ASSERT(sizeof(__m512i) <= sizeof(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " needs to be expanded to fit intrinsic type size"); \
+	\
 	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_avx512f_load_aligned(const vec_##sign##int##bits in[size]) \
 	{ \
 		union v##sign##int##bits##x##size##_impl_data vec; \
--- a/src/impl/x86/mmx.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/x86/mmx.c	Sun Nov 24 03:32:53 2024 -0500
@@ -71,6 +71,9 @@
 		__m64 mmx; \
 	}; \
 	\
+	VEC_STATIC_ASSERT(VEC_ALIGNOF(__m64) <= VEC_ALIGNOF(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " alignment needs to be expanded to fit intrinsic type size"); \
+	VEC_STATIC_ASSERT(sizeof(__m64) <= sizeof(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " needs to be expanded to fit intrinsic type size"); \
+	\
 	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_mmx_load_aligned(const vec_##sign##int##bits in[size]) \
 	{ \
 		v##sign##int##bits##x##size vec; \
--- a/src/impl/x86/sse2.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/x86/sse2.c	Sun Nov 24 03:32:53 2024 -0500
@@ -148,6 +148,9 @@
 		__m128i sse; \
 	}; \
 	\
+	VEC_STATIC_ASSERT(VEC_ALIGNOF(__m128i) <= VEC_ALIGNOF(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " alignment needs to be expanded to fit intrinsic type size"); \
+	VEC_STATIC_ASSERT(sizeof(__m128i) <= sizeof(v##sign##int##bits##x##size), "vec: v" #sign "int" #bits "x" #size " needs to be expanded to fit intrinsic type size"); \
+	\
 	v##sign##int##bits##x##size v##sign##int##bits##x##size##_sse2_load_aligned(const vec_##sign##int##bits in[size]) \
 	{ \
 		union v##sign##int##bits##x##size##_impl_data vec; \
--- a/src/impl/x86/sse41.c	Sun Nov 24 02:52:40 2024 -0500
+++ b/src/impl/x86/sse41.c	Sun Nov 24 03:32:53 2024 -0500
@@ -35,6 +35,9 @@
 		__m128i sse; \
 	}; \
 	\
+	VEC_STATIC_ASSERT(VEC_ALIGNOF(__m128i) <= VEC_ALIGNOF(v##sign##int32x4), "vec: v" #sign "int32x4 alignment needs to be expanded to fit intrinsic type size"); \
+	VEC_STATIC_ASSERT(sizeof(__m128i) <= sizeof(v##sign##int32x4), "vec: v" #sign "int32x4 needs to be expanded to fit intrinsic type size"); \
+	\
 	static v##sign##int32x4 v##sign##int32x4_sse41_mul(v##sign##int32x4 vec1, v##sign##int32x4 vec2) \
 	{ \
 		union v##sign##int32x4_impl_data *vec1d = (union v##sign##int32x4_impl_data *)&vec1; \