Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / fuzzing / math / TwoInputSingleOutputDiff.h
blobe3e3fe703b139ef6bc73c025a31863a0d5eb602c
1 //===-- Template to diff two-input-single-output functions ------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H
10 #define LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H
12 #include "fuzzing/math/Compare.h"
13 #include "src/__support/FPUtil/FPBits.h"
15 #include <math.h>
16 #include <stddef.h>
17 #include <stdint.h>
19 template <typename T1, typename T2>
20 using TwoInputSingleOutputFunc = T1 (*)(T1, T2);
22 template <typename T1, typename T2>
23 void TwoInputSingleOutputDiff(TwoInputSingleOutputFunc<T1, T2> func1,
24 TwoInputSingleOutputFunc<T1, T2> func2,
25 const uint8_t *data, size_t size) {
26 constexpr size_t t1Size = sizeof(T1);
27 if (size < t1Size + sizeof(T2))
28 return;
30 T1 x = *reinterpret_cast<const T1 *>(data);
31 T2 y = *reinterpret_cast<const T2 *>(data + t1Size);
33 T1 result1 = func1(x, y);
34 T1 result2 = func2(x, y);
36 if (!ValuesEqual(result1, result2))
37 __builtin_trap();
40 #endif // LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H