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 "src/__support/CPP/algorithm.h"
10#include "src/__support/FPUtil/FPBits.h"
11#include "test/UnitTest/FEnvSafeTest.h"
12#include "test/UnitTest/FPMatcher.h"
13#include "test/UnitTest/Test.h"
14
15using LIBC_NAMESPACE::Sign;
16
17template <typename T>
18class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
19public:
20 using FuncPtr = T (*)(T, T);
21 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
22 using StorageType = typename FPBits::StorageType;
23
24 const T inf = FPBits::inf(Sign::POS).get_val();
25 const T neg_inf = FPBits::inf(Sign::NEG).get_val();
26 const T zero = FPBits::zero(Sign::POS).get_val();
27 const T neg_zero = FPBits::zero(Sign::NEG).get_val();
28 const T nan = FPBits::quiet_nan().get_val();
29
30 void test_nan_arg(FuncPtr func) {
31 EXPECT_FP_EQ(nan, func(nan, inf));
32 EXPECT_FP_EQ(nan, func(neg_inf, nan));
33 EXPECT_FP_EQ(nan, func(nan, zero));
34 EXPECT_FP_EQ(nan, func(neg_zero, nan));
35 EXPECT_FP_EQ(nan, func(nan, T(-1.2345)));
36 EXPECT_FP_EQ(nan, func(T(1.2345), nan));
37 EXPECT_FP_EQ(func(nan, nan), nan);
38 }
39
40 void test_inf_arg(FuncPtr func) {
41 EXPECT_FP_EQ(zero, func(neg_inf, inf));
42 EXPECT_FP_EQ(inf, func(inf, zero));
43 EXPECT_FP_EQ(zero, func(neg_zero, inf));
44 EXPECT_FP_EQ(inf, func(inf, T(1.2345)));
45 EXPECT_FP_EQ(zero, func(T(-1.2345), inf));
46 }
47
48 void test_neg_inf_arg(FuncPtr func) {
49 EXPECT_FP_EQ(inf, func(inf, neg_inf));
50 EXPECT_FP_EQ(zero, func(neg_inf, zero));
51 EXPECT_FP_EQ(inf, func(neg_zero, neg_inf));
52 EXPECT_FP_EQ(zero, func(neg_inf, T(-1.2345)));
53 EXPECT_FP_EQ(inf, func(T(1.2345), neg_inf));
54 }
55
56 void test_both_zero(FuncPtr func) {
57 EXPECT_FP_EQ(zero, func(zero, zero));
58 EXPECT_FP_EQ(zero, func(zero, neg_zero));
59 EXPECT_FP_EQ(zero, func(neg_zero, zero));
60 EXPECT_FP_EQ(zero, func(neg_zero, neg_zero));
61 }
62
63 void test_in_range(FuncPtr func) {
64 constexpr StorageType STORAGE_MAX =
65 LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
66 constexpr int COUNT = 100'001;
67 constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
68 static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
69 StorageType v = 0, w = STORAGE_MAX;
70 for (int i = 0; i <= COUNT; ++i, v += STEP, w -= STEP) {
71 FPBits xbits(v), ybits(w);
72 if (xbits.is_inf_or_nan())
73 continue;
74 if (ybits.is_inf_or_nan())
75 continue;
76
77 T x = xbits.get_val();
78 T y = ybits.get_val();
79
80 if (x > y) {
81 EXPECT_FP_EQ(x - y, func(x, y));
82 } else {
83 EXPECT_FP_EQ(zero, func(x, y));
84 }
85 }
86 }
87};
88
89#define LIST_FDIM_TESTS(T, func) \
90 using LlvmLibcFDimTest = FDimTestTemplate<T>; \
91 TEST_F(LlvmLibcFDimTest, NaNArg) { test_nan_arg(&func); } \
92 TEST_F(LlvmLibcFDimTest, InfArg) { test_inf_arg(&func); } \
93 TEST_F(LlvmLibcFDimTest, NegInfArg) { test_neg_inf_arg(&func); } \
94 TEST_F(LlvmLibcFDimTest, BothZero) { test_both_zero(&func); } \
95 TEST_F(LlvmLibcFDimTest, InFloatRange) { test_in_range(&func); } \
96 static_assert(true, "Require semicolon.")
97

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of libc/test/src/math/smoke/FDimTest.h