brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · ad15637 Raw
75 lines · c
1//===-- Utility class to test fxdivi functions ------------------*- 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#include "src/__support/CPP/type_traits.h"10#include "src/__support/fixed_point/fx_bits.h"11#include "src/__support/fixed_point/fx_rep.h"12#include "test/UnitTest/Test.h"13 14template <typename XType> XType get_epsilon() = delete;15template <> fract get_epsilon() { return FRACT_EPSILON; }16template <> unsigned fract get_epsilon() { return UFRACT_EPSILON; }17template <> long fract get_epsilon() { return LFRACT_EPSILON; }18 19template <typename XType>20class DivITest : public LIBC_NAMESPACE::testing::Test {21  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<XType>;22  using FXBits = LIBC_NAMESPACE::fixed_point::FXBits<XType>;23 24public:25  typedef XType (*DivIFunc)(int, int);26 27  void testBasic(DivIFunc func) {28    XType epsilon = get_epsilon<XType>();29    EXPECT_LT((func(2, 3) - 0.666656494140625r), epsilon);30    EXPECT_LT((func(3, 4) - 0.75r), epsilon);31    EXPECT_LT((func(1043, 2764) - 0.3773516643r), epsilon);32    EXPECT_LT((func(60000, 720293) - 0.08329943509r), epsilon);33 34    EXPECT_EQ(func(128, 256), 0.5r);35    EXPECT_EQ(func(1, 2), 0.5r);36    EXPECT_EQ(func(1, 4), 0.25r);37    EXPECT_EQ(func(1, 8), 0.125r);38    EXPECT_EQ(func(1, 16), 0.0625r);39 40    EXPECT_EQ(func(-1, 2), -0.5r);41    EXPECT_EQ(func(1, -4), -0.25r);42    EXPECT_EQ(func(-1, 8), -0.125r);43    EXPECT_EQ(func(1, -16), -0.0625r);44  }45 46  void testSpecial(DivIFunc func) {47    XType epsilon = get_epsilon<XType>();48    EXPECT_EQ(func(0, 10), 0.r);49    EXPECT_EQ(func(0, -10), 0.r);50    EXPECT_EQ(func(-(1 << FRACT_FBIT), 1 << FRACT_FBIT), FRACT_MIN);51    EXPECT_EQ(func((1 << FRACT_FBIT) - 1, 1 << FRACT_FBIT), FRACT_MAX);52    // From Section 7.18a.6.1, functions returning a fixed-point value, the53    // return value is saturated on overflow.54    EXPECT_EQ(func(INT_MAX, INT_MAX), FRACT_MAX);55    EXPECT_LT(func(INT_MAX - 1, INT_MAX) - 0.99999999r, epsilon);56    EXPECT_EQ(func(INT_MIN, INT_MAX), FRACT_MIN);57    // Expecting 0 here as fract is not precise enough to58    // handle 1/INT_MAX59    EXPECT_LT(func(1, INT_MAX) - 0.r, epsilon);60    // This results in 1.1739, which should be saturated to FRACT_MAX61    EXPECT_EQ(func(27, 23), FRACT_MAX);62 63    EXPECT_EQ(func(INT_MIN, 1), FRACT_MIN);64    EXPECT_LT(func(1, INT_MIN) - 0.r, epsilon);65 66    EXPECT_EQ(func(INT_MIN, INT_MIN), 1.r);67  }68};69 70#define LIST_DIVI_TESTS(Name, XType, func)                                     \71  using LlvmLibc##Name##diviTest = DivITest<XType>;                            \72  TEST_F(LlvmLibc##Name##diviTest, Basic) { testBasic(&func); }                \73  TEST_F(LlvmLibc##Name##diviTest, Special) { testSpecial(&func); }            \74  static_assert(true, "Require semicolon.")75