llvm/libc/test/src/__support/FPUtil/fpbits_test.cpp

//===-- Unittests for the FPBits class ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/fpbits_str.h"
#include "src/__support/integer_literals.h"
#include "src/__support/macros/properties/types.h"
#include "src/__support/sign.h" // Sign
#include "test/UnitTest/Test.h"

FPBits;
FPType;
FPRep;

operator""_u16;
operator""_u32;
operator""_u64;
operator""_u128;

TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary16) {}

TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary32) {}

TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary64) {}

TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary128) {}

TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80) {}

TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80_IsNan) {}

enum class FP {};

constexpr FP all_fp_values[] =;

constexpr Sign all_signs[] =;

FPTypes;

template <typename T> constexpr auto make(Sign sign, FP fp) {}

// Tests all properties for all types of float.
TYPED_TEST(LlvmLibcFPBitsTest, Properties, FPTypes) {}

#define ASSERT_SAME_REP(A, B)

TYPED_TEST(LlvmLibcFPBitsTest, NextTowardInf, FPTypes) {}

TEST(LlvmLibcFPBitsTest, FloatType) {}

TEST(LlvmLibcFPBitsTest, DoubleType) {}

#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {}
#else
TEST(LlvmLibcFPBitsTest, LongDoubleType) {
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
  return; // The tests for the "double" type cover for this case.
#else
  using LongDoubleBits = FPBits<long double>;

  EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
               "(+Infinity)");
  EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
               "(-Infinity)");
  EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
               "(NaN)");

  LongDoubleBits zero(0.0l);
  EXPECT_TRUE(zero.is_pos());
  EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
  EXPECT_EQ(zero.get_mantissa(), 0_u128);
  EXPECT_EQ(zero.uintval(), 0_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
               "0x00000000000000000000000000000000 = "
               "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");

  LongDoubleBits negzero(-0.0l);
  EXPECT_TRUE(negzero.is_neg());
  EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
  EXPECT_EQ(negzero.get_mantissa(), 0_u128);
  EXPECT_EQ(negzero.uintval(), 0x80000000'00000000'00000000'00000000_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
               "0x80000000000000000000000000000000 = "
               "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");

  LongDoubleBits one(1.0l);
  EXPECT_TRUE(one.is_pos());
  EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
  EXPECT_EQ(one.get_mantissa(), 0_u128);
  EXPECT_EQ(one.uintval(), 0x3FFF0000'00000000'00000000'00000000_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
               "0x3FFF0000000000000000000000000000 = "
               "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");

  LongDoubleBits negone(-1.0l);
  EXPECT_TRUE(negone.is_neg());
  EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
  EXPECT_EQ(negone.get_mantissa(), 0_u128);
  EXPECT_EQ(negone.uintval(), 0xBFFF0000'00000000'00000000'00000000_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
               "0xBFFF0000000000000000000000000000 = "
               "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");

  LongDoubleBits num(1.125l);
  EXPECT_TRUE(num.is_pos());
  EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
  EXPECT_EQ(num.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
  EXPECT_EQ(num.uintval(), 0x3FFF2000'00000000'00000000'00000000_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
               "0x3FFF2000000000000000000000000000 = "
               "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");

  LongDoubleBits negnum(-1.125l);
  EXPECT_TRUE(negnum.is_neg());
  EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
  EXPECT_EQ(negnum.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
  EXPECT_EQ(negnum.uintval(), 0xBFFF2000'00000000'00000000'00000000_u128);
  EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
               "0xBFFF2000000000000000000000000000 = "
               "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");

  LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
#endif
}
#endif

#if defined(LIBC_TYPES_HAS_FLOAT128)
TEST(LlvmLibcFPBitsTest, Float128Type) {}
#endif // LIBC_TYPES_HAS_FLOAT128