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-2017 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 case UNDEF_SOME:
54 expected_vbits.num_bits = result->vbits.num_bits;
55
56 if ((result->vbits.bits.u128[0] != 0) ||
57 (result->vbits.bits.u128[1] != 0)) {
58 expected_vbits.bits.u128[0] = result->vbits.bits.u128[0];
59 expected_vbits.bits.u128[1] = result->vbits.bits.u128[1];
60
61 } else {
62 /* The input had at least one vbit set but the result doesn't have any
63 * bit set. Set them all so we will trigger the error on the call
64 * to complain().
65 */
66 expected_vbits.bits.u128[0] = ~0x0ULL;
67 expected_vbits.bits.u128[1] = ~0x0ULL;
68 }
69
70 break;
71
72 default:
73 panic(__func__);
74 }
75
76 if (! equal_vbits(result->vbits, expected_vbits))
77 complain(op, data, expected_vbits);
78 }
79
80
81 int
test_ternary_op(const irop_t * op,test_data_t * data)82 test_ternary_op(const irop_t *op, test_data_t *data)
83 {
84 unsigned num_input_bits, i, bitpos;
85 opnd_t *opnds = data->opnds;
86 int tests_done = 0;
87
88 /* For each operand, set a single bit to undefined and observe how
89 that propagates to the output. Do this for all bits in each
90 operand. */
91 for (i = 0; i < 3; ++i) {
92 num_input_bits = bitsof_irtype(opnds[i].type);
93
94 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
95 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
96 opnds[2].vbits = defined_vbits(bitsof_irtype(opnds[2].type));
97
98 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
99 opnds[i].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[i].type));
100
101 valgrind_execute_test(op, data);
102
103 check_result_for_ternary(op, data);
104
105 tests_done++;
106 }
107 }
108 return tests_done;
109 }
110