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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#include <stdio.h>
#define TRUE_VAL (!0)
#define FALSE_VAL 0
#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
typedef unsigned long long int uint64_t;
typedef long long int int64_t;
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
int64_t a = 1234567890003LL;
int64_t b = 2345678901235LL;
int64_t c = 1234567890001LL;
int64_t d = 10001LL;
int64_t e = 10000LL;
int64_t f = -1068103409991LL;
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
int
i64_eq(int64_t a, int64_t b)
{
return (a == b);
}
int
i64_neq(int64_t a, int64_t b)
{
return (a != b);
}
int
i64_ugt(uint64_t a, uint64_t b)
{
return (a > b);
}
int
i64_ule(uint64_t a, uint64_t b)
{
return (a <= b);
}
int64_t
i64_eq_select(int64_t a, int64_t b, int64_t c, int64_t d)
{
return ((a == b) ? c : d);
}
int64_t
i64_neq_select(int64_t a, int64_t b, int64_t c, int64_t d)
{
return ((a != b) ? c : d);
}
uint64_t
i64_ugt_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
{
return ((a > b) ? c : d);
}
uint64_t
i64_ule_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
{
return ((a <= b) ? c : d);
}
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
struct harness_int64_pred {
const char *fmt_string;
int64_t *lhs;
int64_t *rhs;
int64_t *select_a;
int64_t *select_b;
int expected;
int64_t *select_expected;
};
struct harness_uint64_pred {
const char *fmt_string;
uint64_t *lhs;
uint64_t *rhs;
uint64_t *select_a;
uint64_t *select_b;
int expected;
uint64_t *select_expected;
};
struct int64_pred_s {
const char *name;
int (*predfunc) (int64_t, int64_t);
int64_t (*selfunc) (int64_t, int64_t, int64_t, int64_t);
struct harness_int64_pred *tests;
int n_tests;
};
struct uint64_pred_s {
const char *name;
int (*predfunc) (uint64_t, uint64_t);
uint64_t (*selfunc) (uint64_t, uint64_t, uint64_t, uint64_t);
struct harness_uint64_pred *tests;
int n_tests;
};
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
struct harness_int64_pred int64_tests_eq[] = {
{"a %s a", &a, &a, &c, &d, TRUE_VAL, &c},
{"a %s b", &a, &b, &c, &d, FALSE_VAL, &d},
{"a %s c", &a, &c, &c, &d, FALSE_VAL, &d},
{"d %s e", &d, &e, &c, &d, FALSE_VAL, &d},
{"e %s e", &e, &e, &c, &d, TRUE_VAL, &c}
};
struct harness_int64_pred int64_tests_neq[] = {
{"a %s a", &a, &a, &c, &d, FALSE_VAL, &d},
{"a %s b", &a, &b, &c, &d, TRUE_VAL, &c},
{"a %s c", &a, &c, &c, &d, TRUE_VAL, &c},
{"d %s e", &d, &e, &c, &d, TRUE_VAL, &c},
{"e %s e", &e, &e, &c, &d, FALSE_VAL, &d}
};
struct int64_pred_s int64_preds[] = {
{"eq", i64_eq, i64_eq_select,
int64_tests_eq, ARR_SIZE(int64_tests_eq)},
{"neq", i64_neq, i64_neq_select,
int64_tests_neq, ARR_SIZE(int64_tests_neq)}
};
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
struct harness_uint64_pred uint64_tests_ugt[] = {
{"a %s a", (uint64_t *) &a, (uint64_t *) &a, (uint64_t *) &c,
(uint64_t *) &d, FALSE_VAL, (uint64_t *) &d},
{"a %s b", (uint64_t *) &a, (uint64_t *) &b, (uint64_t *) &c,
(uint64_t *) &d, FALSE_VAL, (uint64_t *) &d },
{"a %s c", (uint64_t *) &a, (uint64_t *) &c, (uint64_t *) &c,
(uint64_t *) &d, TRUE_VAL, (uint64_t *) &c },
{"d %s e", (uint64_t *) &d, (uint64_t *) &e, (uint64_t *) &c,
(uint64_t *) &d, TRUE_VAL, (uint64_t *) &c },
{"e %s e", (uint64_t *) &e, (uint64_t *) &e, (uint64_t *) &c,
(uint64_t *) &d, FALSE_VAL, (uint64_t *) &d }
};
struct harness_uint64_pred uint64_tests_ule[] = {
{"a %s a", (uint64_t *) &a, (uint64_t *) &a, (uint64_t *) &c,
(uint64_t *) &d, TRUE_VAL, (uint64_t *) &c},
{"a %s b", (uint64_t *) &a, (uint64_t *) &b, (uint64_t *) &c,
(uint64_t *) &d, TRUE_VAL, (uint64_t *) &c},
{"a %s c", (uint64_t *) &a, (uint64_t *) &c, (uint64_t *) &c,
(uint64_t *) &d, FALSE_VAL, (uint64_t *) &d},
{"d %s e", (uint64_t *) &d, (uint64_t *) &e, (uint64_t *) &c,
(uint64_t *) &d, FALSE_VAL, (uint64_t *) &d},
{"e %s e", (uint64_t *) &e, (uint64_t *) &e, (uint64_t *) &c,
(uint64_t *) &d, TRUE_VAL, (uint64_t *) &c}
};
struct uint64_pred_s uint64_preds[] = {
{"ugt", i64_ugt, i64_ugt_select,
uint64_tests_ugt, ARR_SIZE(uint64_tests_ugt)},
{"ule", i64_ule, i64_ule_select,
uint64_tests_ule, ARR_SIZE(uint64_tests_ule)}
};
int
compare_expect_int64(const struct int64_pred_s * pred)
{
int j, failed = 0;
for (j = 0; j < pred->n_tests; ++j) {
int pred_result =
(*pred->predfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs);
if (pred_result != pred->tests[j].expected) {
char str[64];
sprintf(str, pred->tests[j].fmt_string, pred->name);
printf("%s: returned value is %d, expecting %d\n", str,
pred_result, pred->tests[j].expected);
printf(" lhs = %19lld (0x%016llx)\n", *pred->tests[j].lhs, *pred->tests[j].lhs);
printf(" rhs = %19lld (0x%016llx)\n", *pred->tests[j].rhs, *pred->tests[j].rhs);
++failed;
} else {
int64_t selresult = (pred->selfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs,
*pred->tests[j].select_a, *pred->tests[j].select_b);
if (selresult != *pred->tests[j].select_expected) {
char str[64];
sprintf(str, pred->tests[j].fmt_string, pred->name);
printf("%s select: returned value is %d, expecting %d\n", str,
pred_result, pred->tests[j].expected);
printf(" lhs = %19lld (0x%016llx)\n", *pred->tests[j].lhs, *pred->tests[j].lhs);
printf(" rhs = %19lld (0x%016llx)\n", *pred->tests[j].rhs, *pred->tests[j].rhs);
printf(" true = %19lld (0x%016llx)\n", *pred->tests[j].select_a, *pred->tests[j].select_a);
printf(" false = %19lld (0x%016llx)\n", *pred->tests[j].select_b, *pred->tests[j].select_b);
++failed;
}
}
}
return failed;
}
int
compare_expect_uint64(const struct uint64_pred_s * pred)
{
int j, failed = 0;
for (j = 0; j < pred->n_tests; ++j) {
int pred_result = (*pred->predfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs);
if (pred_result != pred->tests[j].expected) {
char str[64];
sprintf(str, pred->tests[j].fmt_string, pred->name);
printf("%s: returned value is %d, expecting %d\n", str,
pred_result, pred->tests[j].expected);
printf(" lhs = %19llu (0x%016llx)\n", *pred->tests[j].lhs, *pred->tests[j].lhs);
printf(" rhs = %19llu (0x%016llx)\n", *pred->tests[j].rhs, *pred->tests[j].rhs);
++failed;
} else {
uint64_t selresult = (pred->selfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs,
*pred->tests[j].select_a, *pred->tests[j].select_b);
if (selresult != *pred->tests[j].select_expected) {
char str[64];
sprintf(str, pred->tests[j].fmt_string, pred->name);
printf("%s select: returned value is %d, expecting %d\n", str,
pred_result, pred->tests[j].expected);
printf(" lhs = %19llu (0x%016llx)\n", *pred->tests[j].lhs, *pred->tests[j].lhs);
printf(" rhs = %19llu (0x%016llx)\n", *pred->tests[j].rhs, *pred->tests[j].rhs);
printf(" true = %19llu (0x%016llx)\n", *pred->tests[j].select_a, *pred->tests[j].select_a);
printf(" false = %19llu (0x%016llx)\n", *pred->tests[j].select_b, *pred->tests[j].select_b);
++failed;
}
}
}
return failed;
}
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
uint64_t
i64_shl_const(uint64_t a)
{
return a << 10;
}
uint64_t
i64_shl(uint64_t a, int amt)
{
return a << amt;
}
uint64_t
i64_srl_const(uint64_t a)
{
return a >> 10;
}
uint64_t
i64_srl(uint64_t a, int amt)
{
return a >> amt;
}
int64_t
i64_sra_const(int64_t a)
{
return a >> 10;
}
int64_t
i64_sra(int64_t a, int amt)
{
return a >> amt;
}
/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
int
main(void)
{
int i, j, failed = 0;
const char *something_failed = " %d tests failed.\n";
const char *all_tests_passed = " All tests passed.\n";
printf("a = %16lld (0x%016llx)\n", a, a);
printf("b = %16lld (0x%016llx)\n", b, b);
printf("c = %16lld (0x%016llx)\n", c, c);
printf("d = %16lld (0x%016llx)\n", d, d);
printf("e = %16lld (0x%016llx)\n", e, e);
printf("f = %16lld (0x%016llx)\n", f, f);
printf("----------------------------------------\n");
for (i = 0; i < ARR_SIZE(int64_preds); ++i) {
printf("%s series:\n", int64_preds[i].name);
if ((failed = compare_expect_int64(int64_preds + i)) > 0) {
printf(something_failed, failed);
} else {
printf(all_tests_passed);
}
printf("----------------------------------------\n");
}
for (i = 0; i < ARR_SIZE(uint64_preds); ++i) {
printf("%s series:\n", uint64_preds[i].name);
if ((failed = compare_expect_uint64(uint64_preds + i)) > 0) {
printf(something_failed, failed);
} else {
printf(all_tests_passed);
}
printf("----------------------------------------\n");
}
printf("a = 0x%016llx\n", a);
printf("i64_shl_const(a) = 0x%016llx\n", i64_shl_const(a));
printf("i64_shl(a) = 0x%016llx\n", i64_shl(a, 10));
printf("i64_srl_const(a) = 0x%016llx\n", i64_srl_const(a));
printf("i64_srl(a) = 0x%016llx\n", i64_srl(a, 10));
printf("i64_sra_const(a) = 0x%016llx\n", i64_sra_const(a));
printf("i64_sra(a) = 0x%016llx\n", i64_sra(a, 10));
printf("----------------------------------------\n");
printf("f = 0x%016llx\n", f);
printf("i64_shl_const(f) = 0x%016llx\n", i64_shl_const(f));
printf("i64_shl(f) = 0x%016llx\n", i64_shl(f, 10));
printf("i64_srl_const(f) = 0x%016llx\n", i64_srl_const(f));
printf("i64_srl(f) = 0x%016llx\n", i64_srl(f, 10));
printf("i64_sra_const(f) = 0x%016llx\n", i64_sra_const(f));
printf("i64_sra(f) = 0x%016llx\n", i64_sra(f, 10));
printf("----------------------------------------\n");
return 0;
}
|