1 //===------ Pretty print function for FPBits --------------------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_FPBITS_STR_H
10 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FPBITS_STR_H
12 #include "src/__support/CPP/string.h"
13 #include "src/__support/CPP/type_traits.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "src/__support/integer_to_string.h"
16 #include "src/__support/macros/attributes.h"
17 #include "src/__support/macros/config.h"
19 namespace LIBC_NAMESPACE_DECL
{
23 // Format T as uppercase hexadecimal number with leading zeros.
25 using ZeroPaddedHexFmt
= IntegerToString
<
26 T
, typename
radix::Hex::WithWidth
<(sizeof(T
) * 2)>::WithPrefix::Uppercase
>;
28 } // namespace details
30 // Converts the bits to a string in the following format:
31 // "0x<NNN...N> = S: N, E: 0xNNNN, M:0xNNN...N"
32 // 1. N is a hexadecimal digit.
33 // 2. The hexadecimal number on the LHS is the raw numerical representation
35 // 3. The exponent is always 16 bits wide irrespective of the type of the
37 template <typename T
> LIBC_INLINE
cpp::string
str(fputil::FPBits
<T
> x
) {
38 using StorageType
= typename
fputil::FPBits
<T
>::StorageType
;
43 return x
.is_neg() ? "(-Infinity)" : "(+Infinity)";
45 const auto sign_char
= [](Sign sign
) -> char {
46 return sign
.is_neg() ? '1' : '0';
51 const details::ZeroPaddedHexFmt
<StorageType
> bits(x
.uintval());
55 s
+= sign_char(x
.sign());
58 const details::ZeroPaddedHexFmt
<uint16_t> exponent(x
.get_biased_exponent());
61 if constexpr (fputil::get_fp_type
<T
>() == fputil::FPType::X86_Binary80
) {
63 s
+= sign_char(x
.get_implicit_bit() ? Sign::NEG
: Sign::POS
);
67 const details::ZeroPaddedHexFmt
<StorageType
> mantissa(x
.get_mantissa());
74 } // namespace LIBC_NAMESPACE_DECL
76 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FPBITS_STR_H