xref: /llvm-project/libc/fuzzing/math/tan_fuzz.cpp (revision 2d95dee0c06687814eeb5c883e305a7197e26a95)
10142bd6bSRoseZhang03 //===-- tan_fuzz.cpp ------------------------------------------------------===//
20142bd6bSRoseZhang03 //
30142bd6bSRoseZhang03 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40142bd6bSRoseZhang03 // See https://llvm.org/LICENSE.txt for license information.
50142bd6bSRoseZhang03 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60142bd6bSRoseZhang03 //
70142bd6bSRoseZhang03 //===----------------------------------------------------------------------===//
80142bd6bSRoseZhang03 ///
90142bd6bSRoseZhang03 /// Fuzzing test for llvm-libc tan implementation.
100142bd6bSRoseZhang03 ///
110142bd6bSRoseZhang03 //===----------------------------------------------------------------------===//
120142bd6bSRoseZhang03 
130142bd6bSRoseZhang03 #include "src/math/tan.h"
14*2d95dee0SRoseZhang03 #include "utils/MPFRWrapper/mpfr_inc.h"
150142bd6bSRoseZhang03 #include <math.h>
160142bd6bSRoseZhang03 
170142bd6bSRoseZhang03 extern "C" int LLVMFuzzerTestOneInput(const double x) {
180142bd6bSRoseZhang03   // remove NaN and inf as preconditions
190142bd6bSRoseZhang03   if (isnan(x))
200142bd6bSRoseZhang03     return 0;
210142bd6bSRoseZhang03   if (isinf(x))
220142bd6bSRoseZhang03     return 0;
230142bd6bSRoseZhang03   // signed zeros already tested in unit tests
240142bd6bSRoseZhang03   if (signbit(x) && x == 0.0)
250142bd6bSRoseZhang03     return 0;
260142bd6bSRoseZhang03   mpfr_t input;
270142bd6bSRoseZhang03   mpfr_init2(input, 53);
280142bd6bSRoseZhang03   mpfr_set_d(input, x, MPFR_RNDN);
290142bd6bSRoseZhang03   int output = mpfr_tan(input, input, MPFR_RNDN);
300142bd6bSRoseZhang03   mpfr_subnormalize(input, output, MPFR_RNDN);
310142bd6bSRoseZhang03   double to_compare = mpfr_get_d(input, MPFR_RNDN);
320142bd6bSRoseZhang03 
330142bd6bSRoseZhang03   double result = LIBC_NAMESPACE::tan(x);
340142bd6bSRoseZhang03 
350142bd6bSRoseZhang03   if (result != to_compare)
360142bd6bSRoseZhang03     __builtin_trap();
370142bd6bSRoseZhang03 
380142bd6bSRoseZhang03   mpfr_clear(input);
390142bd6bSRoseZhang03   return 0;
400142bd6bSRoseZhang03 }
41