Skip to content

Commit

Permalink
Incorporate cast simplifications from #880
Browse files Browse the repository at this point in the history
Add tests and make sure everything is covered, and fix resulting errors.
  • Loading branch information
jzmaddock committed Oct 17, 2023
1 parent e3e79b5 commit c6f4a88
Show file tree
Hide file tree
Showing 5 changed files with 5,233 additions and 71 deletions.
56 changes: 0 additions & 56 deletions include/boost/math/cstdfloat/cstdfloat_cmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,62 +918,6 @@ namespace std
using boost::math::cstdfloat::detail::isunordered;
// end more functions

//
// Very basic iostream operator:
//
inline std::ostream& operator << (std::ostream& os, __float128 m_value)
{
std::streamsize digits = os.precision();
std::ios_base::fmtflags f = os.flags();
std::string s;

char buf[100];
std::unique_ptr<char[]> buf2;
std::string format = "%";
if (f & std::ios_base::showpos)
format += "+";
if (f & std::ios_base::showpoint)
format += "#";
format += ".*";
if (digits == 0)
digits = 36;
format += "Q";
if (f & std::ios_base::scientific)
format += "e";
else if (f & std::ios_base::fixed)
format += "f";
else
format += "g";

int v = quadmath_snprintf(buf, 100, format.c_str(), digits, m_value);

if ((v < 0) || (v >= 99))
{
int v_max = v;
buf2.reset(new char[v + 3]);
v = quadmath_snprintf(&buf2[0], v_max + 3, format.c_str(), digits, m_value);
if (v >= v_max + 3)
{
BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
}
s = &buf2[0];
}
else
s = buf;
std::streamsize ss = os.width();
if (ss > static_cast<std::streamsize>(s.size()))
{
char fill = os.fill();
if ((os.flags() & std::ios_base::left) == std::ios_base::left)
s.append(static_cast<std::string::size_type>(ss - s.size()), fill);
else
s.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(ss - s.size()), fill);
}

return os << s;
}


} // namespace std

// We will now remove the preprocessor symbols representing quadruple-precision <cmath>
Expand Down
33 changes: 18 additions & 15 deletions include/boost/math/cstdfloat/cstdfloat_iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
#include <boost/math/tools/nothrow.hpp>
#include <boost/math/tools/throw_exception.hpp>

// #if (0)
#if defined(__GNUC__)
#if defined(__GNUC__) && !defined(BOOST_MATH_TEST_IO_AS_INTEL_QUAD)

// Forward declarations of quadruple-precision string functions.
extern "C" int quadmath_snprintf(char *str, size_t size, const char *format, ...) BOOST_MATH_NOTHROW;
Expand Down Expand Up @@ -96,7 +95,7 @@
// So we have to use dynamic memory allocation for the output
// string buffer.

char* my_buffer2 = static_cast<char*>(0U);
char* my_buffer2 = nullptr;

#ifndef BOOST_NO_EXCEPTIONS
try
Expand Down Expand Up @@ -160,8 +159,7 @@
}
}

// #elif defined(__GNUC__)
#elif defined(__INTEL_COMPILER)
#elif defined(__INTEL_COMPILER) || defined(BOOST_MATH_TEST_IO_AS_INTEL_QUAD)

// The section for I/O stream support for the ICC compiler is particularly
// long, because these functions must be painstakingly synthesized from
Expand All @@ -172,6 +170,7 @@
// used in Boost.Multiprecision by John Maddock and Christopher Kormanyos.
// This methodology has been slightly modified here for boost::float128_t.


#include <cstring>
#include <cctype>

Expand Down Expand Up @@ -266,7 +265,7 @@
{
// Pad out the end with zero's if we need to.

int chars = static_cast<int>(str.size());
std::ptrdiff_t chars = static_cast<std::ptrdiff_t>(str.size());
chars = digits - chars;

if(scientific)
Expand Down Expand Up @@ -507,6 +506,8 @@
eval_subtract(t, digit);
eval_multiply(t, ten);
}
if (result.size() == 0)
result = "0";

// Possibly round the result.
if(digits >= 0)
Expand All @@ -522,11 +523,13 @@
if((static_cast<int>(*result.rbegin() - '0') & 1) != 0)
{
round_string_up_at(result, static_cast<int>(result.size() - 1U), expon);
if (digits == 0) digits = 1;
}
}
else if(cdigit >= 5)
{
round_string_up_at(result, static_cast<int>(result.size() - 1), expon);
round_string_up_at(result, static_cast<int>(result.size() - 1u), expon);
if (digits == 0) digits = 1;
}
}
}
Expand Down Expand Up @@ -569,7 +572,7 @@
{
value = 0;

if((p == static_cast<const char*>(0U)) || (*p == static_cast<char>(0)))
if((p == nullptr) || (*p == '\0'))
{
return false;
}
Expand All @@ -584,11 +587,11 @@

constexpr int max_digits = std::numeric_limits<float_type>::max_digits10 + 1;

if(*p == static_cast<char>('+'))
if(*p == '+')
{
++p;
}
else if(*p == static_cast<char>('-'))
else if(*p == '-')
{
is_neg = true;
++p;
Expand Down Expand Up @@ -632,7 +635,7 @@
++digits_seen;
}

if(*p == static_cast<char>('.'))
if(*p == '.')
{
// Grab everything after the point, stop when we've seen
// enough digits, even if there are actually more available.
Expand All @@ -659,15 +662,15 @@
}

// Parse the exponent.
if((*p == static_cast<char>('e')) || (*p == static_cast<char>('E')))
if((*p == 'e') || (*p == 'E'))
{
++p;

if(*p == static_cast<char>('+'))
if(*p == '+')
{
++p;
}
else if(*p == static_cast<char>('-'))
else if(*p == '-')
{
is_neg_expon = true;
++p;
Expand Down Expand Up @@ -718,7 +721,7 @@
value = -value;
}

return (*p == static_cast<char>(0));
return (*p == '\0');
}
} } } } // boost::math::cstdfloat::detail

Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,8 @@ test-suite new_floats :
[ compile compile_test/float32.cpp ]
[ compile compile_test/float64.cpp ]
[ compile compile_test/float128.cpp ]
[ run test_float_io.cpp : : : [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : <define>BOOST_MATH_TEST_FLOAT128 <linkflags>"-Bstatic -lquadmath -Bdynamic" ] ]
[ run test_float_io.cpp : : : <define>BOOST_MATH_TEST_IO_AS_INTEL_QUAD=1 [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : <define>BOOST_MATH_TEST_FLOAT128 <linkflags>"-Bstatic -lquadmath -Bdynamic" ] : test_float_io_quad ]
;

test-suite mp :
Expand Down
Loading

0 comments on commit c6f4a88

Please sign in to comment.