10142bd6bSRoseZhang03 //===-- cos_fuzz.cpp ------------------------------------------------------===// 2b45d3629SRoseZhang03 // 3b45d3629SRoseZhang03 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4b45d3629SRoseZhang03 // See https://llvm.org/LICENSE.txt for license information. 5b45d3629SRoseZhang03 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6b45d3629SRoseZhang03 // 7b45d3629SRoseZhang03 //===----------------------------------------------------------------------===// 8b45d3629SRoseZhang03 /// 9b45d3629SRoseZhang03 /// Fuzzing test for llvm-libc cos implementation. 10b45d3629SRoseZhang03 /// 11b45d3629SRoseZhang03 //===----------------------------------------------------------------------===// 12b45d3629SRoseZhang03 13b45d3629SRoseZhang03 #include "src/math/cos.h" 14*2d95dee0SRoseZhang03 #include "utils/MPFRWrapper/mpfr_inc.h" 15b45d3629SRoseZhang03 #include <math.h> 16b45d3629SRoseZhang03 17b45d3629SRoseZhang03 extern "C" int LLVMFuzzerTestOneInput(const double x) { 18b45d3629SRoseZhang03 // remove NaN and inf as preconditions 19b45d3629SRoseZhang03 if (isnan(x)) 20b45d3629SRoseZhang03 return 0; 21b45d3629SRoseZhang03 if (isinf(x)) 22b45d3629SRoseZhang03 return 0; 23b45d3629SRoseZhang03 // signed zeros already tested in unit tests 24b45d3629SRoseZhang03 if (signbit(x) && x == 0.0) 25b45d3629SRoseZhang03 return 0; 26b45d3629SRoseZhang03 mpfr_t input; 27b45d3629SRoseZhang03 mpfr_init2(input, 53); 28b45d3629SRoseZhang03 mpfr_set_d(input, x, MPFR_RNDN); 29b45d3629SRoseZhang03 int output = mpfr_cos(input, input, MPFR_RNDN); 30b45d3629SRoseZhang03 mpfr_subnormalize(input, output, MPFR_RNDN); 31b45d3629SRoseZhang03 double to_compare = mpfr_get_d(input, MPFR_RNDN); 32b45d3629SRoseZhang03 33b45d3629SRoseZhang03 double result = LIBC_NAMESPACE::cos(x); 34b45d3629SRoseZhang03 35b45d3629SRoseZhang03 if (result != to_compare) 36b45d3629SRoseZhang03 __builtin_trap(); 37b45d3629SRoseZhang03 38b45d3629SRoseZhang03 mpfr_clear(input); 39b45d3629SRoseZhang03 return 0; 40b45d3629SRoseZhang03 } 41