1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 
3 #include <assert.h>
4 #include "vtest.h"
5 
6 
7 /* Check the result of a unary operation. */
8 static void
check_result_for_unary(const irop_t * op,const test_data_t * data)9 check_result_for_unary(const irop_t *op, const test_data_t *data)
10 {
11    const opnd_t *result = &data->result;
12    const opnd_t *opnd   = &data->opnds[0];
13    unsigned num_bits = result->vbits.num_bits;
14    vbits_t expected_vbits;
15 
16    /* Only handle those undef-kinds that actually occur. */
17    switch (op->undef_kind) {
18    case UNDEF_ALL:
19       expected_vbits = undefined_vbits(num_bits);
20       break;
21 
22    case UNDEF_SAME:
23       expected_vbits = opnd->vbits;
24       break;
25 
26    case UNDEF_TRUNC:
27       expected_vbits = truncate_vbits(opnd->vbits, num_bits);
28       break;
29 
30    case UNDEF_LEFT:
31       expected_vbits = left_vbits(opnd->vbits, num_bits);
32       break;
33 
34    case UNDEF_UPPER:
35       assert(num_bits * 2 == opnd->vbits.num_bits);
36       expected_vbits = upper_vbits(opnd->vbits);
37       break;
38 
39    case UNDEF_SEXT:
40       expected_vbits = sextend_vbits(opnd->vbits, num_bits);
41       break;
42 
43    case UNDEF_ZEXT:
44       expected_vbits = zextend_vbits(opnd->vbits, num_bits);
45       break;
46 
47    default:
48       panic(__func__);
49    }
50 
51    if (! equal_vbits(result->vbits, expected_vbits))
52       complain(op, data, expected_vbits);
53 }
54 
55 
56 int
test_unary_op(const irop_t * op,test_data_t * data)57 test_unary_op(const irop_t *op, test_data_t *data)
58 {
59    unsigned num_input_bits, bitpos;
60    int tests_done = 0;
61 
62    num_input_bits = bitsof_irtype(data->opnds[0].type);
63 
64    for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
65       data->opnds[0].vbits = onehot_vbits(bitpos, num_input_bits);
66 
67       valgrind_execute_test(op, data);
68 
69       check_result_for_unary(op, data);
70       tests_done++;
71    }
72    return tests_done;
73 }
74