summaryrefslogtreecommitdiff
path: root/test/src/math/smoke/CanonicalizeTest.h
blob: ab45e0eb8e94d3f3044acd4a0056bcbcc0c3cdde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===-- Utility class to test canonicalize[f|l] -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/integer_literals.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

#include "hdr/math_macros.h"

#define TEST_SPECIAL(x, y, expected, expected_exception)                       \
  EXPECT_EQ(expected, f(&x, &y));                                              \
  EXPECT_FP_EXCEPTION(expected_exception);                                     \
  LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)

#define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0)

using LIBC_NAMESPACE::operator""_u128;

template <typename T>
class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {

  DECLARE_SPECIAL_CONSTANTS(T)

public:
  typedef int (*CanonicalizeFunc)(T *, const T *);

  void testSpecialNumbers(CanonicalizeFunc f) {
    T cx;

    TEST_SPECIAL(cx, zero, 0, 0);
    EXPECT_FP_EQ(cx, zero);

    TEST_SPECIAL(cx, neg_zero, 0, 0);
    EXPECT_FP_EQ(cx, neg_zero);

    TEST_SPECIAL(cx, inf, 0, 0);
    EXPECT_FP_EQ(cx, inf);

    TEST_SPECIAL(cx, neg_inf, 0, 0);
    EXPECT_FP_EQ(cx, neg_inf);

    TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
    EXPECT_FP_EQ(cx, aNaN);
  }

  void testX64_80SpecialNumbers(CanonicalizeFunc f) {
    if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() ==
                  LIBC_NAMESPACE::fputil::FPType::X86_Binary80) {
      T cx;
      // Exponent   |       Significand      | Meaning
      //            | Bits 63-62 | Bits 61-0 |
      // All Ones   |     00     |    Zero   | Pseudo Infinity, Value = SNaN
      FPBits test1(0x00000000'00007FFF'00000000'00000000_u128);
      const T test1_val = test1.get_val();
      TEST_SPECIAL(cx, test1_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      // Exponent   |       Significand      | Meaning
      //            | Bits 63-62 | Bits 61-0 |
      // All Ones   |     00     |  Non-Zero | Pseudo NaN, Value = SNaN
      FPBits test2_1(0x00000000'00007FFF'00000000'00000001_u128);
      const T test2_1_val = test2_1.get_val();
      TEST_SPECIAL(cx, test2_1_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test2_2(0x00000000'00007FFF'00000042'70000001_u128);
      const T test2_2_val = test2_2.get_val();
      TEST_SPECIAL(cx, test2_2_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test2_3(0x00000000'00007FFF'00000000'08261001_u128);
      const T test2_3_val = test2_3.get_val();
      TEST_SPECIAL(cx, test2_3_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test2_4(0x00000000'00007FFF'00007800'08261001_u128);
      const T test2_4_val = test2_4.get_val();
      TEST_SPECIAL(cx, test2_4_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      // Exponent   |       Significand      | Meaning
      //            | Bits 63-62 | Bits 61-0 |
      // All Ones   |     01     | Anything  | Pseudo NaN, Value = SNaN
      FPBits test3_1(0x00000000'00007FFF'40000000'00000000_u128);
      const T test3_1_val = test3_1.get_val();
      TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test3_2(0x00000000'00007FFF'40000042'70000001_u128);
      const T test3_2_val = test3_2.get_val();
      TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test3_3(0x00000000'00007FFF'40000000'08261001_u128);
      const T test3_3_val = test3_3.get_val();
      TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test3_4(0x00000000'00007FFF'40007800'08261001_u128);
      const T test3_4_val = test3_4.get_val();
      TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      // Exponent   |       Significand      | Meaning
      //            |   Bit 63   | Bits 62-0 |
      // All zeroes |   One      | Anything  | Pseudo Denormal, Value =
      //            |            |           | (−1)**s × m × 2**−16382
      FPBits test4_1(0x00000000'00000000'80000000'00000000_u128);
      const T test4_1_val = test4_1.get_val();
      TEST_SPECIAL(cx, test4_1_val, 0, 0);
      EXPECT_FP_EQ(
          cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val());

      FPBits test4_2(0x00000000'00000000'80000042'70000001_u128);
      const T test4_2_val = test4_2.get_val();
      TEST_SPECIAL(cx, test4_2_val, 0, 0);
      EXPECT_FP_EQ(
          cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val());

      FPBits test4_3(0x00000000'00000000'80000000'08261001_u128);
      const T test4_3_val = test4_3.get_val();
      TEST_SPECIAL(cx, test4_3_val, 0, 0);
      EXPECT_FP_EQ(
          cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val());

      // Exponent   |       Significand      | Meaning
      //            |   Bit 63   | Bits 62-0 |
      // All Other  |   Zero     | Anything  | Unnormal, Value = SNaN
      //  Values    |            |           |
      FPBits test5_1(0x00000000'00000040'00000000'00000001_u128);
      const T test5_1_val = test5_1.get_val();
      TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test5_2(0x00000000'00000230'00000042'70000001_u128);
      const T test5_2_val = test5_2.get_val();
      TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test5_3(0x00000000'00000560'00000000'08261001_u128);
      const T test5_3_val = test5_3.get_val();
      TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test5_4(0x00000000'00000780'00000028'16000000_u128);
      const T test5_4_val = test5_4.get_val();
      TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test5_5(0x00000000'00000900'00000042'70000001_u128);
      const T test5_5_val = test5_5.get_val();
      TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);

      FPBits test5_6(0x00000000'00000AB0'00000000'08261001_u128);
      const T test5_6_val = test5_6.get_val();
      TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID);
      EXPECT_FP_EQ(cx, aNaN);
    }
  }

  void testRegularNumbers(CanonicalizeFunc f) {
    T cx;
    const T test_var_1 = T(1.0);
    TEST_REGULAR(cx, test_var_1, 0);
    EXPECT_FP_EQ(cx, test_var_1);
    const T test_var_2 = T(-1.0);
    TEST_REGULAR(cx, test_var_2, 0);
    EXPECT_FP_EQ(cx, test_var_2);
    const T test_var_3 = T(10.0);
    TEST_REGULAR(cx, test_var_3, 0);
    EXPECT_FP_EQ(cx, test_var_3);
    const T test_var_4 = T(-10.0);
    TEST_REGULAR(cx, test_var_4, 0);
    EXPECT_FP_EQ(cx, test_var_4);
    const T test_var_5 = T(1234.0);
    TEST_REGULAR(cx, test_var_5, 0);
    EXPECT_FP_EQ(cx, test_var_5);
    const T test_var_6 = T(-1234.0);
    TEST_REGULAR(cx, test_var_6, 0);
    EXPECT_FP_EQ(cx, test_var_6);
  }
};

#define LIST_CANONICALIZE_TESTS(T, func)                                       \
  using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>;                        \
  TEST_F(LlvmLibcCanonicalizeTest, SpecialNumbers) {                           \
    testSpecialNumbers(&func);                                                 \
  }                                                                            \
  TEST_F(LlvmLibcCanonicalizeTest, RegularNubmers) {                           \
    testRegularNumbers(&func);                                                 \
  }

#define X86_80_SPECIAL_CANONICALIZE_TEST(T, func)                              \
  using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>;                        \
  TEST_F(LlvmLibcCanonicalizeTest, X64_80SpecialNumbers) {                     \
    testX64_80SpecialNumbers(&func);                                           \
  }

#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H