1 //===-- Template for diffing remquo results ---------------------*- 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 #include "utils/FPUtil/FPBits.h"
10 
11 #include <math.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 template <typename T> using RemQuoFunc = T (*)(T, T, int *);
16 
17 template <typename T>
RemQuoDiff(RemQuoFunc<T> func1,RemQuoFunc<T> func2,const uint8_t * data,size_t size)18 void RemQuoDiff(RemQuoFunc<T> func1, RemQuoFunc<T> func2, const uint8_t *data,
19                 size_t size) {
20   constexpr size_t typeSize = sizeof(T);
21   if (size < 2 * typeSize)
22     return;
23 
24   T x = *reinterpret_cast<const T *>(data);
25   T y = *reinterpret_cast<const T *>(data + typeSize);
26 
27   int q1, q2;
28   T remainder1 = func1(x, y, &q1);
29   T remainder2 = func2(x, y, &q2);
30 
31   if (isnan(remainder1)) {
32     if (!isnan(remainder2))
33       __builtin_trap();
34     return;
35   }
36 
37   if (isinf(remainder2) != isinf(remainder1))
38     __builtin_trap();
39 
40   // Compare only the 3 LS bits of the quotient.
41   if ((q1 & 0x7) != (q2 & 0x7))
42     __builtin_trap();
43 
44   __llvm_libc::fputil::FPBits<T> bits1(remainder1);
45   __llvm_libc::fputil::FPBits<T> bits2(remainder2);
46   if (bits1.bitsAsUInt() != bits2.bitsAsUInt())
47     __builtin_trap();
48 }
49