-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimsum_fuzz.cc
478 lines (419 loc) · 15.4 KB
/
dimsum_fuzz.cc
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dimsum.h"
#include "dimsum_x86.h"
#include "simulated.h"
namespace dimsum {
template <typename T, typename Abi>
bool Equal(const Simd<T, Abi>& lhs, const Simd<T, Abi>& rhs) {
for (size_t i = 0; i < lhs.size(); i++) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
template <typename T, typename Abi>
void TrapIfNotEqual(const Simd<T, Abi>& lhs, const Simd<T, Abi>& rhs) {
for (size_t i = 0; i < lhs.size(); i++) {
if (lhs[i] != rhs[i]) __builtin_trap();
}
}
template <typename T, typename Abi>
bool IsNormal(Simd<T, Abi> simd) {
if (std::is_floating_point<T>::value) {
for (size_t i = 0; i < simd.size(); i++) {
if (!std::isnormal(simd[i])) {
return false;
}
}
}
return true;
}
} // namespace dimsum
namespace {
using ::dimsum::Simd;
using ::dimsum::NativeSimd;
using ::dimsum::Simd128;
using dimsum::detail::CheckAddOverflow;
using dimsum::detail::OverflowType;
// Returns true if a floating point is in a safe range that is guaranteed to be
// represented precisely by an integral type in every rounding mode. Returns
// false for NaN.
//
// Use float -> int32 to simplify the discussion.
// The bounds are [-2**31, 2**31 - 2**(31-24)].
// The upper bound is 24 ones followed by 31-24 zeros, 24 is
// numeric_limits<float>::digits, 23 mantissa bits plus an implicit hidden bit.
// The lower and upper bounds can be represented by int32 and are thus
// safe.
// For float -> uint32, the bounds are [0, 2**32 - 2**(32-24)].
// The upper bound is 24 ones followed by 32-24 zeros.
template <typename Integral, typename T>
bool IsInSafeBound(T f) {
static_assert(std::is_floating_point<T>::value, "Unsupported type");
static_assert(std::is_integral<Integral>::value, "Unsupported type");
constexpr int sign = std::is_signed<Integral>::value;
// The least significant one bit of the upper bound.
// Note it is different for signed/unsigned types.
constexpr int bit = static_cast<int>(CHAR_BIT * sizeof(Integral) - sign) -
std::numeric_limits<T>::digits;
constexpr T lower = static_cast<T>(std::numeric_limits<Integral>::min());
// If T(max()) cannot be represented precisely by Integral,
// the upper bound is a bit pattern with numeric_limits<T>::digits ones.
// Under the FE_UPWARD rounding mode, larger numbers will be rounded up to
// be greater or equal to max()+1 which can be represented by T.
constexpr T upper =
bit > 0 ? static_cast<T>(std::numeric_limits<Integral>::max() -
(Integral{1} << bit) + 1)
: static_cast<T>(std::numeric_limits<Integral>::max());
// NaNs are also precluded.
return lower <= f && f <= upper;
}
// Converts NaN to 0 and clamp non-NaN values to a safe bound that can be
// represented precisely by Integral.
template <typename Integral, typename T, typename Abi>
bool IsSimdInSafeBound(Simd<T, Abi> simd) {
for (int i = 0; i < simd.size(); i++) {
if (!IsInSafeBound<Integral>(simd[i])) return false;
}
return true;
}
template <typename T, typename Abi>
void LoadFromRaw(const uint8_t* data, dimsum::Simd<T, Abi>* simd) {
T buffer[dimsum::Simd<T, Abi>::size()];
memcpy(buffer, data, sizeof(buffer));
*simd = dimsum::Simd<T, Abi>(buffer, dimsum::flags::element_aligned);
}
template <typename To, typename From>
void TestFloatingToIntegralStaticSimdCast(const uint8_t* data) {
NativeSimd<From> simd;
LoadFromRaw(data, &simd);
if (IsSimdInSafeBound<To>(simd)) {
TrapIfNotEqual(dimsum::simulated::static_simd_cast<To>(simd),
dimsum::static_simd_cast<To>(simd));
}
}
template <typename To, typename From>
void TestStaticSimdCast(const uint8_t* data) {
NativeSimd<From> simd;
LoadFromRaw(data, &simd);
TrapIfNotEqual(dimsum::simulated::static_simd_cast<To>(simd),
dimsum::static_simd_cast<To>(simd));
}
template <typename SimdType, size_t kArity>
void TestReduceAdd(const uint8_t* data) {
SimdType input;
LoadFromRaw(data, &input);
for (int i = 0; i < input.size(); i += kArity) {
typename SimdType::value_type sum = 0;
for (int j = 0; j < kArity; j++) {
if (dimsum::detail::CheckAddOverflow(sum, input[i + j]) !=
OverflowType::kNoOverflow) {
return;
}
sum += input[i + j];
}
}
TrapIfNotEqual(dimsum::simulated::reduce_add<kArity>(input),
dimsum::reduce_add<kArity>(input));
}
template <typename SrcType, size_t kArity>
void TestReduceAddWidened(const uint8_t* data) {
NativeSimd<SrcType> input;
LoadFromRaw(data, &input);
TrapIfNotEqual(dimsum::simulated::reduce_add_widened<kArity>(input),
dimsum::reduce_add_widened<kArity>(input));
}
template <typename T, typename Acc>
void TestMulSum(const uint8_t* data) {
NativeSimd<T> lhs, rhs;
NativeSimd<Acc> acc;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
LoadFromRaw(data + sizeof(lhs) + sizeof(rhs), &acc);
TrapIfNotEqual(dimsum::simulated::mul_sum<T>(lhs, rhs),
dimsum::mul_sum<T>(lhs, rhs));
TrapIfNotEqual(dimsum::simulated::mul_sum<T>(lhs, rhs, acc),
dimsum::mul_sum<T>(lhs, rhs, acc));
}
void TestMaddubs(const uint8_t* data) {
NativeSimd<uint8> lhs;
NativeSimd<int8> rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
TrapIfNotEqual(dimsum::simulated::maddubs(lhs, rhs),
dimsum::x86::maddubs(lhs, rhs));
}
template <typename T>
void TestAbs(const uint8_t* data) {
NativeSimd<T> simd, res, sim_res;
LoadFromRaw(data, &simd);
if (std::is_signed<T>::value) {
for (int i = 0; i < simd.size(); i++) {
if (simd[i] == std::numeric_limits<T>::min()) {
return;
}
}
}
res = abs(simd);
sim_res = dimsum::simulated::abs(simd);
for (int i = 0; i < simd.size(); i++) {
if ((std::is_integral<T>::value || !std::isnan(simd[i])) &&
res[i] != sim_res[i]) {
__builtin_trap();
}
}
}
void TestReciprocalEstimate(const uint8_t* data) {
NativeSimd<float> simd, sim_res, res;
LoadFromRaw(data, &simd);
// Each lane is independent, so we don't reject the vector if any of its lane
// is infinity, NaN, or denormalized value.
sim_res = dimsum::simulated::reciprocal_estimate(simd);
res = dimsum::reciprocal_estimate(simd);
for (size_t i = 0; i < simd.size(); i++) {
float x = simd[i], f = res[i], sim_f = sim_res[i];
// On ARM, POWER and x86, the result is NaN if the operand is NaN.
// ARM offers the least precision, the relative error may be slightly
// greater than 1/512. For some number whose
// absolute value is within a safe bound [2**-125, 2**125], if the results
// differ by a fraction of more than 1/128 (much larger than 1/512),
// consider it an error.
if (!(std::isnan(x) || sim_f == f ||
std::abs((sim_f - f) / f) < 1 / 128.f ||
std::abs(x) < std::ldexp(1.f, -125) ||
std::ldexp(1.f, 125) < std::abs(x))) {
__builtin_trap();
}
}
}
void TestSqrt(const uint8_t* data) {
NativeSimd<float> simd, res, sim_res;
LoadFromRaw(data, &simd);
// Each lane is independent, so we don't reject the vector if any of its lane
// is infinity, NaN, or denormalized value.
sim_res = dimsum::simulated::sqrt(simd);
res = dimsum::sqrt(simd);
for (int i = 0; i < NativeSimd<float>::size(); i++) {
float x = simd[i], f = res[i], sim_f = sim_res[i];
// Treat it as an error if the relative error is too large.
if (std::isnormal(x) && std::abs((sim_f - f) / f) > 1 / 1024.f) {
__builtin_trap();
}
}
}
void TestReciprocalSqrtEstimate(const uint8_t* data) {
NativeSimd<float> simd, res, sim_res;
LoadFromRaw(data, &simd);
// Each lane is independent, so we don't reject the vector if any of its lane
// is infinity, NaN, or denormalized value.
sim_res = dimsum::simulated::reciprocal_sqrt_estimate(simd);
res = dimsum::reciprocal_sqrt_estimate(simd);
for (int i = 0; i < simd.size(); i++) {
float x = simd[i], f = res[i], sim_f = sim_res[i];
// Treat it as an error if the relative error is too large.
if (std::isnormal(x) && std::abs(x) < std::ldexp(1.f, -125) &&
std::ldexp(1.f, 125) < std::abs(x) &&
std::abs((sim_f - f) / f) > 1 / 128.f) {
__builtin_trap();
}
}
}
template <typename T>
void TestRound(const uint8_t* data) {
NativeSimd<T> simd;
LoadFromRaw(data, &simd);
if (IsNormal(simd)) {
TrapIfNotEqual(dimsum::simulated::round(simd), dimsum::round(simd));
}
}
template <typename T, typename Integral>
void TestRoundToInteger(const uint8_t* data) {
NativeSimd<T> simd;
LoadFromRaw(data, &simd);
if (IsSimdInSafeBound<Integral>(simd)) {
TrapIfNotEqual(dimsum::simulated::round_to_integer<Integral>(simd),
dimsum::round_to_integer<Integral>(simd));
}
}
template <typename T>
void TestMax(const uint8_t* data) {
NativeSimd<T> lhs, rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
if (IsNormal(lhs) && IsNormal(rhs)) {
TrapIfNotEqual(dimsum::simulated::max(lhs, rhs), max(lhs, rhs));
}
}
template <typename T>
void TestMin(const uint8_t* data) {
NativeSimd<T> lhs, rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
if (IsNormal(lhs) && IsNormal(rhs)) {
TrapIfNotEqual(dimsum::simulated::min(lhs, rhs), min(lhs, rhs));
}
}
template <typename SrcType>
void TestMulWidened(const uint8_t* data) {
NativeSimd<SrcType> lhs, rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
TrapIfNotEqual(dimsum::simulated::mul_widened(lhs, rhs),
mul_widened(lhs, rhs));
}
template <typename Tin, typename Tout>
void TestPack(const uint8_t* data) {
NativeSimd<Tin> lhs, rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
auto res = pack_saturated(lhs, rhs);
auto sim_res = dimsum::simulated::pack_saturated(lhs, rhs);
static_assert(std::is_same<NativeSimd<Tout>, decltype(res)>::value,
"Result type mismatch!");
TrapIfNotEqual(sim_res, res);
}
template <typename Tin, typename Tout>
void TestPacku(const uint8_t* data) {
NativeSimd<Tin> lhs, rhs;
LoadFromRaw(data, &lhs);
LoadFromRaw(data + sizeof(lhs), &rhs);
auto res = packu_saturated(lhs, rhs);
auto sim_res = dimsum::simulated::packu_saturated(lhs, rhs);
static_assert(std::is_same<NativeSimd<Tout>, decltype(res)>::value,
"Result type mismatch!");
TrapIfNotEqual(sim_res, res);
}
template <typename T>
void TestMovemask(const uint8_t* data) {
NativeSimd<T> simd;
LoadFromRaw(data, &simd);
if (dimsum::simulated::movemask(simd) != dimsum::x86::movemask(simd)) {
__builtin_trap();
}
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size >= dimsum::detail::kMachineWidth) {
TestFloatingToIntegralStaticSimdCast<int32, float>(data);
TestFloatingToIntegralStaticSimdCast<uint32, float>(data);
TestFloatingToIntegralStaticSimdCast<int64, double>(data);
TestFloatingToIntegralStaticSimdCast<uint64, double>(data);
TestStaticSimdCast<float, int32>(data);
TestStaticSimdCast<float, uint32>(data);
TestStaticSimdCast<double, int64>(data);
TestStaticSimdCast<double, uint64>(data);
TestStaticSimdCast<int8, uint8>(data);
TestStaticSimdCast<uint8, int8>(data);
TestStaticSimdCast<int16, uint16>(data);
TestStaticSimdCast<uint16, int16>(data);
TestStaticSimdCast<int32, uint32>(data);
TestStaticSimdCast<uint32, int32>(data);
TestStaticSimdCast<int64, uint64>(data);
TestStaticSimdCast<uint64, int64>(data);
TestAbs<int8>(data);
TestAbs<int16>(data);
TestAbs<int32>(data);
TestAbs<int64>(data);
TestAbs<float>(data);
TestAbs<double>(data);
TestReciprocalEstimate(data);
TestSqrt(data);
TestRound<float>(data);
TestRound<double>(data);
TestRoundToInteger<float, int32>(data);
TestReduceAdd<Simd128<int8>, 2>(data);
TestReduceAdd<Simd128<int8>, 4>(data);
TestReduceAdd<Simd128<int8>, 8>(data);
TestReduceAdd<Simd128<int8>, 16>(data);
TestReduceAdd<Simd128<int16>, 2>(data);
TestReduceAdd<Simd128<int16>, 4>(data);
TestReduceAdd<Simd128<int16>, 8>(data);
TestReduceAdd<Simd128<int32>, 2>(data);
TestReduceAdd<Simd128<int32>, 4>(data);
TestReduceAdd<Simd128<int64>, 2>(data);
TestReduceAdd<Simd128<uint8>, 2>(data);
TestReduceAdd<Simd128<uint8>, 4>(data);
TestReduceAdd<Simd128<uint8>, 8>(data);
TestReduceAdd<Simd128<uint8>, 16>(data);
TestReduceAdd<Simd128<uint16>, 2>(data);
TestReduceAdd<Simd128<uint16>, 4>(data);
TestReduceAdd<Simd128<uint16>, 8>(data);
TestReduceAdd<Simd128<uint32>, 2>(data);
TestReduceAdd<Simd128<uint32>, 4>(data);
TestReduceAdd<Simd128<uint64>, 2>(data);
TestReduceAdd<NativeSimd<int8>, NativeSimd<int8>::size()>(data);
TestReduceAdd<NativeSimd<int16>, NativeSimd<int16>::size()>(data);
TestReduceAdd<NativeSimd<int32>, NativeSimd<int32>::size()>(data);
TestReduceAdd<NativeSimd<int64>, NativeSimd<int64>::size()>(data);
TestReduceAdd<NativeSimd<uint8>, NativeSimd<uint8>::size()>(data);
TestReduceAdd<NativeSimd<uint16>, NativeSimd<uint16>::size()>(data);
TestReduceAdd<NativeSimd<uint32>, NativeSimd<uint32>::size()>(data);
TestReduceAdd<NativeSimd<uint64>, NativeSimd<uint64>::size()>(data);
TestReduceAddWidened<int8, 2>(data);
TestReduceAddWidened<int8, 4>(data);
TestReduceAddWidened<int8, 8>(data);
TestReduceAddWidened<int16, 2>(data);
TestReduceAddWidened<int16, 4>(data);
TestReduceAddWidened<int32, 2>(data);
TestReduceAddWidened<uint8, 2>(data);
TestReduceAddWidened<uint8, 4>(data);
TestReduceAddWidened<uint8, 8>(data);
TestReduceAddWidened<uint16, 2>(data);
TestReduceAddWidened<uint16, 4>(data);
TestReduceAddWidened<uint32, 2>(data);
// ----- dimsum::x86::*
TestMovemask<int8>(data);
TestMovemask<int32>(data);
TestMovemask<int64>(data);
TestMovemask<uint8>(data);
TestMovemask<uint32>(data);
TestMovemask<uint64>(data);
}
if (size >= dimsum::detail::kMachineWidth * 2) {
TestPack<int16, int8>(data);
TestPack<int32, int16>(data);
TestPacku<int16, uint8>(data);
TestPacku<int32, uint16>(data);
TestMax<int8>(data);
TestMax<int16>(data);
TestMax<int32>(data);
TestMax<uint8>(data);
TestMax<uint16>(data);
TestMax<uint32>(data);
TestMax<float>(data);
TestMax<double>(data);
TestMin<int8>(data);
TestMin<int16>(data);
TestMin<int32>(data);
TestMin<uint8>(data);
TestMin<uint16>(data);
TestMin<uint32>(data);
TestMin<float>(data);
TestMin<double>(data);
TestMulWidened<int8>(data);
TestMulWidened<int16>(data);
TestMulWidened<int32>(data);
TestMulWidened<uint8>(data);
TestMulWidened<uint16>(data);
TestMulWidened<uint32>(data);
// ----- dimsum::x86::*
TestMaddubs(data);
}
if (size >= dimsum::detail::kMachineWidth * 3) {
TestMulSum<int16, int32>(data);
}
return 0;
}