summaryrefslogtreecommitdiff
path: root/libc/test/src/math/smoke/SetPayloadTest.h
blob: b86b3250f03a8da455d85ab84e4f598891bd352f (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
//===-- Utility class to test different flavors of setpayload ---*- 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 LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H
#define LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H

#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

using LIBC_NAMESPACE::Sign;

template <typename T>
class SetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {

  DECLARE_SPECIAL_CONSTANTS(T)

public:
  typedef int (*SetPayloadFunc)(T *, T);

  void testInvalidPayloads(SetPayloadFunc func) {
    T res;

    EXPECT_EQ(1, func(&res, T(aNaN)));
    EXPECT_EQ(1, func(&res, T(neg_aNaN)));
    EXPECT_EQ(1, func(&res, T(inf)));
    EXPECT_EQ(1, func(&res, T(neg_inf)));
    EXPECT_EQ(1, func(&res, T(0.1)));
    EXPECT_EQ(1, func(&res, T(-0.1)));
    EXPECT_EQ(1, func(&res, T(-1.0)));
    EXPECT_EQ(1, func(&res, T(0x42.1p+0)));
    EXPECT_EQ(1, func(&res, T(-0x42.1p+0)));

    FPBits nan_payload_bits = FPBits::one();
    nan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 1 +
                                         FPBits::EXP_BIAS);
    T nan_payload = nan_payload_bits.get_val();
    EXPECT_EQ(1, func(&res, nan_payload));
  }

  void testValidPayloads(SetPayloadFunc func) {
    T res;

    EXPECT_EQ(0, func(&res, T(0.0)));
    EXPECT_TRUE(FPBits(res).is_quiet_nan());
    EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(res).uintval());

    EXPECT_EQ(0, func(&res, T(1.0)));
    EXPECT_TRUE(FPBits(res).is_quiet_nan());
    EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 1).uintval(), FPBits(res).uintval());

    if constexpr (FPBits::FRACTION_LEN - 1 >= 5) {
      EXPECT_EQ(0, func(&res, T(0x15.0p+0)));
      EXPECT_TRUE(FPBits(res).is_quiet_nan());
      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x15).uintval(),
                FPBits(res).uintval());
    }

    if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {
      EXPECT_EQ(0, func(&res, T(0x31.0p+0)));
      EXPECT_TRUE(FPBits(res).is_quiet_nan());
      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x31).uintval(),
                FPBits(res).uintval());
    }

    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
      EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
      EXPECT_TRUE(FPBits(res).is_quiet_nan());
      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x42).uintval(),
                FPBits(res).uintval());
    }

    if constexpr (FPBits::FRACTION_LEN - 1 >= 9) {
      EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
      EXPECT_TRUE(FPBits(res).is_quiet_nan());
      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x123).uintval(),
                FPBits(res).uintval());
    }

    // The following code is creating a NaN payload manually to prevent a
    // conversion from BigInt to float128.
    FPBits nan_payload_bits = FPBits::one();
    nan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 2 +
                                         FPBits::EXP_BIAS);
    nan_payload_bits.set_mantissa(FPBits::SIG_MASK - 3);
    T nan_payload = nan_payload_bits.get_val();

    EXPECT_EQ(0, func(&res, nan_payload));
    EXPECT_TRUE(FPBits(res).is_quiet_nan());
    EXPECT_EQ(
        FPBits::quiet_nan(Sign::POS, FPBits::FRACTION_MASK >> 1).uintval(),
        FPBits(res).uintval());
  }
};

#define LIST_SETPAYLOAD_TESTS(T, func)                                         \
  using LlvmLibcSetPayloadTest = SetPayloadTestTemplate<T>;                    \
  TEST_F(LlvmLibcSetPayloadTest, InvalidPayloads) {                            \
    testInvalidPayloads(&func);                                                \
  }                                                                            \
  TEST_F(LlvmLibcSetPayloadTest, ValidPayloads) { testValidPayloads(&func); }

#endif // LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H