1/*
2 * Copyright 2017, Data61, CSIRO (ABN 41 687 119 230)
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7/* A couple of operations that should trigger faults on x86 if
8 * our stack isn't set up correctly.
9 */
10
11#pragma once
12
13#include <string.h>
14
15typedef struct foo {
16    float arr[4];
17} foo_t;
18
19/* Hopefully the compiler will attempt to optimize this
20 * function with sse instructions on x86.
21 */
22static inline void memcpy_test(foo_t *a, foo_t *b) {
23    foo_t intermediate = *a;
24    intermediate.arr[1] += 0.5;
25    memcpy(b, &intermediate, sizeof(foo_t));
26}
27
28/* Implemented in assembly */
29void movaps_test(void);
30
31/* Hopefully this function will result in a GP fault due
32 * to unaligned operands of sse instructions with alignment
33 * requirements, unless the stack is correctly aligned.
34 */
35static inline void test_alignment(void) {
36    foo_t a, b;
37    a.arr[0] = 0.1;
38    a.arr[1] = 0.2;
39    a.arr[2] = 0.3;
40    a.arr[3] = 0.4;
41
42    memcpy_test(&a, &b);
43    movaps_test();
44
45    printf("Done testing alignment: %f\n", b.arr[1]);
46}
47