• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utility class to test different flavors of fdim ---------*- 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/BasicOperations.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/UnitTest/Test.h"
13 #include <math.h>
14 
15 template <typename T>
16 class FDimTestTemplate : public __llvm_libc::testing::Test {
17 public:
18   using FuncPtr = T (*)(T, T);
19   using FPBits = __llvm_libc::fputil::FPBits<T>;
20   using UIntType = typename FPBits::UIntType;
21 
testNaNArg(FuncPtr func)22   void testNaNArg(FuncPtr func) {
23     EXPECT_FP_EQ(nan, func(nan, inf));
24     EXPECT_FP_EQ(nan, func(negInf, nan));
25     EXPECT_FP_EQ(nan, func(nan, zero));
26     EXPECT_FP_EQ(nan, func(negZero, nan));
27     EXPECT_FP_EQ(nan, func(nan, T(-1.2345)));
28     EXPECT_FP_EQ(nan, func(T(1.2345), nan));
29     EXPECT_NE(isnan(func(nan, nan)), 0);
30   }
31 
testInfArg(FuncPtr func)32   void testInfArg(FuncPtr func) {
33     EXPECT_FP_EQ(zero, func(negInf, inf));
34     EXPECT_FP_EQ(inf, func(inf, zero));
35     EXPECT_FP_EQ(zero, func(negZero, inf));
36     EXPECT_FP_EQ(inf, func(inf, T(1.2345)));
37     EXPECT_FP_EQ(zero, func(T(-1.2345), inf));
38   }
39 
testNegInfArg(FuncPtr func)40   void testNegInfArg(FuncPtr func) {
41     EXPECT_FP_EQ(inf, func(inf, negInf));
42     EXPECT_FP_EQ(zero, func(negInf, zero));
43     EXPECT_FP_EQ(inf, func(negZero, negInf));
44     EXPECT_FP_EQ(zero, func(negInf, T(-1.2345)));
45     EXPECT_FP_EQ(inf, func(T(1.2345), negInf));
46   }
47 
testBothZero(FuncPtr func)48   void testBothZero(FuncPtr func) {
49     EXPECT_FP_EQ(zero, func(zero, zero));
50     EXPECT_FP_EQ(zero, func(zero, negZero));
51     EXPECT_FP_EQ(zero, func(negZero, zero));
52     EXPECT_FP_EQ(zero, func(negZero, negZero));
53   }
54 
testInRange(FuncPtr func)55   void testInRange(FuncPtr func) {
56     constexpr UIntType count = 10000001;
57     constexpr UIntType step = UIntType(-1) / count;
58     for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
59          ++i, v += step, w -= step) {
60       T x = FPBits(v), y = FPBits(w);
61       if (isnan(x) || isinf(x))
62         continue;
63       if (isnan(y) || isinf(y))
64         continue;
65 
66       if (x > y) {
67         EXPECT_FP_EQ(x - y, func(x, y));
68       } else {
69         EXPECT_FP_EQ(zero, func(x, y));
70       }
71     }
72   }
73 
74 private:
75   // constexpr does not work on FPBits yet, so we cannot have these constants as
76   // static.
77   const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1);
78   const T inf = __llvm_libc::fputil::FPBits<T>::inf();
79   const T negInf = __llvm_libc::fputil::FPBits<T>::negInf();
80   const T zero = __llvm_libc::fputil::FPBits<T>::zero();
81   const T negZero = __llvm_libc::fputil::FPBits<T>::negZero();
82 };
83