|
| 1 | +//===-- sincos_fuzz.cpp ---------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// Fuzzing test for llvm-libc cos implementation. |
| 10 | +/// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "src/math/sincos.h" |
| 14 | +#include "utils/MPFRWrapper/mpfr_inc.h" |
| 15 | +#include <math.h> |
| 16 | + |
| 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; |
| 24 | + mpfr_t input; |
| 25 | + mpfr_t sin_x; |
| 26 | + mpfr_t cos_x; |
| 27 | + |
| 28 | + mpfr_init2(input, 53); |
| 29 | + mpfr_init2(sin_x, 53); |
| 30 | + mpfr_init2(cos_x, 53); |
| 31 | + |
| 32 | + mpfr_set_d(input, x, MPFR_RNDN); |
| 33 | + |
| 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 | + |
| 38 | + double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN); |
| 39 | + double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN); |
| 40 | + |
| 41 | + double sin_res, cos_res; |
| 42 | + LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res); |
| 43 | + |
| 44 | + if (sin_res != to_compare_sin || cos_res != to_compare_cos) |
| 45 | + __builtin_trap(); |
| 46 | + |
| 47 | + mpfr_clear(input); |
| 48 | + mpfr_clear(sin_x); |
| 49 | + mpfr_clear(cos_x); |
| 50 | + return 0; |
| 51 | +} |
0 commit comments