Home | History | Annotate | Download | only in vbit-test
      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 quarternary operation. */
     32 static void
     33 check_result_for_qernary(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    const opnd_t *opnd4  = &data->opnds[3];
     40    vbits_t expected_vbits;
     41 
     42    /* Only handle those undef-kinds that actually occur. */
     43    switch (op->undef_kind) {
     44    case UNDEF_ALL:
     45       expected_vbits = undefined_vbits(result->vbits.num_bits);
     46       break;
     47 
     48    case UNDEF_SAME:
     49       // SAME with respect to the 1-bits in all operands
     50       expected_vbits  = or_vbits(or_vbits(or_vbits(opnd1->vbits, opnd2->vbits),
     51                                           opnd3->vbits), opnd4->vbits);
     52       break;
     53 
     54    default:
     55       panic(__func__);
     56    }
     57 
     58    if (! equal_vbits(result->vbits, expected_vbits))
     59       complain(op, data, expected_vbits);
     60 }
     61 
     62 
     63 int
     64 test_qernary_op(const irop_t *op, test_data_t *data)
     65 {
     66    unsigned num_input_bits, i, bitpos;
     67    opnd_t *opnds = data->opnds;
     68    int tests_done = 0;
     69 
     70    /* Immediate operands are currently not supported here */
     71    assert(op->immediate_index == 0);
     72 
     73    /* For each operand, set a single bit to undefined and observe how
     74       that propagates to the output. Do this for all bits in each
     75       operand. */
     76    for (i = 0; i < 4; ++i) {
     77       num_input_bits = bitsof_irtype(opnds[i].type);
     78 
     79       opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
     80       opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
     81       opnds[2].vbits = defined_vbits(bitsof_irtype(opnds[2].type));
     82       opnds[3].vbits = defined_vbits(bitsof_irtype(opnds[3].type));
     83 
     84       for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
     85          opnds[i].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[i].type));
     86 
     87          valgrind_execute_test(op, data);
     88 
     89          check_result_for_qernary(op, data);
     90 
     91          tests_done++;
     92       }
     93    }
     94    return tests_done;
     95 }
     96