changeset 7:945d410803f8

*: fix clang & gcc warnings, add avg test, etc
author Paper <paper@tflc.us>
date Wed, 23 Oct 2024 20:07:08 -0400
parents 978c167dcceb
children 6e0eb3aa12ab
files include/vec/impl/gcc.h include/vec/impl/sse2.h include/vec/vec.h test/main.c test/test_arith.h test/test_compare.h
diffstat 6 files changed, 37 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/include/vec/impl/gcc.h	Wed Oct 23 19:05:34 2024 -0400
+++ b/include/vec/impl/gcc.h	Wed Oct 23 20:07:08 2024 -0400
@@ -30,8 +30,9 @@
 #define VEC_DEFINE_OPERATIONS(sign, csign, bits, size) \
 	VEC_DECL_LOAD_ALIGNED(sign, bits, size) \
 	{ \
-		/* no real known alignment here */ \
-		return *(v##sign##int##bits##x##size *)in; \
+		v##sign##int##bits##x##size vec; \
+		memcpy(&vec, in, sizeof(*in) * size); \
+		return vec; \
 	} \
 	\
 	VEC_DECL_LOAD(sign, bits, size) \
@@ -41,7 +42,7 @@
 	\
 	VEC_DECL_STORE_ALIGNED(sign, bits, size) \
 	{ \
-		*(v##sign##int##bits##x##size *)out = vec; \
+		memcpy(out, &vec, sizeof(vec)); \
 	} \
 	\
 	VEC_DECL_STORE(sign, bits, size) \
--- a/include/vec/impl/sse2.h	Wed Oct 23 19:05:34 2024 -0400
+++ b/include/vec/impl/sse2.h	Wed Oct 23 20:07:08 2024 -0400
@@ -174,7 +174,7 @@
 #ifndef VEC_VUINT64X2
 # define VEC_VUINT64X2
 typedef __m128i vuint64x2;
-static inline VEC_ALWAYS_INLINE vuint64x2 VUINT64x2_CONSTANT(uint64_t a, uint64_t b)
+VEC_FUNC_KEYWORDS vuint64x2 VUINT64x2_CONSTANT(uint64_t a, uint64_t b)
 {
 	return _mm_setr_epi32(b, b >> 32, a, a >> 32);
 }
@@ -216,11 +216,11 @@
 #ifndef VEC_VINT64X2
 # define VEC_VINT64X2
 typedef __m128i vint64x2;
-static inline VEC_ALWAYS_INLINE vint64x2 VINT64x2_CONSTANT(int64_t a, int64_t b)
+# define VINT64x2_ALIGNMENT VEC_SSE2_ALIGNMENT
+VEC_FUNC_KEYWORDS vint64x2 VINT64x2_CONSTANT(int64_t a, int64_t b)
 {
 	return _mm_setr_epi32(b, vec_rshift(b, 32), a, vec_rshift(a, 32));
 }
-# define VINT64x2_ALIGNMENT VEC_SSE2_ALIGNMENT
 VEC_DEFINE_OPERATIONS(, , 64, 2)
 VEC_GENERIC_COMPARISONS(, , 64, 2)
 #endif
--- a/include/vec/vec.h	Wed Oct 23 19:05:34 2024 -0400
+++ b/include/vec/vec.h	Wed Oct 23 20:07:08 2024 -0400
@@ -63,6 +63,18 @@
 # define VEC_ALWAYS_INLINE
 #endif
 
+/* Allow users to define all of the symbols externally in
+ * one translation unit. */
+#ifdef VEC_EXTERN
+# ifdef VEC_EXTERN_DEFINE
+#  define VEC_FUNC_KEYWORDS extern inline
+# else
+#  define VEC_FUNC_KEYWORDS inline
+# endif
+#else
+# define VEC_FUNC_KEYWORDS static inline VEC_ALWAYS_INLINE
+#endif
+
 #ifdef VEC_ALIGNED
 # define VEC_ALIGNED_ARRAY(type, var, length, align) \
 	VEC_ALIGNED(align) type var[length]
@@ -83,32 +95,32 @@
 /* --------------------------------------------------------------- */
 /* bit shift */
 
-static inline VEC_ALWAYS_INLINE uintmax_t vec_ulrshift(uintmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS uintmax_t vec_ulrshift(uintmax_t x, unsigned int y)
 {
 	return x >> y;
 }
 
-static inline VEC_ALWAYS_INLINE uintmax_t vec_ullshift(uintmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS uintmax_t vec_ullshift(uintmax_t x, unsigned int y)
 {
 	return x << y;
 }
 
-static inline VEC_ALWAYS_INLINE intmax_t vec_lrshift(intmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS intmax_t vec_lrshift(intmax_t x, unsigned int y)
 {
 	return (intmax_t)(((uintmax_t)x) >> y);
 }
 
-static inline VEC_ALWAYS_INLINE intmax_t vec_llshift(intmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS intmax_t vec_llshift(intmax_t x, unsigned int y)
 {
 	return (intmax_t)(((uintmax_t)x) << y);
 }
 
-static inline VEC_ALWAYS_INLINE uintmax_t vec_urshift(uintmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS uintmax_t vec_urshift(uintmax_t x, unsigned int y)
 {
 	return x >> y;
 }
 
-static inline VEC_ALWAYS_INLINE uintmax_t vec_ulshift(uintmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS uintmax_t vec_ulshift(uintmax_t x, unsigned int y)
 {
 	return x << y;
 }
@@ -139,7 +151,7 @@
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
 **/
-static inline VEC_ALWAYS_INLINE intmax_t vec_rshift(intmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS intmax_t vec_rshift(intmax_t x, unsigned int y)
 {
 	static const uintmax_t roffset = UINTMAX_C(1) << ((sizeof(intmax_t) * CHAR_BIT) - 1);
 
@@ -151,7 +163,7 @@
 	return (intmax_t)urx;
 }
 
-static inline VEC_ALWAYS_INLINE intmax_t vec_lshift(intmax_t x, unsigned int y)
+VEC_FUNC_KEYWORDS intmax_t vec_lshift(intmax_t x, unsigned int y)
 {
 	static const uintmax_t roffset = UINTMAX_C(1) << ((sizeof(intmax_t) * CHAR_BIT) - 1);
 
@@ -210,7 +222,7 @@
 /* Implementation includes */
 
 #define VEC_OPERATION_DECL(sign, bits, size, ret, op, params) \
-	static inline VEC_ALWAYS_INLINE ret v##sign##int##bits##x##size##_##op params
+	VEC_FUNC_KEYWORDS ret v##sign##int##bits##x##size##_##op params
 
 #define VEC_OPERATION_THIS_DECL(sign, bits, size, op, params) \
 	VEC_OPERATION_DECL(sign, bits, size, v##sign##int##bits##x##size, op, params)
@@ -300,7 +312,7 @@
 #define VEC_GENERIC_AVG(sign, bits, size) \
 	VEC_DECL_AVG(sign, bits, size) \
 	{ \
-		return v##sign##int##bits##x##size##_div(v##sign##int##bits##x##size##_mul(vec1, vec2), v##sign##int##bits##x##size##_splat(2)); \
+		return v##sign##int##bits##x##size##_div(v##sign##int##bits##x##size##_add(vec1, vec2), v##sign##int##bits##x##size##_splat(2)); \
 	}
 
 #define VEC_GENERIC_THAN_OR_EQUAL(sign, bits, size) \
@@ -324,7 +336,7 @@
 		v##sign##int##bits##x##size##_store_aligned(vec1, vec1a); \
 		v##sign##int##bits##x##size##_store_aligned(vec2, vec2a); \
 	\
-		for (int i = 0; i < size; i++) vec1a[i] = (vec1a[i] op vec2a[i]) ? UINT##bits##_MAX : 0; \
+		for (int i = 0; i < size; i++) vec1a[i] = (vec1a[i] op vec2a[i]) ? (sign##int##bits##_t)(UINT##bits##_MAX) : 0; \
 	\
 		return v##sign##int##bits##x##size##_load_aligned(vec1a); \
 	}
@@ -360,7 +372,7 @@
 #define DEFINE_NOT_OPERATION(sign, bits, size) \
 	VEC_DECL_NOT(sign, bits, size) \
 	{ \
-		return v##sign##int##bits##x##size##_xor(vec, v##sign##int##bits##x##size##_splat(UINT##bits##_MAX)); \
+		return v##sign##int##bits##x##size##_xor(vec, v##sign##int##bits##x##size##_splat((sign##int##bits##_t)UINT##bits##_MAX)); \
 	}
 
 DEFINE_NOT_OPERATION(, 8, 16)
--- a/test/main.c	Wed Oct 23 19:05:34 2024 -0400
+++ b/test/main.c	Wed Oct 23 20:07:08 2024 -0400
@@ -1,3 +1,5 @@
+#define VEC_EXTERN
+#define VEC_EXTERN_DEFINE
 #include "vec/vec.h"
 
 #include <stdio.h>
--- a/test/test_arith.h	Wed Oct 23 19:05:34 2024 -0400
+++ b/test/test_arith.h	Wed Oct 23 20:07:08 2024 -0400
@@ -40,7 +40,7 @@
 	\
 		for (int i = 0; i < size; i++) { \
 			if ((sign##int##bits##_t)(equiv) != orig_c[i]) { \
-				fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%" PRI ## psign ## bits "] does not equal result [%" PRI ## psign ## bits "]!\n", i, equiv, orig_c[i]); \
+				fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%" PRI ## psign ## bits "] does not equal result [%" PRI ## psign ## bits "]!\n", i, (sign##int##bits##_t)(equiv), orig_c[i]); \
 				print_v##sign##int##bits##x##size(stderr,a); \
 				print_vuint##bits##x##size(stderr,b); \
 				print_v##sign##int##bits##x##size(stderr,c); \
@@ -60,7 +60,7 @@
 	CREATE_TEST(sign, psign, csign, bits, size, and, orig_a[i] & orig_b[i]) \
 	CREATE_TEST(sign, psign, csign, bits, size, or,  orig_a[i] | orig_b[i]) \
 	CREATE_TEST(sign, psign, csign, bits, size, xor, orig_a[i] ^ orig_b[i]) \
-	CREATE_TEST(sign, psign, csign, bits, size, avg, (orig_a[i] * orig_b[i]) / 2) \
+	CREATE_TEST(sign, psign, csign, bits, size, avg, (sign##int##bits##_t)(orig_a[i] + orig_b[i]) / 2) \
 	CREATE_TEST_SHIFT(sign, psign, csign, bits, size, rshift, vec_##sign##rshift(orig_a[i], orig_b[i])) \
 	CREATE_TEST_SHIFT(sign, psign, csign, bits, size, lshift, vec_##sign##lshift(orig_a[i], orig_b[i])) \
 	CREATE_TEST_SHIFT(sign, psign, csign, bits, size, lrshift, vec_##sign##lrshift(orig_a[i], orig_b[i]))
@@ -94,6 +94,7 @@
 			ret |= test_arith_v##sign##int##bits##x##size##_and(a, b); \
 			ret |= test_arith_v##sign##int##bits##x##size##_or(a, b); \
 			ret |= test_arith_v##sign##int##bits##x##size##_xor(a, b); \
+			ret |= test_arith_v##sign##int##bits##x##size##_avg(a, b); \
 		} \
 	} \
 	\
--- a/test/test_compare.h	Wed Oct 23 19:05:34 2024 -0400
+++ b/test/test_compare.h	Wed Oct 23 20:07:08 2024 -0400
@@ -11,7 +11,7 @@
 	\
 		for (int i = 0; i < size; i++) { \
 			if ((sign##int##bits##_t)(((equiv) ? UINT##bits##_MAX : 0)) != orig_c[i]) { \
-				fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%" PRI ## psign ## bits "] does not equal result [%" PRI ## psign ## bits "]!\n", i, equiv, orig_c[i]); \
+				fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%d] does not equal result [%" PRI ## psign ## bits "]!\n", i, equiv, orig_c[i]); \
 				print_v##sign##int##bits##x##size(stderr,a); \
 				print_v##sign##int##bits##x##size(stderr,b); \
 				print_v##sign##int##bits##x##size(stderr,c); \