Skip to content
Snippets Groups Projects
Unverified Commit ee9f0060 authored by Jonathan Wakely's avatar Jonathan Wakely Committed by Jonathan Wakely
Browse files

libstdc++: Preserve signbit of nan when converting float to double [PR113578]


LWG 117 specifies that inserting a float into an ostream should cast it
to double, because there's no std::num_put::put member that takes a
float. However, on RISC-V converting a NaN float to double loses the
sign, which means that negative NaN floats are printed as positive.

This has been reported as LWG 4101 and there is good support for fixing
the standard to preserve the sign bit when printing negative NaN values.

This change uses copysign((double)f, (double)std::bit_cast<int>(f)) to
get a double that preserves the sign. The bit_cast gives us an integer
with the same signbit, and casting that to the target type preserves
the signbit. We don't care about the value, as copysign only uses the
signbit.

The inserters for extended floating-point types need the same treatment,
so add a new _S_cast_flt helper to do the signbit-preserving conversion
generically.

So far only RISC-V has been confirmed to need this treatment, but we
might need to extend it to other targets later.

libstdc++-v3/ChangeLog:

	PR libstdc++/113578
	* include/std/ostream (_S_cast_flt): New static member function
	to restore signbit after casting to double or long double.
	(operator<<(float), operator<<(_Float16), operator<<(_Float32))
	(operator<<(_Float64), operator(_Float128))
	(operator<<(__bfloat16_t)): Use _S_cast_flt.
	testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc:
	New test.

Co-authored-by: default avatarAndrew Waterman <andrew@sifive.com>
parent 5ad6ff2b
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment