From d907784e229e7a2a2cca0e306048935cfa385795 Mon Sep 17 00:00:00 2001 From: Bret Ambrose Date: Tue, 14 Jan 2020 12:10:01 -0800 Subject: [PATCH] Split alignment tests (#573) * Split alignment tests into 16 and 32 bytes; 32 enabled only if avx2 support is active * Factor out stack alignment of structure variable * Use offsetof --- tests/CMakeLists.txt | 8 +++++++- tests/byte_order_test.c | 33 ++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e11d102d2..0f3775fb8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -241,7 +241,13 @@ add_test_case(test_byte_cursor_compare_lexical) add_test_case(test_byte_cursor_compare_lookup) add_test_case(byte_swap_test) -add_test_case(alignment_test) + +if (HAVE_AVX2_INTRINSICS AND HAVE_SIMD_CPUID) + add_test_case(alignment32_test) +else() + add_test_case(alignment16_test) +endif() + add_test_case(test_cpu_count_at_least_works_superficially) add_test_case(test_stack_trace_decoding) diff --git a/tests/byte_order_test.c b/tests/byte_order_test.c index 8b6cef5e6..21bad3dff 100644 --- a/tests/byte_order_test.c +++ b/tests/byte_order_test.c @@ -66,23 +66,38 @@ static int s_byte_swap_test_fn(struct aws_allocator *allocator, void *ctx) { } AWS_TEST_CASE(byte_swap_test, s_byte_swap_test_fn); -AWS_ALIGNED_TYPEDEF(uint8_t, aligned_storage[64], 32); +AWS_ALIGNED_TYPEDEF(uint8_t, aligned32_storage[64], 32); -struct padding_disaster { - aligned_storage a; +struct padding32_disaster { uint8_t dumb; - aligned_storage b; + aligned32_storage b; }; -static int s_alignment_test_fn(struct aws_allocator *allocator, void *ctx) { +static int s_alignment32_test_fn(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; - struct padding_disaster padded; + size_t spacing = offsetof(struct padding32_disaster, b) - offsetof(struct padding32_disaster, dumb); + ASSERT_UINT_EQUALS(0, spacing % 32); - ASSERT_UINT_EQUALS(0, ((intptr_t)&padded.a) % 32); - ASSERT_UINT_EQUALS(0, ((intptr_t)&padded.b) % 32); + return 0; +} +AWS_TEST_CASE(alignment32_test, s_alignment32_test_fn) + +AWS_ALIGNED_TYPEDEF(uint8_t, aligned16_storage[64], 16); + +struct padding16_disaster { + uint8_t dumb; + aligned16_storage b; +}; + +static int s_alignment16_test_fn(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + + size_t spacing = offsetof(struct padding32_disaster, b) - offsetof(struct padding32_disaster, dumb); + ASSERT_UINT_EQUALS(0, spacing % 16); return 0; } -AWS_TEST_CASE(alignment_test, s_alignment_test_fn) +AWS_TEST_CASE(alignment16_test, s_alignment16_test_fn)