Skip to content

Commit

Permalink
Split alignment tests (#573)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bretambrose authored Jan 14, 2020
1 parent f42eca7 commit d907784
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
8 changes: 7 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 24 additions & 9 deletions tests/byte_order_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d907784

Please sign in to comment.