109 lines · c
1//===-- Utility class to test different flavors of setpayload ---*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H10#define LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H11 12#include "test/UnitTest/FEnvSafeTest.h"13#include "test/UnitTest/FPMatcher.h"14#include "test/UnitTest/Test.h"15 16using LIBC_NAMESPACE::Sign;17 18template <typename T>19class SetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {20 21 DECLARE_SPECIAL_CONSTANTS(T)22 23public:24 typedef int (*SetPayloadFunc)(T *, T);25 26 void testInvalidPayloads(SetPayloadFunc func) {27 T res;28 29 EXPECT_EQ(1, func(&res, T(aNaN)));30 EXPECT_EQ(1, func(&res, T(neg_aNaN)));31 EXPECT_EQ(1, func(&res, T(inf)));32 EXPECT_EQ(1, func(&res, T(neg_inf)));33 EXPECT_EQ(1, func(&res, T(0.1)));34 EXPECT_EQ(1, func(&res, T(-0.1)));35 EXPECT_EQ(1, func(&res, T(-1.0)));36 EXPECT_EQ(1, func(&res, T(0x42.1p+0)));37 EXPECT_EQ(1, func(&res, T(-0x42.1p+0)));38 39 FPBits nan_payload_bits = FPBits::one();40 nan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 1 +41 FPBits::EXP_BIAS);42 T nan_payload = nan_payload_bits.get_val();43 EXPECT_EQ(1, func(&res, nan_payload));44 }45 46 void testValidPayloads(SetPayloadFunc func) {47 T res;48 49 EXPECT_EQ(0, func(&res, T(0.0)));50 EXPECT_TRUE(FPBits(res).is_quiet_nan());51 EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(res).uintval());52 53 EXPECT_EQ(0, func(&res, T(1.0)));54 EXPECT_TRUE(FPBits(res).is_quiet_nan());55 EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 1).uintval(), FPBits(res).uintval());56 57 if constexpr (FPBits::FRACTION_LEN - 1 >= 5) {58 EXPECT_EQ(0, func(&res, T(0x15.0p+0)));59 EXPECT_TRUE(FPBits(res).is_quiet_nan());60 EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x15).uintval(),61 FPBits(res).uintval());62 }63 64 if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {65 EXPECT_EQ(0, func(&res, T(0x31.0p+0)));66 EXPECT_TRUE(FPBits(res).is_quiet_nan());67 EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x31).uintval(),68 FPBits(res).uintval());69 }70 71 if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {72 EXPECT_EQ(0, func(&res, T(0x42.0p+0)));73 EXPECT_TRUE(FPBits(res).is_quiet_nan());74 EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x42).uintval(),75 FPBits(res).uintval());76 }77 78 if constexpr (FPBits::FRACTION_LEN - 1 >= 9) {79 EXPECT_EQ(0, func(&res, T(0x123.0p+0)));80 EXPECT_TRUE(FPBits(res).is_quiet_nan());81 EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x123).uintval(),82 FPBits(res).uintval());83 }84 85 // The following code is creating a NaN payload manually to prevent a86 // conversion from BigInt to float128.87 FPBits nan_payload_bits = FPBits::one();88 nan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 2 +89 FPBits::EXP_BIAS);90 nan_payload_bits.set_mantissa(FPBits::SIG_MASK - 3);91 T nan_payload = nan_payload_bits.get_val();92 93 EXPECT_EQ(0, func(&res, nan_payload));94 EXPECT_TRUE(FPBits(res).is_quiet_nan());95 EXPECT_EQ(96 FPBits::quiet_nan(Sign::POS, FPBits::FRACTION_MASK >> 1).uintval(),97 FPBits(res).uintval());98 }99};100 101#define LIST_SETPAYLOAD_TESTS(T, func) \102 using LlvmLibcSetPayloadTest = SetPayloadTestTemplate<T>; \103 TEST_F(LlvmLibcSetPayloadTest, InvalidPayloads) { \104 testInvalidPayloads(&func); \105 } \106 TEST_F(LlvmLibcSetPayloadTest, ValidPayloads) { testValidPayloads(&func); }107 108#endif // LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H109