Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / vbit-test / binary.c
blob4ac0435b0094cd912e45cc52f94219559d559613
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 <string.h> // memset
27 #include "vtest.h"
30 /* A convenience function to compute either v1 & ~v2 & val2 or
31 v1 & ~v2 & ~val2 depending on INVERT_VAL2. */
32 static vbits_t
33 and_combine(vbits_t v1, vbits_t v2, value_t val2, int invert_val2)
35 assert(v1.num_bits == v2.num_bits);
37 vbits_t new = { .num_bits = v2.num_bits };
39 if (invert_val2) {
40 switch (v2.num_bits) {
41 case 1: val2.u32 = ~val2.u32 & 1; break;
42 case 8: val2.u8 = ~val2.u8 & 0xff; break;
43 case 16: val2.u16 = ~val2.u16 & 0xffff; break;
44 case 32: val2.u32 = ~val2.u32; break;
45 case 64: val2.u64 = ~val2.u64; break;
46 default:
47 panic(__func__);
51 switch (v2.num_bits) {
52 case 1:
53 new.bits.u32 = (v1.bits.u32 & ~v2.bits.u32 & val2.u32) & 1;
54 break;
55 case 8:
56 new.bits.u8 = (v1.bits.u8 & ~v2.bits.u8 & val2.u8) & 0xff;
57 break;
58 case 16:
59 new.bits.u16 = (v1.bits.u16 & ~v2.bits.u16 & val2.u16) & 0xffff;
60 break;
61 case 32:
62 new.bits.u32 = (v1.bits.u32 & ~v2.bits.u32 & val2.u32);
63 break;
64 case 64:
65 new.bits.u64 = (v1.bits.u64 & ~v2.bits.u64 & val2.u64);
66 break;
67 default:
68 panic(__func__);
70 return new;
73 /* Check the result of a binary operation. */
74 static void
75 check_result_for_binary(const irop_t *op, const test_data_t *data)
77 const opnd_t *result = &data->result;
78 const opnd_t *opnd1 = &data->opnds[0];
79 const opnd_t *opnd2 = &data->opnds[1];
80 opnd_t tmp;
81 vbits_t expected_vbits;
83 /* Only handle those undef-kinds that actually occur. */
84 switch (op->undef_kind) {
85 case UNDEF_NONE:
86 expected_vbits = defined_vbits(result->vbits.num_bits);
87 break;
89 case UNDEF_ALL:
90 /* Iop_ShlD64, Iop_ShrD64, Iop_ShlD128, Iop_ShrD128 have
91 * one immediate operand in operand 2.
93 expected_vbits = undefined_vbits(result->vbits.num_bits);
94 break;
96 case UNDEF_LEFT:
97 // LEFT with respect to the leftmost 1-bit in both operands
98 expected_vbits = left_vbits(or_vbits(opnd1->vbits, opnd2->vbits),
99 result->vbits.num_bits);
100 break;
102 case UNDEF_SAME:
103 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
104 assert(opnd1->vbits.num_bits == result->vbits.num_bits);
106 // SAME with respect to the 1-bits in both operands
107 expected_vbits = or_vbits(opnd1->vbits, opnd2->vbits);
108 break;
110 case UNDEF_CONCAT:
111 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
112 assert(result->vbits.num_bits == 2 * opnd1->vbits.num_bits);
113 expected_vbits = concat_vbits(opnd1->vbits, opnd2->vbits);
114 break;
116 case UNDEF_SHL:
117 /* If any bit in the 2nd operand is undefined, so are all bits
118 of the result. */
119 if (! completely_defined_vbits(opnd2->vbits)) {
120 expected_vbits = undefined_vbits(result->vbits.num_bits);
121 } else {
122 assert(opnd2->vbits.num_bits == 8);
123 unsigned shift_amount = opnd2->value.u8;
125 expected_vbits = shl_vbits(opnd1->vbits, shift_amount);
127 break;
129 case UNDEF_SHR:
130 /* If any bit in the 2nd operand is undefined, so are all bits
131 of the result. */
132 if (! completely_defined_vbits(opnd2->vbits)) {
133 expected_vbits = undefined_vbits(result->vbits.num_bits);
134 } else {
135 assert(opnd2->vbits.num_bits == 8);
136 unsigned shift_amount = opnd2->value.u8;
138 expected_vbits = shr_vbits(opnd1->vbits, shift_amount);
140 break;
142 case UNDEF_SAR:
143 /* If any bit in the 2nd operand is undefined, so are all bits
144 of the result. */
145 if (! completely_defined_vbits(opnd2->vbits)) {
146 expected_vbits = undefined_vbits(result->vbits.num_bits);
147 } else {
148 assert(opnd2->vbits.num_bits == 8);
149 unsigned shift_amount = opnd2->value.u8;
151 expected_vbits = sar_vbits(opnd1->vbits, shift_amount);
153 break;
155 case UNDEF_AND: {
156 /* Let v1, v2 be the V-bits of the 1st and 2nd operand, respectively
157 Let b1, b2 be the actual value of the 1st and 2nd operand, respect.
158 And output bit is undefined (i.e. its V-bit == 1), iff
159 (1) (v1 == 1) && (v2 == 1) OR
160 (2) (v1 == 1) && (v2 == 0 && b2 == 1) OR
161 (3) (v2 == 1) && (v1 == 0 && b1 == 1)
163 vbits_t term1, term2, term3;
164 term1 = and_vbits(opnd1->vbits, opnd2->vbits);
165 term2 = and_combine(opnd1->vbits, opnd2->vbits, opnd2->value, 0);
166 term3 = and_combine(opnd2->vbits, opnd1->vbits, opnd1->value, 0);
167 expected_vbits = or_vbits(term1, or_vbits(term2, term3));
168 break;
171 case UNDEF_OR: {
172 /* Let v1, v2 be the V-bits of the 1st and 2nd operand, respectively
173 Let b1, b2 be the actual value of the 1st and 2nd operand, respect.
174 And output bit is undefined (i.e. its V-bit == 1), iff
175 (1) (v1 == 1) && (v2 == 1) OR
176 (2) (v1 == 1) && (v2 == 0 && b2 == 0) OR
177 (3) (v2 == 1) && (v1 == 0 && b1 == 0)
179 vbits_t term1, term2, term3;
180 term1 = and_vbits(opnd1->vbits, opnd2->vbits);
181 term2 = and_combine(opnd1->vbits, opnd2->vbits, opnd2->value, 1);
182 term3 = and_combine(opnd2->vbits, opnd1->vbits, opnd1->value, 1);
183 expected_vbits = or_vbits(term1, or_vbits(term2, term3));
184 break;
187 case UNDEF_ORD:
188 /* Set expected_vbits for the Iop_CmpORD category of iops.
189 * If any of the input bits is undefined the least significant
190 * three bits in the result will be set, i.e. 0xe.
192 expected_vbits = cmpord_vbits(opnd1->vbits.num_bits,
193 opnd2->vbits.num_bits);
194 break;
196 case UNDEF_CMP_EQ_NE:
197 expected_vbits = cmp_eq_ne_vbits(opnd1->vbits, opnd2->vbits,
198 opnd1->value, opnd2->value);
199 break;
201 case UNDEF_INT_ADD:
202 expected_vbits = int_add_or_sub_vbits(1/*isAdd*/,
203 opnd1->vbits, opnd2->vbits,
204 opnd1->value, opnd2->value);
205 break;
207 case UNDEF_INT_SUB:
208 expected_vbits = int_add_or_sub_vbits(0/*!isAdd*/,
209 opnd1->vbits, opnd2->vbits,
210 opnd1->value, opnd2->value);
211 break;
213 case UNDEF_ALL_64x2:
214 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
215 expected_vbits =
216 undefined_vbits_BxE(64, 2,
217 or_vbits(opnd1->vbits, opnd2->vbits));
218 break;
220 case UNDEF_ALL_32x4:
221 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
222 expected_vbits =
223 undefined_vbits_BxE(32, 4,
224 or_vbits(opnd1->vbits, opnd2->vbits));
225 break;
227 case UNDEF_ALL_16x8:
228 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
229 expected_vbits =
230 undefined_vbits_BxE(16, 8,
231 or_vbits(opnd1->vbits, opnd2->vbits));
232 break;
234 case UNDEF_ALL_8x16:
235 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
236 expected_vbits =
237 undefined_vbits_BxE(8, 16,
238 or_vbits(opnd1->vbits, opnd2->vbits));
239 break;
241 case UNDEF_ALL_32x4_EVEN:
242 /* Only even input bytes are used, result can be twice as wide */
243 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
244 expected_vbits =
245 undefined_vbits_BxE(64, 2,
246 undefined_vbits_128_even_element(32, 4,
247 or_vbits(opnd1->vbits, opnd2->vbits)));
248 break;
250 case UNDEF_ALL_16x8_EVEN:
251 /* Only even input bytes are used, result can be twice as wide */
252 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
253 expected_vbits =
254 undefined_vbits_BxE(32, 4,
255 undefined_vbits_128_even_element(16, 8,
256 or_vbits(opnd1->vbits, opnd2->vbits)));
257 break;
259 case UNDEF_ALL_8x16_EVEN:
260 /* Only even input bytes are used, result can be twice as wide */
261 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
262 expected_vbits =
263 undefined_vbits_BxE(16, 8,
264 undefined_vbits_128_even_element(8, 16,
265 or_vbits(opnd1->vbits, opnd2->vbits)));
266 break;
268 case UNDEF_64x2_ROTATE:
269 /* Rotate left each element in opnd1 by the amount in the corresponding
270 * element of opnd2.
272 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
273 /* Setup the tmp to match what the vbit tester seems to use. I can't
274 * use opnd2-value since valgrind doesn't think it has been set.
276 tmp.value.u128[0] = -1;
277 tmp.value.u128[1] = -1;
278 /* Calculate expected for the first operand when it is shifted.
279 * If any of the vbits are set for the shift field of the second operand
280 * then the result of the expected result for that element is all 1's.
282 expected_vbits = or_vbits(undefined_vbits_BxE_rotate(64, 2, opnd1->vbits,
283 tmp.value),
284 undefined_vbits_BxE(64, 2, opnd2->vbits));
285 break;
287 case UNDEF_32x4_ROTATE:
288 /* Rotate left each element in opnd1 by the amount in the corresponding
289 * element of opnd2.
291 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
292 expected_vbits = undefined_vbits_BxE_rotate(32, 4, opnd1->vbits,
293 opnd2->value);
294 break;
296 case UNDEF_16x8_ROTATE:
297 /* Rotate left each element in opnd1 by the amount in the corresponding
298 * element of opnd2.
300 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
301 expected_vbits = undefined_vbits_BxE_rotate(16, 8, opnd1->vbits,
302 opnd2->value);
303 break;
305 case UNDEF_8x16_ROTATE:
306 /* Rotate left each element in opnd1 by the amount in the corresponding
307 * element of opnd2.
309 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
310 expected_vbits = undefined_vbits_BxE_rotate(16, 8, opnd1->vbits,
311 opnd2->value);
312 break;
314 case UNDEF_SOME:
315 /* The result for the Iop_SHA256 and Iop_SHA256 is a secure hash. If
316 * one of the input bits is not defined there must be atleast one
317 * undefined bit in the output. Which bit and how many depends on
318 * which bit is undefined. Don't know the secure hash algorithm so
319 * we can only make sure at least one of the result bits is set.
321 * The Iop_SHA256, Iop_SHA512 iops have one immediate value in the
322 * second operand.
324 expected_vbits.num_bits = result->vbits.num_bits;
326 if ((result->vbits.bits.u128[0] != 0) ||
327 (result->vbits.bits.u128[1] != 0)) {
328 expected_vbits.bits.u128[0] = result->vbits.bits.u128[0];
329 expected_vbits.bits.u128[1] = result->vbits.bits.u128[1];
331 } else {
332 /* The input had at least one vbit set but the result doesn't have any
333 * bit set. Set them all so we will trigger the error on the call
334 * to complain().
336 expected_vbits.bits.u128[0] = ~0x0ULL;
337 expected_vbits.bits.u128[1] = ~0x0ULL;
339 break;
341 case UNDEF_NARROW256_AtoB:
342 assert(opnd1->vbits.num_bits == opnd2->vbits.num_bits);
343 switch(op->op) {
344 case Iop_NarrowBin64to32x4:
345 expected_vbits =
346 undefined_vbits_Narrow256_AtoB(64, 32, opnd1->vbits, opnd1->value,
347 opnd2->vbits, opnd2->value,
348 False);
349 break;
350 case Iop_QNarrowBin64Sto32Sx4:
351 expected_vbits =
352 undefined_vbits_Narrow256_AtoB(64, 32, opnd1->vbits, opnd1->value,
353 opnd2->vbits, opnd2->value,
354 True);
355 break;
356 case Iop_QNarrowBin64Uto32Ux4:
357 expected_vbits =
358 undefined_vbits_Narrow256_AtoB(64, 32, opnd1->vbits, opnd1->value,
359 opnd2->vbits, opnd2->value,
360 True);
361 break;
362 default:
363 fprintf(stderr, "ERROR, unknown Iop for UNDEF_NARROW256_AtoB\n");
364 panic(__func__);
366 break;
367 case UNDEF_GT_S_8x16:
368 expected_vbits = cmp_gt_vbits(1/* is_signed */, 8 /* bits_per_element */, 16 /* element_count */,
369 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
370 break;
371 case UNDEF_GT_S_16x8:
372 expected_vbits = cmp_gt_vbits(1/* is_signed */, 16 /* bits_per_element */, 8 /* element_count */,
373 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
374 break;
375 case UNDEF_GT_S_32x4:
376 expected_vbits = cmp_gt_vbits(1/* is_signed */, 32 /* bits_per_element */, 4 /* element_count */,
377 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
378 break;
379 case UNDEF_GT_S_64x2:
380 expected_vbits = cmp_gt_vbits(1/* is_signed */, 64 /* bits_per_element */, 2 /* element_count */,
381 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
382 break;
383 case UNDEF_GT_U_8x16:
384 expected_vbits = cmp_gt_vbits(0/* is_signed */, 8 /* bits_per_element */, 16 /* element_count */,
385 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
386 break;
387 case UNDEF_GT_U_16x8:
388 expected_vbits = cmp_gt_vbits(0/* is_signed */, 16 /* bits_per_element */, 8 /* element_count */,
389 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
390 break;
391 case UNDEF_GT_U_32x4:
392 expected_vbits = cmp_gt_vbits(0/* is_signed */, 32 /* bits_per_element */, 4 /* element_count */,
393 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
394 break;
395 case UNDEF_GT_U_64x2:
396 expected_vbits = cmp_gt_vbits(0/* is_signed */, 64 /* bits_per_element */, 2 /* element_count */,
397 opnd1->vbits, opnd2->vbits, opnd1->value, opnd2->value);
398 break;
400 default:
401 panic(__func__);
404 if (! equal_vbits(result->vbits, expected_vbits))
405 complain(op, data, expected_vbits);
409 static int
410 test_shift(const irop_t *op, test_data_t *data)
412 unsigned num_input_bits, i;
413 opnd_t *opnds = data->opnds;
414 int tests_done = 0;
416 /* When testing the 1st operand's undefinedness propagation,
417 do so with all possible shift amnounts */
418 for (unsigned amount = 0; amount < bitsof_irtype(opnds[0].type); ++amount) {
419 opnds[1].value.u8 = amount;
421 // 1st (left) operand
422 num_input_bits = bitsof_irtype(opnds[0].type);
424 for (i = 0; i < num_input_bits; ++i) {
425 opnds[0].vbits = onehot_vbits(i, bitsof_irtype(opnds[0].type));
426 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
428 valgrind_execute_test(op, data);
430 check_result_for_binary(op, data);
431 tests_done++;
435 // 2nd (right) operand
437 /* If the operand is an immediate value, there are no v-bits to set. */
438 if (!op->immediate_index) return tests_done;
440 num_input_bits = bitsof_irtype(opnds[1].type);
442 for (i = 0; i < num_input_bits; ++i) {
443 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
444 opnds[1].vbits = onehot_vbits(i, bitsof_irtype(opnds[1].type));
446 valgrind_execute_test(op, data);
448 check_result_for_binary(op, data);
450 tests_done++;
452 return tests_done;
456 static value_t
457 all_bits_zero_value(unsigned num_bits)
459 value_t val;
461 switch (num_bits) {
462 case 8: val.u8 = 0; break;
463 case 16: val.u16 = 0; break;
464 case 1:
465 case 32: val.u32 = 0; break;
466 case 64: val.u64 = 0; break;
467 default:
468 panic(__func__);
470 return val;
474 static value_t
475 all_bits_one_value(unsigned num_bits)
477 value_t val;
479 switch (num_bits) {
480 case 1: val.u32 = 1; break;
481 case 8: val.u8 = 0xff; break;
482 case 16: val.u16 = 0xffff; break;
483 case 32: val.u32 = ~0u; break;
484 case 64: val.u64 = ~0ull; break;
485 default:
486 panic(__func__);
488 return val;
492 static int
493 test_and(const irop_t *op, test_data_t *data)
495 unsigned num_input_bits, bitpos;
496 opnd_t *opnds = data->opnds;
497 int tests_done = 0;
499 /* Undefinedness does not propagate if the other operand is 0.
500 Use an all-bits-zero operand and test the other operand in
501 the usual way (one bit undefined at a time). */
503 // 1st (left) operand variable, 2nd operand all-bits-zero
504 num_input_bits = bitsof_irtype(opnds[0].type);
506 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
507 opnds[0].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[0].type));
508 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
509 opnds[1].value = all_bits_zero_value(bitsof_irtype(opnds[1].type));
511 valgrind_execute_test(op, data);
513 check_result_for_binary(op, data);
514 tests_done++;
517 // 2nd (right) operand variable, 1st operand all-bits-zero
518 num_input_bits = bitsof_irtype(opnds[1].type);
520 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
521 opnds[1].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[1].type));
522 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
523 opnds[0].value = all_bits_zero_value(bitsof_irtype(opnds[0].type));
525 valgrind_execute_test(op, data);
527 check_result_for_binary(op, data);
528 tests_done++;
531 /* Undefinedness propagates if the other operand is 1.
532 Use an all-bits-one operand and test the other operand in
533 the usual way (one bit undefined at a time). */
535 // 1st (left) operand variable, 2nd operand all-bits-one
536 num_input_bits = bitsof_irtype(opnds[0].type);
538 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
539 opnds[0].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[0].type));
540 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
541 opnds[1].value = all_bits_one_value(bitsof_irtype(opnds[1].type));
543 valgrind_execute_test(op, data);
545 check_result_for_binary(op, data);
546 tests_done++;
549 // 2nd (right) operand variable, 1st operand all-bits-one
550 num_input_bits = bitsof_irtype(opnds[1].type);
552 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
553 opnds[1].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[1].type));
554 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
555 opnds[0].value = all_bits_one_value(bitsof_irtype(opnds[0].type));
557 valgrind_execute_test(op, data);
559 check_result_for_binary(op, data);
560 tests_done++;
562 return tests_done;
566 static int
567 test_or(const irop_t *op, test_data_t *data)
569 unsigned num_input_bits, bitpos;
570 opnd_t *opnds = data->opnds;
571 int tests_done = 0;
573 /* Undefinedness does not propagate if the other operand is 1.
574 Use an all-bits-one operand and test the other operand in
575 the usual way (one bit undefined at a time). */
577 // 1st (left) operand variable, 2nd operand all-bits-one
578 num_input_bits = bitsof_irtype(opnds[0].type);
580 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
581 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
582 opnds[1].value = all_bits_one_value(bitsof_irtype(opnds[1].type));
584 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
585 opnds[0].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[0].type));
587 valgrind_execute_test(op, data);
589 check_result_for_binary(op, data);
590 tests_done++;
593 // 2nd (right) operand variable, 1st operand all-bits-one
594 num_input_bits = bitsof_irtype(opnds[1].type);
596 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
597 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
598 opnds[0].value = all_bits_one_value(bitsof_irtype(opnds[0].type));
600 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
601 opnds[1].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[1].type));
603 valgrind_execute_test(op, data);
605 check_result_for_binary(op, data);
606 tests_done++;
609 /* Undefinedness propagates if the other operand is 0.
610 Use an all-bits-zero operand and test the other operand in
611 the usual way (one bit undefined at a time). */
613 // 1st (left) operand variable, 2nd operand all-bits-zero
614 num_input_bits = bitsof_irtype(opnds[0].type);
616 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
617 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
618 opnds[1].value = all_bits_zero_value(bitsof_irtype(opnds[1].type));
620 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
621 opnds[0].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[0].type));
623 valgrind_execute_test(op, data);
625 check_result_for_binary(op, data);
626 tests_done++;
629 // 2nd (right) operand variable, 1st operand all-bits-zero
630 num_input_bits = bitsof_irtype(opnds[1].type);
632 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
633 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
634 opnds[0].value = all_bits_zero_value(bitsof_irtype(opnds[0].type));
636 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
637 opnds[1].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[1].type));
639 valgrind_execute_test(op, data);
641 check_result_for_binary(op, data);
642 tests_done++;
644 return tests_done;
649 test_binary_op(const irop_t *op, test_data_t *data)
651 unsigned num_input_bits, i, bitpos;
652 opnd_t *opnds = data->opnds;
653 int tests_done = 0;
655 /* Handle special cases upfront */
656 switch (op->undef_kind) {
657 case UNDEF_SHL:
658 case UNDEF_SHR:
659 case UNDEF_SAR:
660 return test_shift(op, data);
662 case UNDEF_AND:
663 return test_and(op, data);
665 case UNDEF_OR:
666 return test_or(op, data);
668 default:
669 break;
672 /* For each operand, set a single bit to undefined and observe how
673 that propagates to the output. Do this for all bits in each
674 operand. */
675 for (i = 0; i < 2; ++i) {
677 /* If this is a Iop that requires an immediate amount,
678 do not iterate the v-bits of the operand */
679 if (((i+1) == op->immediate_index)
680 && (op->immediate_index)) break;
682 num_input_bits = bitsof_irtype(opnds[i].type);
683 opnds[0].vbits = defined_vbits(bitsof_irtype(opnds[0].type));
684 opnds[1].vbits = defined_vbits(bitsof_irtype(opnds[1].type));
686 /* Set the value of the 2nd operand to something != 0. So division
687 won't crash. */
688 memset(&opnds[1].value, 0xff, sizeof opnds[1].value);
690 /* For immediate shift amounts choose a value of '1'. That value should
691 not cause a problem. Note: we always assign to the u64 member here.
692 The reason is that in ir_inject.c the value_t type is not visible.
693 The value is picked up there by interpreting the memory as an
694 ULong value. So, we rely on
695 union {
696 ULong v1; // value picked up in ir_inject.c
697 value_t v2; // value assigned here
698 } xx;
699 assert(sizeof xx.v1 == sizeof xx.v2.u64);
700 assert(xx.v1 == xx.v2.u64);
703 if (op->immediate_index > 0) {
704 assert((op->immediate_type == Ity_I8)
705 || (op->immediate_type == Ity_I16)
706 || (op->immediate_type == Ity_I32));
707 opnds[1].value.u64 = 1;
710 for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
711 opnds[i].vbits = onehot_vbits(bitpos, bitsof_irtype(opnds[i].type));
713 valgrind_execute_test(op, data);
715 check_result_for_binary(op, data);
717 tests_done++;
720 return tests_done;