1818
1919namespace __llvm_libc {
2020
21- namespace details {
22-
23- // Format T as uppercase hexadecimal number with leading zeros.
24- template <typename T>
25- using ZeroPaddedHexFmt = IntegerToString<
26- T, typename radix::Hex::WithWidth<(sizeof (T) * 2 )>::WithPrefix::Uppercase>;
27-
28- } // namespace details
29-
3021// Converts the bits to a string in the following format:
3122// "0x<NNN...N> = S: N, E: 0xNNNN, M:0xNNN...N"
3223// 1. N is a hexadecimal digit.
@@ -42,31 +33,36 @@ template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
4233 if (x.is_inf ())
4334 return x.get_sign () ? " (-Infinity)" : " (+Infinity)" ;
4435
45- const auto sign_char = [](bool sign) -> char { return sign ? ' 1' : ' 0' ; };
46-
47- cpp::string s;
36+ auto zerofill = [](char *arr, size_t n) {
37+ for (size_t i = 0 ; i < n; ++i)
38+ arr[i] = ' 0' ;
39+ };
4840
49- const details::ZeroPaddedHexFmt<UIntType> bits (x.bits );
50- s += bits.view ();
41+ cpp::string s (" 0x" );
42+ char bitsbuf[IntegerToString::hex_bufsize<UIntType>()];
43+ zerofill (bitsbuf, sizeof (bitsbuf));
44+ IntegerToString::hex (x.bits , bitsbuf, false );
45+ s += cpp::string (bitsbuf, sizeof (bitsbuf));
5146
52- s += " = (S: " ;
53- s += sign_char ( x.get_sign ());
47+ s += " = (" ;
48+ s += cpp::string ( " S: " ) + ( x.get_sign () ? " 1 " : " 0 " );
5449
55- s += " , E: " ;
56- const details::ZeroPaddedHexFmt<uint16_t > exponent (x.get_unbiased_exponent ());
57- s += exponent.view ();
50+ char expbuf[IntegerToString::hex_bufsize<uint16_t >()];
51+ zerofill (expbuf, sizeof (expbuf));
52+ IntegerToString::hex (x.get_unbiased_exponent (), expbuf, false );
53+ s += cpp::string (" , E: 0x" ) + cpp::string (expbuf, sizeof (expbuf));
5854
5955 if constexpr (cpp::is_same_v<T, long double > &&
6056 fputil::FloatProperties<long double >::MANTISSA_WIDTH == 63 ) {
61- s += " , I: " ;
62- s += sign_char (x.get_implicit_bit ());
57+ s += cpp::string (" , I: " ) + (x.get_implicit_bit () ? " 1" : " 0" );
6358 }
6459
65- s += " , M: " ;
66- const details::ZeroPaddedHexFmt<UIntType> mantissa (x.get_mantissa ());
67- s += mantissa.view ();
60+ char mantbuf[IntegerToString::hex_bufsize<UIntType>()] = {' 0' };
61+ zerofill (mantbuf, sizeof (mantbuf));
62+ IntegerToString::hex (x.get_mantissa (), mantbuf, false );
63+ s += cpp::string (" , M: 0x" ) + cpp::string (mantbuf, sizeof (mantbuf));
6864
69- s += ' ) ' ;
65+ s += " ) " ;
7066 return s;
7167}
7268
0 commit comments