1*71db0c75SAndroid Build Coastguard Worker //===-- Utility class to test different flavors of fma --------------------===// 2*71db0c75SAndroid Build Coastguard Worker // 3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information. 5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*71db0c75SAndroid Build Coastguard Worker // 7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 8*71db0c75SAndroid Build Coastguard Worker 9*71db0c75SAndroid Build Coastguard Worker #ifndef LLVM_LIBC_TEST_SRC_MATH_FMATEST_H 10*71db0c75SAndroid Build Coastguard Worker #define LLVM_LIBC_TEST_SRC_MATH_FMATEST_H 11*71db0c75SAndroid Build Coastguard Worker 12*71db0c75SAndroid Build Coastguard Worker #include "src/stdlib/rand.h" 13*71db0c75SAndroid Build Coastguard Worker #include "src/stdlib/srand.h" 14*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FEnvSafeTest.h" 15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FPMatcher.h" 16*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h" 17*71db0c75SAndroid Build Coastguard Worker #include "utils/MPFRWrapper/MPFRUtils.h" 18*71db0c75SAndroid Build Coastguard Worker 19*71db0c75SAndroid Build Coastguard Worker namespace mpfr = LIBC_NAMESPACE::testing::mpfr; 20*71db0c75SAndroid Build Coastguard Worker 21*71db0c75SAndroid Build Coastguard Worker template <typename OutType, typename InType = OutType> 22*71db0c75SAndroid Build Coastguard Worker class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { 23*71db0c75SAndroid Build Coastguard Worker 24*71db0c75SAndroid Build Coastguard Worker struct OutConstants { 25*71db0c75SAndroid Build Coastguard Worker DECLARE_SPECIAL_CONSTANTS(OutType) 26*71db0c75SAndroid Build Coastguard Worker }; 27*71db0c75SAndroid Build Coastguard Worker 28*71db0c75SAndroid Build Coastguard Worker struct InConstants { 29*71db0c75SAndroid Build Coastguard Worker DECLARE_SPECIAL_CONSTANTS(InType) 30*71db0c75SAndroid Build Coastguard Worker }; 31*71db0c75SAndroid Build Coastguard Worker 32*71db0c75SAndroid Build Coastguard Worker using OutFPBits = typename OutConstants::FPBits; 33*71db0c75SAndroid Build Coastguard Worker using OutStorageType = typename OutConstants::StorageType; 34*71db0c75SAndroid Build Coastguard Worker using InFPBits = typename InConstants::FPBits; 35*71db0c75SAndroid Build Coastguard Worker using InStorageType = typename InConstants::StorageType; 36*71db0c75SAndroid Build Coastguard Worker 37*71db0c75SAndroid Build Coastguard Worker static constexpr OutStorageType OUT_MIN_NORMAL_U = 38*71db0c75SAndroid Build Coastguard Worker OutFPBits::min_normal().uintval(); 39*71db0c75SAndroid Build Coastguard Worker static constexpr InStorageType IN_MAX_NORMAL_U = 40*71db0c75SAndroid Build Coastguard Worker InFPBits::max_normal().uintval(); 41*71db0c75SAndroid Build Coastguard Worker static constexpr InStorageType IN_MIN_NORMAL_U = 42*71db0c75SAndroid Build Coastguard Worker InFPBits::min_normal().uintval(); 43*71db0c75SAndroid Build Coastguard Worker static constexpr InStorageType IN_MAX_SUBNORMAL_U = 44*71db0c75SAndroid Build Coastguard Worker InFPBits::max_subnormal().uintval(); 45*71db0c75SAndroid Build Coastguard Worker static constexpr InStorageType IN_MIN_SUBNORMAL_U = 46*71db0c75SAndroid Build Coastguard Worker InFPBits::min_subnormal().uintval(); 47*71db0c75SAndroid Build Coastguard Worker get_random_bit_pattern()48*71db0c75SAndroid Build Coastguard Worker InStorageType get_random_bit_pattern() { 49*71db0c75SAndroid Build Coastguard Worker InStorageType bits{0}; 50*71db0c75SAndroid Build Coastguard Worker for (InStorageType i = 0; i < sizeof(InStorageType) / 2; ++i) { 51*71db0c75SAndroid Build Coastguard Worker bits = (bits << 2) + static_cast<uint16_t>(LIBC_NAMESPACE::rand()); 52*71db0c75SAndroid Build Coastguard Worker } 53*71db0c75SAndroid Build Coastguard Worker return bits; 54*71db0c75SAndroid Build Coastguard Worker } 55*71db0c75SAndroid Build Coastguard Worker 56*71db0c75SAndroid Build Coastguard Worker public: 57*71db0c75SAndroid Build Coastguard Worker using FmaFunc = OutType (*)(InType, InType, InType); 58*71db0c75SAndroid Build Coastguard Worker test_subnormal_range(FmaFunc func)59*71db0c75SAndroid Build Coastguard Worker void test_subnormal_range(FmaFunc func) { 60*71db0c75SAndroid Build Coastguard Worker constexpr InStorageType COUNT = 100'001; 61*71db0c75SAndroid Build Coastguard Worker constexpr InStorageType STEP = 62*71db0c75SAndroid Build Coastguard Worker (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT; 63*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::srand(1); 64*71db0c75SAndroid Build Coastguard Worker for (InStorageType v = IN_MIN_SUBNORMAL_U, w = IN_MAX_SUBNORMAL_U; 65*71db0c75SAndroid Build Coastguard Worker v <= IN_MAX_SUBNORMAL_U && w >= IN_MIN_SUBNORMAL_U; 66*71db0c75SAndroid Build Coastguard Worker v += STEP, w -= STEP) { 67*71db0c75SAndroid Build Coastguard Worker InType x = InFPBits(get_random_bit_pattern()).get_val(); 68*71db0c75SAndroid Build Coastguard Worker InType y = InFPBits(v).get_val(); 69*71db0c75SAndroid Build Coastguard Worker InType z = InFPBits(w).get_val(); 70*71db0c75SAndroid Build Coastguard Worker mpfr::TernaryInput<InType> input{x, y, z}; 71*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z), 72*71db0c75SAndroid Build Coastguard Worker 0.5); 73*71db0c75SAndroid Build Coastguard Worker } 74*71db0c75SAndroid Build Coastguard Worker } 75*71db0c75SAndroid Build Coastguard Worker 76*71db0c75SAndroid Build Coastguard Worker void test_normal_range(FmaFunc func) { 77*71db0c75SAndroid Build Coastguard Worker constexpr InStorageType COUNT = 100'001; 78*71db0c75SAndroid Build Coastguard Worker constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT; 79*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::srand(1); 80*71db0c75SAndroid Build Coastguard Worker for (InStorageType v = IN_MIN_NORMAL_U, w = IN_MAX_NORMAL_U; 81*71db0c75SAndroid Build Coastguard Worker v <= IN_MAX_NORMAL_U && w >= IN_MIN_NORMAL_U; v += STEP, w -= STEP) { 82*71db0c75SAndroid Build Coastguard Worker InType x = InFPBits(v).get_val(); 83*71db0c75SAndroid Build Coastguard Worker InType y = InFPBits(w).get_val(); 84*71db0c75SAndroid Build Coastguard Worker InType z = InFPBits(get_random_bit_pattern()).get_val(); 85*71db0c75SAndroid Build Coastguard Worker mpfr::TernaryInput<InType> input{x, y, z}; 86*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z), 87*71db0c75SAndroid Build Coastguard Worker 0.5); 88*71db0c75SAndroid Build Coastguard Worker } 89*71db0c75SAndroid Build Coastguard Worker } 90*71db0c75SAndroid Build Coastguard Worker }; 91*71db0c75SAndroid Build Coastguard Worker 92*71db0c75SAndroid Build Coastguard Worker #define LIST_FMA_TESTS(T, func) \ 93*71db0c75SAndroid Build Coastguard Worker using LlvmLibcFmaTest = FmaTestTemplate<T>; \ 94*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); } \ 95*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); } 96*71db0c75SAndroid Build Coastguard Worker 97*71db0c75SAndroid Build Coastguard Worker #define LIST_NARROWING_FMA_TESTS(OutType, InType, func) \ 98*71db0c75SAndroid Build Coastguard Worker using LlvmLibcFmaTest = FmaTestTemplate<OutType, InType>; \ 99*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); } \ 100*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); } 101*71db0c75SAndroid Build Coastguard Worker 102*71db0c75SAndroid Build Coastguard Worker #endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H 103