Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / vbit-test / ternary.c
blob96d392de79715c6f2e4c43a26a475a466239ca73
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*
4 This file is part of MemCheck, a heavyweight Valgrind tool for
5 detecting memory errors.
7 Copyright (C) 2012-2017 Florian Krohm
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.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 The GNU General Public License is contained in the file COPYING.
25 #include <assert.h>
26 #include "vtest.h"
29 /* Check the result of a ternary operation. */
30 static void
31 check_result_for_ternary(const irop_t *op, const test_data_t *data)
33 const opnd_t *result = &data->result;
34 const opnd_t *opnd1 = &data->opnds[0];
35 const opnd_t *opnd2 = &data->opnds[1];
36 const opnd_t *opnd3 = &data->opnds[2];
37 vbits_t expected_vbits;
39 /* Only handle those undef-kinds that actually occur. */
40 switch (op->undef_kind) {
41 case UNDEF_ALL:
42 expected_vbits = undefined_vbits(result->vbits.num_bits);
43 break;
45 case UNDEF_SAME:
46 // SAME with respect to the 1-bits in all operands
47 expected_vbits = or_vbits(or_vbits(opnd1->vbits, opnd2->vbits),
48 opnd3->vbits);
49 break;
51 case UNDEF_SOME:
52 expected_vbits.num_bits = result->vbits.num_bits;
54 if ((result->vbits.bits.u128[0] != 0) ||
55 (result->vbits.bits.u128[1] != 0)) {
56 expected_vbits.bits.u128[0] = result->vbits.bits.u128[0];
57 expected_vbits.bits.u128[1] = result->vbits.bits.u128[1];
59 } else {
60 /* The input had at least one vbit set but the result doesn't have any
61 * bit set. Set them all so we will trigger the error on the call
62 * to complain().
64 expected_vbits.bits.u128[0] = ~0x0ULL;
65 expected_vbits.bits.u128[1] = ~0x0ULL;
68 break;
70 default:
71 panic(__func__);
74 if (! equal_vbits(result->vbits, expected_vbits))
75 complain(op, data, expected_vbits);
79 int
80 test_ternary_op(const irop_t *op, test_data_t *data)
82 unsigned num_input_bits, i, bitpos;
83 opnd_t *opnds = data->opnds;
84 int tests_done = 0;
86 /* For each operand, set a single bit to undefined and observe how
87 that propagates to the output. Do this for all bits in each
88 operand. */
89 for (i = 0; i < 3; ++i) {
90 num_input_bits = bitsof_irtype(opnds[i].type);
92 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
93 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
94 opnds[2].vbits = defined_vbits(bitsof_irtype(opnds[2].type));
96 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
97 opnds[i].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[i].type));
99 valgrind_execute_test(op, data);
101 check_result_for_ternary(op, data);
103 tests_done++;
106 return tests_done;