brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · 902ff04 Raw
107 lines · c
1//===-- Utility class to test different flavors of fma --------------------===//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 LLVM_LIBC_TEST_SRC_MATH_FMATEST_H10#define LLVM_LIBC_TEST_SRC_MATH_FMATEST_H11 12#include "src/stdlib/rand.h"13#include "src/stdlib/srand.h"14#include "test/UnitTest/FEnvSafeTest.h"15#include "test/UnitTest/FPMatcher.h"16#include "test/UnitTest/Test.h"17#include "utils/MPFRWrapper/MPFRUtils.h"18 19namespace mpfr = LIBC_NAMESPACE::testing::mpfr;20 21template <typename OutType, typename InType = OutType>22class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {23 24  struct OutConstants {25    DECLARE_SPECIAL_CONSTANTS(OutType)26  };27 28  struct InConstants {29    DECLARE_SPECIAL_CONSTANTS(InType)30  };31 32  using OutFPBits = typename OutConstants::FPBits;33  using OutStorageType = typename OutConstants::StorageType;34  using InFPBits = typename InConstants::FPBits;35  using InStorageType = typename InConstants::StorageType;36 37  static constexpr OutStorageType OUT_MIN_NORMAL_U =38      OutFPBits::min_normal().uintval();39  static constexpr InStorageType IN_MAX_NORMAL_U =40      InFPBits::max_normal().uintval();41  static constexpr InStorageType IN_MIN_NORMAL_U =42      InFPBits::min_normal().uintval();43  static constexpr InStorageType IN_MAX_SUBNORMAL_U =44      InFPBits::max_subnormal().uintval();45  static constexpr InStorageType IN_MIN_SUBNORMAL_U =46      InFPBits::min_subnormal().uintval();47 48  InStorageType get_random_bit_pattern() {49    InStorageType bits{0};50    for (InStorageType i = 0; i < sizeof(InStorageType) / 2; ++i) {51      bits = static_cast<InStorageType>(52          (bits << 2) + static_cast<uint16_t>(LIBC_NAMESPACE::rand()));53    }54    return bits;55  }56 57public:58  using FmaFunc = OutType (*)(InType, InType, InType);59 60  void test_subnormal_range(FmaFunc func) {61    constexpr InStorageType COUNT = 10'001;62    constexpr InStorageType RAW_STEP =63        (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;64    constexpr InStorageType STEP = (RAW_STEP == 0 ? 1 : RAW_STEP);65    LIBC_NAMESPACE::srand(1);66    for (InStorageType v = IN_MIN_SUBNORMAL_U, w = IN_MAX_SUBNORMAL_U;67         v <= IN_MAX_SUBNORMAL_U && w >= IN_MIN_SUBNORMAL_U;68         v += STEP, w -= STEP) {69      InType x = InFPBits(get_random_bit_pattern()).get_val();70      InType y = InFPBits(v).get_val();71      InType z = InFPBits(w).get_val();72      mpfr::TernaryInput<InType> input{x, y, z};73      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),74                                     0.5);75    }76  }77 78  void test_normal_range(FmaFunc func) {79    constexpr InStorageType COUNT = 10'001;80    constexpr InStorageType RAW_STEP =81        (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;82    constexpr InStorageType STEP = (RAW_STEP == 0 ? 1 : RAW_STEP);83    LIBC_NAMESPACE::srand(1);84    for (InStorageType v = IN_MIN_NORMAL_U, w = IN_MAX_NORMAL_U;85         v <= IN_MAX_NORMAL_U && w >= IN_MIN_NORMAL_U; v += STEP, w -= STEP) {86      InType x = InFPBits(v).get_val();87      InType y = InFPBits(w).get_val();88      InType z = InFPBits(get_random_bit_pattern()).get_val();89      mpfr::TernaryInput<InType> input{x, y, z};90      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),91                                     0.5);92    }93  }94};95 96#define LIST_FMA_TESTS(T, func)                                                \97  using LlvmLibcFmaTest = FmaTestTemplate<T>;                                  \98  TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); }     \99  TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); }100 101#define LIST_NARROWING_FMA_TESTS(OutType, InType, func)                        \102  using LlvmLibcFmaTest = FmaTestTemplate<OutType, InType>;                    \103  TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); }     \104  TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); }105 106#endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H107