Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / vbit-test / util.c
blob63eabc8993a9ecbd3eb570afd24cb335905c33e6
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 <stdio.h> // fprintf
26 #include <stdlib.h> // exit
27 #include <assert.h> // assert
28 #if defined(__APPLE__)
29 #include <machine/endian.h>
30 #define __BYTE_ORDER BYTE_ORDER
31 #define __LITTLE_ENDIAN LITTLE_ENDIAN
32 #elif defined(__sun)
33 #define __LITTLE_ENDIAN 1234
34 #define __BIG_ENDIAN 4321
35 # if defined(_LITTLE_ENDIAN)
36 # define __BYTE_ORDER __LITTLE_ENDIAN
37 # else
38 # define __BYTE_ORDER __BIG_ENDIAN
39 # endif
40 #elif defined(__linux__)
41 #include <endian.h>
42 #else
43 #define __BYTE_ORDER BYTE_ORDER
44 #define __LITTLE_ENDIAN LITTLE_ENDIAN
45 #include <sys/endian.h>
46 #endif
47 #include <inttypes.h>
48 #include "vtest.h"
50 #include "memcheck.h" // VALGRIND_MAKE_MEM_DEFINED
53 /* Something bad happened. Cannot continue. */
54 void __attribute__((noreturn))
55 panic(const char *string)
57 fprintf(stderr, "*** OOPS: %s\n", string);
58 exit(1);
62 /* Issue a complaint because the V-bits of the result of an operation
63 differ from what was expected. */
64 void
65 complain(const irop_t *op, const test_data_t *data, vbits_t expected)
67 fprintf(stderr, "*** Incorrect result for operator %s\n", op->name);
69 int num_operands = get_num_operands(op->op);
71 for (unsigned i = 0; i < num_operands; ++i) {
72 fprintf(stderr, " opnd %u: ", i);
73 print_opnd(stderr, &data->opnds[i]);
74 fprintf(stderr, "\n");
76 fprintf(stderr, " result: ");
77 print_opnd(stderr, &data->result);
78 fprintf(stderr, "\n");
79 fprintf(stderr, " expect: vbits = ");
80 print_vbits(stderr, expected);
81 fprintf(stderr, "\n");
85 static void
86 print_value(FILE *fp, value_t val, unsigned num_bits)
88 switch (num_bits) {
89 case 1: fprintf(fp, "%02x", val.u8); break;
90 case 8: fprintf(fp, "%02x", val.u8); break;
91 case 16: fprintf(fp, "%04x", val.u16); break;
92 case 32: fprintf(fp, "%08x", val.u32); break;
93 case 64: fprintf(fp, "%016"PRIx64, val.u64); break;
94 case 128:
95 if (__BYTE_ORDER == __LITTLE_ENDIAN) {
96 fprintf(fp, "%016"PRIx64, val.u128[1]);
97 fprintf(fp, "%016"PRIx64, val.u128[0]);
98 } else {
99 fprintf(fp, "%016"PRIx64, val.u128[0]);
100 fprintf(fp, "%016"PRIx64, val.u128[1]);
102 break;
103 case 256:
104 if (__BYTE_ORDER == __LITTLE_ENDIAN) {
105 fprintf(fp, "%016"PRIx64, val.u256[3]);
106 fprintf(fp, "%016"PRIx64, val.u256[2]);
107 fprintf(fp, "%016"PRIx64, val.u256[1]);
108 fprintf(fp, "%016"PRIx64, val.u256[0]);
109 } else {
110 fprintf(fp, "%016"PRIx64, val.u256[0]);
111 fprintf(fp, "%016"PRIx64, val.u256[1]);
112 fprintf(fp, "%016"PRIx64, val.u256[2]);
113 fprintf(fp, "%016"PRIx64, val.u256[3]);
115 break;
116 default:
117 panic(__func__);
122 void
123 print_opnd(FILE *fp, const opnd_t *opnd)
125 fprintf(fp, "vbits = ");
126 print_vbits(fp, opnd->vbits);
127 /* The value itself might be partially or fully undefined, so take a
128 copy, paint the copy as defined, and print the copied value. This is
129 so as to avoid error messages from Memcheck, which are correct, but
130 confusing. */
131 volatile value_t value_copy = opnd->value;
132 VALGRIND_MAKE_MEM_DEFINED(&value_copy, sizeof(value_copy));
133 fprintf(fp, " value = ");
134 print_value(fp, value_copy, opnd->vbits.num_bits);
138 static int
139 is_floating_point_type(IRType type)
141 switch (type) {
142 case Ity_F32:
143 case Ity_F64:
144 case Ity_F128:
145 case Ity_D32:
146 case Ity_D64:
147 case Ity_D128:
148 return 1;
150 default:
151 return 0;
157 is_floating_point_op_with_rounding_mode(IROp op)
159 IRType t_dst, t_arg1, t_arg2, t_arg3, t_arg4;
161 typeof_primop(op, &t_dst, &t_arg1, &t_arg2, &t_arg3, &t_arg4);
163 // A unary operator cannot have a rounding mode
164 if (t_arg2 == Ity_INVALID) return 0;
166 if (is_floating_point_type(t_dst) ||
167 is_floating_point_type(t_arg1) ||
168 is_floating_point_type(t_arg2) ||
169 is_floating_point_type(t_arg3) ||
170 is_floating_point_type(t_arg4)) {
171 // Rounding mode, if present, is the 1st operand
172 return t_arg1 == Ity_I32;
174 return 0;
178 /* Return the number of operands for which input values can
179 be freely chosen. For floating point ops, the rounding mode
180 is not counted here, as it is restricted. */
182 get_num_operands(IROp op)
184 IRType unused, t1, t2, t3, t4;
186 typeof_primop(op, &unused, &t1, &t2, &t3, &t4);
188 int num_operands = 4;
189 if (t4 == Ity_INVALID) num_operands = 3;
190 if (t3 == Ity_INVALID) num_operands = 2;
191 if (t2 == Ity_INVALID) num_operands = 1;
193 if (is_floating_point_op_with_rounding_mode(op))
194 -- num_operands;
196 return num_operands;
200 unsigned
201 sizeof_irtype(IRType ty)
203 return sizeofIRType(ty);
207 void
208 typeof_primop(IROp op, IRType *t_dst, IRType *t_arg1, IRType *t_arg2,
209 IRType *t_arg3, IRType *t_arg4)
211 return typeOfPrimop(op, t_dst, t_arg1, t_arg2, t_arg3, t_arg4);