Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/include/llvm-libc-types/float128.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ typedef __float128 float128;
#elif (LDBL_MANT_DIG == 113)
#define LIBC_TYPES_HAS_FLOAT128
typedef long double float128;
#else
#include "src/__support/FPUtil/float128.h"
typedef LIBC_NAMESPACE::fputil::Float128 float128;
#endif

#endif // LLVM_LIBC_TYPES_FLOAT128_H
6 changes: 5 additions & 1 deletion libc/src/__support/FPUtil/FPBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "src/__support/math_extras.h" // mask_trailing_ones
#include "src/__support/sign.h" // Sign
#include "src/__support/uint128.h"
#include "src/__support/FPUtil/float128.h" //include float128.h

namespace LIBC_NAMESPACE_DECL {
namespace fputil {
Expand Down Expand Up @@ -809,6 +810,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
else if constexpr (cpp::is_same_v<UnqualT, float128>)
return FPType::IEEE754_Binary128;
#endif
else if constexpr (cpp::is_same_v<UnqualT, LIBC_NAMESPACE::fputil::Float128>)
return FPType::IEEE754_Binary128;
else if constexpr (cpp::is_same_v<UnqualT, bfloat16>)
return FPType::BFloat16;
else
Expand All @@ -825,7 +828,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
// It derives its functionality to FPRepImpl above.
template <typename T>
struct FPBits final : public internal::FPRepImpl<get_fp_type<T>(), FPBits<T>> {
static_assert(cpp::is_floating_point_v<T>,
static_assert(cpp::is_floating_point_v<T>
||cpp::is_same_v<T, LIBC_NAMESPACE::fputil::Float128>,
"FPBits instantiated with invalid type.");
using UP = internal::FPRepImpl<get_fp_type<T>(), FPBits<T>>;
using StorageType = typename UP::StorageType;
Expand Down
37 changes: 37 additions & 0 deletions libc/src/__support/FPUtil/float128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- Float128 software wrapper ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines a minimal software-backed Float128 wrapper type used when
// the host compiler does not provide a native 128-bit floating-point type.
// The wrapper currently only stores the raw 128-bit representation.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H

#include "src/__support/uint128.h"

namespace LIBC_NAMESPACE_DECL {
namespace fputil {

class Float128 {
private:
UInt128 bits_ = 0;

public:
constexpr Float128() = default;

constexpr explicit Float128(UInt128 value) : bits_(value) {}

constexpr UInt128 get_bits() const { return bits_; }
};
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H
6 changes: 6 additions & 0 deletions libc/test/src/__support/FPUtil/fpbits_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,9 @@ TEST(LlvmLibcFPBitsTest, Float128Type) {
EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
}
#endif // LIBC_TYPES_HAS_FLOAT128

TEST(LlvmLibcFPBitsTest, Float128WrapperInstantiation) {
LIBC_NAMESPACE::fputil::Float128 x;
LIBC_NAMESPACE::fputil::FPBits<LIBC_NAMESPACE::fputil::Float128> bits(x);
EXPECT_TRUE(bits.is_zero());
}