brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.2 KiB · cff2aeb Raw
171 lines · c
1//===-- Utility class to test different flavors of ldexp --------*- 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 LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H10#define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H11 12#include "src/__support/CPP/limits.h" // INT_MAX13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/NormalFloat.h"15#include "test/UnitTest/FEnvSafeTest.h"16#include "test/UnitTest/FPMatcher.h"17#include "test/UnitTest/Test.h"18 19#include "hdr/math_macros.h"20#include "hdr/stdint_proxy.h"21 22using LIBC_NAMESPACE::Sign;23 24template <typename T>25class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {26  using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;27  using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;28  using StorageType = typename FPBits::StorageType;29 30  const T inf = FPBits::inf(Sign::POS).get_val();31  const T neg_inf = FPBits::inf(Sign::NEG).get_val();32  const T zero = FPBits::zero(Sign::POS).get_val();33  const T neg_zero = FPBits::zero(Sign::NEG).get_val();34  const T nan = FPBits::quiet_nan().get_val();35 36  // A normalized mantissa to be used with tests.37  static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x1234;38 39public:40  typedef T (*LdExpFunc)(T, int);41 42  void testSpecialNumbers(LdExpFunc func) {43    int exp_array[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};44    for (int exp : exp_array) {45      ASSERT_FP_EQ(zero, func(zero, exp));46      ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));47      ASSERT_FP_EQ(inf, func(inf, exp));48      ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));49      ASSERT_FP_EQ(nan, func(nan, exp));50    }51  }52 53  void testPowersOfTwo(LdExpFunc func) {54    int32_t exp_array[5] = {1, 2, 3, 4, 5};55    int32_t val_array[6] = {1, 2, 4, 8, 16, 32};56    for (int32_t exp : exp_array) {57      for (int32_t val : val_array) {58        ASSERT_FP_EQ(T(val << exp), func(T(val), exp));59        ASSERT_FP_EQ(T(-1 * (val << exp)), func(T(-val), exp));60      }61    }62  }63 64  void testOverflow(LdExpFunc func) {65    NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,66                  NormalFloat::ONE + 0xF00BA);67    for (int32_t exp = 10; exp < 100; ++exp) {68      ASSERT_FP_EQ(inf, func(T(x), exp));69      ASSERT_FP_EQ(neg_inf, func(-T(x), exp));70    }71  }72 73  void testUnderflowToZeroOnNormal(LdExpFunc func) {74    // In this test, we pass a normal nubmer to func and expect zero75    // to be returned due to underflow.76    int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;77    int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,78                           base_exponent + 3, base_exponent + 2,79                           base_exponent + 1};80    T x = NormalFloat(Sign::POS, 0, MANTISSA);81    for (int32_t exp : exp_array) {82      ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);83    }84  }85 86  void testUnderflowToZeroOnSubnormal(LdExpFunc func) {87    // In this test, we pass a normal nubmer to func and expect zero88    // to be returned due to underflow.89    int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;90    int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,91                           base_exponent + 3, base_exponent + 2,92                           base_exponent + 1};93    T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);94    for (int32_t exp : exp_array) {95      ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);96    }97  }98 99  void testNormalOperation(LdExpFunc func) {100    T val_array[] = {// Normal numbers101                     NormalFloat(Sign::POS, 100, MANTISSA),102                     NormalFloat(Sign::POS, -100, MANTISSA),103                     NormalFloat(Sign::NEG, 100, MANTISSA),104                     NormalFloat(Sign::NEG, -100, MANTISSA),105                     // Subnormal numbers106                     NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),107                     NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};108    for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {109      for (T x : val_array) {110        // We compare the result of ldexp with the result111        // of the native multiplication/division instruction.112 113        // We need to use a NormalFloat here (instead of 1 << exp), because114        // there are 32 bit systems that don't support 128bit long ints but115        // support long doubles. This test can do 1 << 64, which would fail116        // in these systems.117        NormalFloat two_to_exp = NormalFloat(static_cast<T>(1.L));118        two_to_exp = two_to_exp.mul2(exp);119 120        ASSERT_FP_EQ(func(x, exp), x * two_to_exp);121        ASSERT_FP_EQ(func(x, -exp), x / two_to_exp);122      }123    }124 125    // Normal which trigger mantissa overflow.126    T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,127                      StorageType(2) * NormalFloat::ONE - StorageType(1));128    ASSERT_FP_EQ(func(x, -1), x / 2);129    ASSERT_FP_EQ(func(-x, -1), -x / 2);130 131    // Start with a normal number high exponent but pass a very low number for132    // exp. The result should be a subnormal number.133    x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);134    int exp = -FPBits::MAX_BIASED_EXPONENT - 5;135    T result = func(x, exp);136    FPBits result_bits(result);137    ASSERT_FALSE(result_bits.is_zero());138    // Verify that the result is indeed subnormal.139    ASSERT_EQ(result_bits.get_biased_exponent(), uint16_t(0));140    // But if the exp is so less that normalization leads to zero, then141    // the result should be zero.142    result = func(x, -FPBits::MAX_BIASED_EXPONENT - FPBits::FRACTION_LEN - 5);143    ASSERT_TRUE(FPBits(result).is_zero());144 145    // Start with a subnormal number but pass a very high number for exponent.146    // The result should not be infinity.147    x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);148    exp = FPBits::MAX_BIASED_EXPONENT + 5;149    ASSERT_FALSE(FPBits(func(x, exp)).is_inf());150    // But if the exp is large enough to oversome than the normalization shift,151    // then it should result in infinity.152    exp = FPBits::MAX_BIASED_EXPONENT + 15;153    ASSERT_FP_EQ(func(x, exp), inf);154  }155};156 157#define LIST_LDEXP_TESTS(T, func)                                              \158  using LlvmLibcLdExpTest = LdExpTestTemplate<T>;                              \159  TEST_F(LlvmLibcLdExpTest, SpecialNumbers) { testSpecialNumbers(&func); }     \160  TEST_F(LlvmLibcLdExpTest, PowersOfTwo) { testPowersOfTwo(&func); }           \161  TEST_F(LlvmLibcLdExpTest, OverFlow) { testOverflow(&func); }                 \162  TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnNormal) {                         \163    testUnderflowToZeroOnNormal(&func);                                        \164  }                                                                            \165  TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnSubnormal) {                      \166    testUnderflowToZeroOnSubnormal(&func);                                     \167  }                                                                            \168  TEST_F(LlvmLibcLdExpTest, NormalOperation) { testNormalOperation(&func); }169 170#endif // LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H171