xref: /llvm-project/libc/fuzzing/math/TwoInputSingleOutputDiff.h (revision 5d56b34807e0f6e7a6684e57bec7c1751778862c)
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H
10 #define LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H
11 
12 #include "fuzzing/math/Compare.h"
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 template <typename T1, typename T2>
18 using TwoInputSingleOutputFunc = T1 (*)(T1, T2);
19 
20 template <typename T1, typename T2>
TwoInputSingleOutputDiff(TwoInputSingleOutputFunc<T1,T2> func1,TwoInputSingleOutputFunc<T1,T2> func2,const uint8_t * data,size_t size)21 void TwoInputSingleOutputDiff(TwoInputSingleOutputFunc<T1, T2> func1,
22                               TwoInputSingleOutputFunc<T1, T2> func2,
23                               const uint8_t *data, size_t size) {
24   constexpr size_t t1Size = sizeof(T1);
25   if (size < t1Size + sizeof(T2))
26     return;
27 
28   T1 x = *reinterpret_cast<const T1 *>(data);
29   T2 y = *reinterpret_cast<const T2 *>(data + t1Size);
30 
31   T1 result1 = func1(x, y);
32   T1 result2 = func2(x, y);
33 
34   if (!ValuesEqual(result1, result2))
35     __builtin_trap();
36 }
37 
38 #endif // LLVM_LIBC_FUZZING_MATH_TWO_INPUT_SINGLE_OUTPUT_DIFF_H
39