brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 9a097a5 Raw
57 lines · cpp
1//===-- f16sqrt_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-exception6//7//===----------------------------------------------------------------------===//8///9/// Fuzzing test for llvm-libc f16sqrt implementation.10///11//===----------------------------------------------------------------------===//12 13#include "src/math/f16sqrt.h"14#include "utils/MPFRWrapper/mpfr_inc.h"15#include <cstdint>16#include <cstring>17#include <iostream>18#include <math.h>19 20extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {21  mpfr_t input;22  mpfr_t out;23  mpfr_init2(input, 53);24  mpfr_init2(out, 128);25  for (size_t i = 0; i < size / sizeof(double); ++i) {26    double x;27    std::memcpy(&x, data, sizeof(double));28    data += sizeof(double);29 30    // remove NaN, inf, and values outside the accepted range31    if (isnan(x) || isinf(x) || x < 0)32      continue;33    // signed zeros already tested in unit tests34    if (signbit(x) && x == 0.0)35      continue;36 37    mpfr_set_d(input, x, MPFR_RNDN);38    mpfr_sqrt(out, input, MPFR_RNDN);39    float16 to_compare = mpfr_get_d(out, MPFR_RNDN);40 41    float16 result = LIBC_NAMESPACE::f16sqrt(x);42 43    if (result != to_compare) {44      std::cout << std::hexfloat << "Failing input: " << x << std::endl;45      std::cout << std::hexfloat46                << "Failing output: " << static_cast<float>(result)47                << std::endl;48      std::cout << std::hexfloat49                << "Expected: " << static_cast<float>(to_compare) << std::endl;50      __builtin_trap();51    }52  }53  mpfr_clear(input);54  mpfr_clear(out);55  return 0;56}57