File: test_value_fuzz.cc

package info (click to toggle)
yosys 0.52-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 69,796 kB
  • sloc: ansic: 696,955; cpp: 239,736; python: 14,617; yacc: 3,529; sh: 2,175; makefile: 1,945; lex: 697; perl: 445; javascript: 323; tcl: 162; vhdl: 115
file content (360 lines) | stat: -rw-r--r-- 7,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <limits>
#include <random>
#include <type_traits>

#include "cxxrtl/cxxrtl.h"

template<typename T>
T rand_int(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
	static_assert(std::is_integral<T>::value, "T must be an integral type.");
	static_assert(!std::is_same<T, signed char>::value && !std::is_same<T, unsigned char>::value,
			  "Using char with uniform_int_distribution is undefined behavior.");

	static std::mt19937 generator = [] {
		std::random_device rd;
		std::mt19937 mt{rd()};
		return mt;
	}();

	std::uniform_int_distribution<T> dist(min, max);
	return dist(generator);
}

int64_t sext(size_t bits, uint64_t value)
{
	return (int64_t)(value << (64 - bits)) >> (64 - bits);
}

struct BinaryOperationBase
{
	void tweak_input(uint64_t &a, uint64_t &b) {}
};

template<size_t Bits, typename Operation>
void test_binary_operation_for_bitsize(Operation &op)
{
	constexpr int iteration_count = 10000000;

	constexpr uint64_t mask = std::numeric_limits<uint64_t>::max() >> (64 - Bits);

	using chunk_type = typename cxxrtl::value<Bits>::chunk::type;
	constexpr size_t chunk_bits = cxxrtl::value<Bits>::chunk::bits;

	for (int iteration = 0; iteration < iteration_count; iteration++) {
		uint64_t ia = rand_int<uint64_t>() >> (64 - Bits);
		uint64_t ib = rand_int<uint64_t>() >> (64 - Bits);
		op.tweak_input(ia, ib);

		cxxrtl::value<Bits> va, vb;
		for (size_t i = 0; i * chunk_bits < Bits; i++) {
			va.data[i] = (chunk_type)(ia >> (i * chunk_bits));
			vb.data[i] = (chunk_type)(ib >> (i * chunk_bits));
		}

		uint64_t iresult = op.reference_impl(Bits, ia, ib) & mask;
		cxxrtl::value<Bits> vresult = op.template testing_impl<Bits>(va, vb);

		for (size_t i = 0; i * chunk_bits < Bits; i++) {
			if ((chunk_type)(iresult >> (i * chunk_bits)) != vresult.data[i]) {
				std::printf("Test failure:\n");
				std::printf("Bits:    %zu\n", Bits);
				std::printf("a:       %016" PRIx64 "\n", ia);
				std::printf("b:       %016" PRIx64 "\n", ib);
				std::printf("iresult: %016" PRIx64 "\n", iresult);
				std::printf("vresult: %016" PRIx64 "\n", vresult.template get<uint64_t>());

				std::terminate();
			}
		}
	}
	std::printf("Test passed @ Bits = %zu.\n", Bits);
}

template<typename Operation>
void test_binary_operation(Operation &op)
{
	// Test at a variety of bitwidths
	test_binary_operation_for_bitsize<8>(op);
	test_binary_operation_for_bitsize<32>(op);
	test_binary_operation_for_bitsize<42>(op);
	test_binary_operation_for_bitsize<63>(op);
	test_binary_operation_for_bitsize<64>(op);
}

template<typename Operation>
struct UnaryOperationWrapper : BinaryOperationBase 
{
	Operation &op;

	UnaryOperationWrapper(Operation &op) : op(op) {}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return op.reference_impl(bits, a);
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return op.template testing_impl<Bits>(a);
	}
};

template<typename Operation>
void test_unary_operation(Operation &op)
{
	UnaryOperationWrapper<Operation> wrapped(op);
	test_binary_operation(wrapped);
}

struct ShlTest : BinaryOperationBase 
{
	ShlTest()
	{
		std::printf("Randomized tests for value::shl:\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return b >= 64 ? 0 : a << b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return a.shl(b);
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		b &= 0x7f;
	}
} shl;

struct ShrTest : BinaryOperationBase 
{
	ShrTest()
	{
		std::printf("Randomized tests for value::shr:\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return b >= 64 ? 0 : a >> b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return a.shr(b);
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		b &= 0x7f;
	}
} shr;

struct SshrTest : BinaryOperationBase 
{
	SshrTest()
	{
		std::printf("Randomized tests for value::sshr:\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		int64_t sa = (int64_t)(a << (64 - bits));
		return sa >> (b >= bits ? 63 : (b + 64 - bits));
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return a.sshr(b);
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		b &= 0x7f;
	}
} sshr;

struct AddTest : BinaryOperationBase 
{
	AddTest()
	{
		std::printf("Randomized tests for value::add:\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return a + b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return a.add(b);
	}
} add;

struct SubTest : BinaryOperationBase 
{
	SubTest()
	{
		std::printf("Randomized tests for value::sub:\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return a - b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return a.sub(b);
	}
} sub;

struct CtlzTest
{
	CtlzTest()
	{
		std::printf("Randomized tests for value::ctlz:\n");
		test_unary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a)
	{
		if (a == 0)
			return bits;
		if (sizeof(long) == 4)
			return __builtin_clzll(a) - (64 - bits);
		else
			return __builtin_clzl(a) - (64 - bits);
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a)
	{
		size_t result = a.ctlz();
		return cxxrtl::value<Bits>((cxxrtl::chunk_t)result);
	}
} ctlz;

struct UdivTest : BinaryOperationBase
{
	UdivTest()
	{
		std::printf("Randomized tests for value::udivmod (div):\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return a / b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return std::get<0>(a.udivmod(b));
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		if (b == 0) b = 1; // Avoid divide by zero
	}
} udiv;

struct UmodTest : BinaryOperationBase
{
	UmodTest()
	{
		std::printf("Randomized tests for value::udivmod (mod):\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return a % b;
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return std::get<1>(a.udivmod(b));
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		if (b == 0) b = 1; // Avoid divide by zero
	}
} umod;

struct SdivTest : BinaryOperationBase
{
	SdivTest()
	{
		std::printf("Randomized tests for value::sdivmod (div):\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return (uint64_t)(sext(bits, a) / sext(bits, b));
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return std::get<0>(a.sdivmod(b));
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		if (b == 0) b = 1; // Avoid divide by zero
	}
} sdiv;

struct SmodTest : BinaryOperationBase
{
	SmodTest()
	{
		std::printf("Randomized tests for value::sdivmod (mod):\n");
		test_binary_operation(*this);
	}

	uint64_t reference_impl(size_t bits, uint64_t a, uint64_t b)
	{
		return (uint64_t)(sext(bits, a) % sext(bits, b));
	}

	template<size_t Bits>
	cxxrtl::value<Bits> testing_impl(cxxrtl::value<Bits> a, cxxrtl::value<Bits> b)
	{
		return std::get<1>(a.sdivmod(b));
	}

	void tweak_input(uint64_t &, uint64_t &b)
	{
		if (b == 0) b = 1; // Avoid divide by zero
	}
} smod;

int main()
{
}