|
12 | 12 |
|
13 | 13 | #include "src/math/sincos.h" |
14 | 14 | #include "utils/MPFRWrapper/mpfr_inc.h" |
| 15 | +#include <cstdint> |
| 16 | +#include <cstring> |
| 17 | +#include <iostream> |
15 | 18 | #include <math.h> |
16 | 19 |
|
17 | | -extern "C" int LLVMFuzzerTestOneInput(double x) { |
18 | | - // remove NaN and inf as preconditions |
19 | | - if (isnan(x) || isinf(x)) |
20 | | - return 0; |
21 | | - // signed zeros already tested in unit tests |
22 | | - if (signbit(x) && x == 0.0) |
23 | | - return 0; |
| 20 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
24 | 21 | mpfr_t input; |
25 | 22 | mpfr_t sin_x; |
26 | 23 | mpfr_t cos_x; |
27 | 24 |
|
28 | 25 | mpfr_init2(input, 53); |
29 | 26 | mpfr_init2(sin_x, 53); |
30 | 27 | mpfr_init2(cos_x, 53); |
| 28 | + for (size_t i = 0; i < size / sizeof(double); ++i) { |
| 29 | + double x; |
| 30 | + std::memcpy(&x, data, sizeof(double)); |
| 31 | + data += sizeof(double); |
31 | 32 |
|
32 | | - mpfr_set_d(input, x, MPFR_RNDN); |
| 33 | + // remove NaN and inf as preconditions |
| 34 | + if (isnan(x) || isinf(x)) |
| 35 | + continue; |
33 | 36 |
|
34 | | - int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN); |
35 | | - mpfr_subnormalize(sin_x, output, MPFR_RNDN); |
36 | | - mpfr_subnormalize(cos_x, output, MPFR_RNDN); |
| 37 | + // signed zeros already tested in unit tests |
| 38 | + if (signbit(x) && x == 0.0) |
| 39 | + continue; |
37 | 40 |
|
38 | | - double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN); |
39 | | - double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN); |
| 41 | + mpfr_set_d(input, x, MPFR_RNDN); |
| 42 | + int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN); |
| 43 | + mpfr_subnormalize(sin_x, output, MPFR_RNDN); |
| 44 | + mpfr_subnormalize(cos_x, output, MPFR_RNDN); |
40 | 45 |
|
41 | | - double sin_res, cos_res; |
42 | | - LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res); |
| 46 | + double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN); |
| 47 | + double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN); |
43 | 48 |
|
44 | | - if (sin_res != to_compare_sin || cos_res != to_compare_cos) |
45 | | - __builtin_trap(); |
| 49 | + double sin_res, cos_res; |
| 50 | + LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res); |
| 51 | + |
| 52 | + if (sin_res != to_compare_sin || cos_res != to_compare_cos) { |
| 53 | + std::cout << std::hexfloat << "Failing input: " << x << std::endl; |
| 54 | + std::cout << std::hexfloat << "Failing sin output: " << sin_res |
| 55 | + << std::endl; |
| 56 | + std::cout << std::hexfloat << "Expected sin: " << to_compare_sin |
| 57 | + << std::endl; |
| 58 | + std::cout << std::hexfloat << "Failing cos output: " << cos_res |
| 59 | + << std::endl; |
| 60 | + std::cout << std::hexfloat << "Expected cos: " << to_compare_cos |
| 61 | + << std::endl; |
| 62 | + __builtin_trap(); |
| 63 | + } |
| 64 | + } |
46 | 65 |
|
47 | 66 | mpfr_clear(input); |
48 | 67 | mpfr_clear(sin_x); |
|
0 commit comments