1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 
3 /*
4    This file is part of MemCheck, a heavyweight Valgrind tool for
5    detecting memory errors.
6 
7    Copyright (C) 2012-2015  Florian Krohm
8 
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2 of the
12    License, or (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22    02111-1307, USA.
23 
24    The GNU General Public License is contained in the file COPYING.
25 */
26 
27 #include <assert.h>
28 #include "vtest.h"
29 
30 
31 /* Check the result of a ternary operation. */
32 static void
check_result_for_ternary(const irop_t * op,const test_data_t * data)33 check_result_for_ternary(const irop_t *op, const test_data_t *data)
34 {
35    const opnd_t *result = &data->result;
36    const opnd_t *opnd1  = &data->opnds[0];
37    const opnd_t *opnd2  = &data->opnds[1];
38    const opnd_t *opnd3  = &data->opnds[2];
39    vbits_t expected_vbits;
40 
41    /* Only handle those undef-kinds that actually occur. */
42    switch (op->undef_kind) {
43    case UNDEF_ALL:
44       expected_vbits = undefined_vbits(result->vbits.num_bits);
45       break;
46 
47    case UNDEF_SAME:
48       // SAME with respect to the 1-bits in all operands
49       expected_vbits = or_vbits(or_vbits(opnd1->vbits, opnd2->vbits),
50                                 opnd3->vbits);
51       break;
52 
53    default:
54       panic(__func__);
55    }
56 
57    if (! equal_vbits(result->vbits, expected_vbits))
58       complain(op, data, expected_vbits);
59 }
60 
61 
62 int
test_ternary_op(const irop_t * op,test_data_t * data)63 test_ternary_op(const irop_t *op, test_data_t *data)
64 {
65    unsigned num_input_bits, i, bitpos;
66    opnd_t *opnds = data->opnds;
67    int tests_done = 0;
68 
69    /* For each operand, set a single bit to undefined and observe how
70       that propagates to the output. Do this for all bits in each
71       operand. */
72    for (i = 0; i < 3; ++i) {
73       num_input_bits = bitsof_irtype(opnds[i].type);
74 
75       opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
76       opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
77       opnds[2].vbits = defined_vbits(bitsof_irtype(opnds[2].type));
78 
79       for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
80          opnds[i].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[i].type));
81 
82          valgrind_execute_test(op, data);
83 
84          check_result_for_ternary(op, data);
85 
86 	 tests_done++;
87       }
88    }
89    return tests_done;
90 }
91