about summary refs log tree commit diff homepage
path: root/test/VectorInstructions/integer_ops_constant.c
blob: 189ad4eeed540cc178c9f6e834559e373fc7031c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// REQUIRES: geq-llvm-3.4
// RUN: %llvmgcc %s -emit-llvm -O0 -g -c -o %t1.bc
// RUN: rm -rf %t.klee-out
// NOTE: Have to pass `--optimize=false` to avoid vector operations being
// constant folded away.
// RUN: %klee --output-dir=%t.klee-out --optimize=false --exit-on-error %t1.bc
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

typedef uint32_t v4ui __attribute__((vector_size(16)));
typedef int32_t v4si __attribute__((vector_size(16)));

#define ASSERT_EL(C, OP, A, B, INDEX) assert(C[INDEX] == (A[INDEX] OP B[INDEX]))
#define ASSERT_ELV4(C, OP, A, B)                                               \
  do {                                                                         \
    ASSERT_EL(C, OP, A, B, 0);                                                 \
    ASSERT_EL(C, OP, A, B, 1);                                                 \
    ASSERT_EL(C, OP, A, B, 2);                                                 \
    ASSERT_EL(C, OP, A, B, 3);                                                 \
  } while (0);

#define ASSERT_EL_TRUTH(C, OP, A, B, INDEX)                                    \
  assert(C[INDEX] ? (A[INDEX] OP B[INDEX]) : (!(A[INDEX] OP B[INDEX])))
#define ASSERT_EL_TRUTH_V4(C, OP, A, B)                                        \
  do {                                                                         \
    ASSERT_EL_TRUTH(C, OP, A, B, 0);                                           \
    ASSERT_EL_TRUTH(C, OP, A, B, 1);                                           \
    ASSERT_EL_TRUTH(C, OP, A, B, 2);                                           \
    ASSERT_EL_TRUTH(C, OP, A, B, 3);                                           \
  } while (0);

#define ASSERT_EL_TERNARY_SCALAR_CONDITION(C, CONDITION, A, B, INDEX)          \
  assert(C[INDEX] == (CONDITION ? A[INDEX] : B[INDEX]))
#define ASSERT_EL_TERNARY_SCALAR_CONDITION_V4(C, CONDITION, A, B)              \
  do {                                                                         \
    ASSERT_EL_TERNARY_SCALAR_CONDITION(C, CONDITION, A, B, 0);                 \
    ASSERT_EL_TERNARY_SCALAR_CONDITION(C, CONDITION, A, B, 1);                 \
    ASSERT_EL_TERNARY_SCALAR_CONDITION(C, CONDITION, A, B, 2);                 \
    ASSERT_EL_TERNARY_SCALAR_CONDITION(C, CONDITION, A, B, 3);                 \
  } while (0);

int main() {
  // Unsigned tests
  {
    v4ui a = {0, 1, 2, 3};
    v4ui b = {10, 20, 30, 3};

    // Test addition
    v4ui c = a + b;
    ASSERT_ELV4(c, +, a, b);

    // Test subtraction
    c = b - a;
    ASSERT_ELV4(c, -, b, a);

    // Test multiplication
    c = a * b;
    ASSERT_ELV4(c, *, a, b);

    // Test division
    c = a / b;
    ASSERT_ELV4(c, /, a, b);

    // Test mod
    c = a % b;
    ASSERT_ELV4(c, %, a, b);

    // Test bitwise and
    c = a & b;
    ASSERT_ELV4(c, &, a, b);

    // Test bitwise or
    c = a | b;
    ASSERT_ELV4(c, |, a, b);

    // Test bitwise xor
    c = a ^ b;
    ASSERT_ELV4(c, ^, a, b);

    // Test left shift
    c = b << a;
    ASSERT_ELV4(c, <<, b, a);

    // Test logic right shift
    c = b >> a;
    ASSERT_ELV4(c, >>, b, a);

    // NOTE: Can't use `ASSERT_ELV4` due to semantics
    // of GCC vector extensions. See
    // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html

    // Test ==
    c = a == b;
    ASSERT_EL_TRUTH_V4(c, ==, a, b);

    // Test <
    c = a < b;
    ASSERT_EL_TRUTH_V4(c, <, a, b);

    // Test <=
    c = a <= b;
    ASSERT_EL_TRUTH_V4(c, <=, a, b);

    // Test >
    c = a > b;
    ASSERT_EL_TRUTH_V4(c, >, a, b);

    // Test >=
    c = a > b;
    ASSERT_EL_TRUTH_V4(c, >, a, b);

    // Test !=
    c = a != b;
    ASSERT_EL_TRUTH_V4(c, !=, a, b);

    // Test ternary operator
    c = 0 ? a : b;
    ASSERT_EL_TERNARY_SCALAR_CONDITION_V4(c, 0, a, b);
    c = 1 ? a : b;
    ASSERT_EL_TERNARY_SCALAR_CONDITION_V4(c, 1, a, b);
  }

  // Signed tests
  {
    v4si a = {-1, 2, -3, 4};
    v4si b = {-10, 20, -30, 40};

    // Test addition
    v4si c = a + b;
    ASSERT_ELV4(c, +, a, b);

    // Test subtraction
    c = b - a;
    ASSERT_ELV4(c, -, b, a);

    // Test multiplication
    c = a * b;
    ASSERT_ELV4(c, *, a, b);

    // Test division
    c = a / b;
    ASSERT_ELV4(c, /, a, b);

    // Test mod
    c = a % b;
    ASSERT_ELV4(c, %, a, b);

    // Test bitwise and
    c = a & b;
    ASSERT_ELV4(c, &, a, b);

    // Test bitwise or
    c = a | b;
    ASSERT_ELV4(c, |, a, b);

    // Test bitwise xor
    c = a ^ b;
    ASSERT_ELV4(c, ^, a, b);

    // Test left shift
    v4si shifts = {1, 2, 3, 4};
    c = b << shifts;
    ASSERT_ELV4(c, <<, b, shifts);

    // Test arithmetic right shift
    c = b >> shifts;
    ASSERT_ELV4(c, >>, b, shifts);

    // Test ==
    c = a == b;
    ASSERT_EL_TRUTH_V4(c, ==, a, b);

    // Test <
    c = a < b;
    ASSERT_EL_TRUTH_V4(c, <, a, b);

    // Test <=
    c = a <= b;
    ASSERT_EL_TRUTH_V4(c, <=, a, b);

    // Test >
    c = a > b;
    ASSERT_EL_TRUTH_V4(c, >, a, b);

    // Test >=
    c = a > b;
    ASSERT_EL_TRUTH_V4(c, >, a, b);

    // Test !=
    c = a != b;
    ASSERT_EL_TRUTH_V4(c, !=, a, b);

    // Test ternary operator
    c = 0 ? a : b;
    ASSERT_EL_TERNARY_SCALAR_CONDITION_V4(c, 0, a, b);
    c = 1 ? a : b;
    ASSERT_EL_TERNARY_SCALAR_CONDITION_V4(c, 1, a, b);
  }
  return 0;
}