Commit graph

7977 commits

Author SHA1 Message Date
Jonathan Wakely
f487469181 libstdc++: Remove unnecessary variant member in std::expected
Hui Xie pointed out that we don't need a dummy member in the union,
because all constructors always initialize either _M_val or _M_unex.

We still need the _M_void member of the expected<void, E>
specialization, because the constructor has to initialize something when
not using the _M_unex member.

libstdc++-v3/ChangeLog:

	* include/std/expected (expected::_M_invalid): Remove.
2022-11-02 12:54:16 +00:00
Jonathan Wakely
cf35818a39 libstdc++: Ignore -Wignored-qualifiers warning in <variant>
The warning is wrong here, the qualifier serves a purpose and is not
ignored (c.f. PR c++/107492).

libstdc++-v3/ChangeLog:

	* include/std/variant (__variant::_Multi_array::__untag_result):
	Use pragma to suppress warning.
2022-11-02 12:54:16 +00:00
Jakub Jelinek
81f98afa22 libstdc++: std::from_chars std::{,b}float16_t support
The following patch adds std::from_chars support, similarly to the
previous std::to_chars patch through APIs that use float instead of
the 16-bit floating point formats as container.
The patch uses the fast_float library and doesn't need any changes
to it, like the previous patch it introduces wrapper classes around
float that represent the float holding float16_t or bfloat16_t value,
and specializes binary_format etc. from fast_float for these classes.

The new test verifies exhaustively to_chars and from_chars afterward
results in the original value (except for nans) in all the fmt cases.

2022-11-01  Jakub Jelinek  <jakub@redhat.com>

	* include/std/charconv (__from_chars_float16_t,
	__from_chars_bfloat16_t): Declare.
	(from_chars): Add _Float16 and __gnu_cxx::__bfloat16_t overloads.
	* config/abi/pre/gnu.ver (GLIBCXX_3.4.31): Export
	_ZSt22__from_chars_float16_tPKcS0_RfSt12chars_format and
	_ZSt23__from_chars_bfloat16_tPKcS0_RfSt12chars_format.
	* src/c++17/floating_from_chars.cc
	(fast_float::floating_type_float16_t,
	fast_float::floating_type_bfloat16_t): New classes.
	(fast_float::binary_format<floating_type_float16_t>,
	fast_float::binary_format<floating_type_bfloat16_t>): New
	specializations.
	(fast_float::to_float<floating_type_float16_t>,
	fast_float::to_float<floating_type_bfloat16_t>,
	fast_float::to_extended<floating_type_float16_t>,
	fast_float::to_extended<floating_type_bfloat16_t>): Likewise.
	(fast_float::from_chars_16): New template function.
	(__floating_from_chars_hex): Allow instantiation with
	fast_float::floating_type_{,b}float16_t.
	(from_chars): Formatting fixes for float/double/long double overloads.
	(__from_chars_float16_t, __from_chars_bfloat16_t): New functions.
	* testsuite/20_util/to_chars/float16_c++23.cc: New test.
2022-11-01 13:41:57 +01:00
Jakub Jelinek
0ae26533b3 libstdc++: std::to_chars std::{,b}float16_t support
The following patch on top of
https://gcc.gnu.org/pipermail/libstdc++/2022-October/054849.html
adds std::{,b}float16_t support for std::to_chars.
When precision is specified (or for std::bfloat16_t for hex mode even if not),
I believe we can just use the std::to_chars float (when float is mode
compatible with std::float32_t) overloads, both formats are proper subsets
of std::float32_t.
Unfortunately when precision is not specified and we are supposed to emit
shortest string, the std::{,b}float16_t strings are usually much shorter.
E.g. 1.e7p-14f16 shortest fixed representation is
0.0001161 and shortest scientific representation is
1.161e-04 while 1.e7p-14f32 (same number promoted to std::float32_t)
0.00011610985 and
1.1610985e-04.
Similarly for 1.38p-112bf16,
0.000000000000000000000000000000000235
2.35e-34 vs. 1.38p-112f32
0.00000000000000000000000000000000023472271
2.3472271e-34
For std::float16_t there are differences even in the shortest hex, say:
0.01p-14 vs. 1p-22
but only for denormal std::float16_t values (where all std::float16_t
denormals converted to std::float32_t are normal), __FLT16_MIN__ and
everything larger in absolute value than that is the same.  Unless
that is a bug and we should try to discover shorter representations
even for denormals...
std::bfloat16_t has the same exponent range as std::float32_t, so all
std::bfloat16_t denormals are also std::float32_t denormals and thus
the shortest hex representations are the same.
As documented, ryu can handle arbitrary IEEE like floating point formats
(probably not wider than IEEE quad) using the generic_128 handling, but
ryu is hidden in libstdc++.so.  As only few architectures support
std::float16_t right now and some of them have special ISA requirements
for those (e.g. on i?86 one needs -msse2) and std::bfloat16_t is right
now supported only on x86 (again with -msse2), perhaps with aarch64/arm
coming next if ARM is interested, but I think it is possible that more
will be added later, instead of exporting APIs from the library to handle
directly the std::{,b}float16_t overloads this patch instead exports
functions which take a float which is a superset of those and expects
the inline overloads to promote the 16-bit formats to 32-bit, then inside
of the library it ensures they are printed right.
With the added [[gnu::cold]] attribute because I think most users
will primarily use these formats as storage formats and perform arithmetics
in the excess precision for them and print also as std::float32_t the
added support doesn't seem to be too large, on x86_64:
readelf -Ws libstdc++.so.6.0.31 | grep float16_t
   912: 00000000000ae824   950 FUNC    GLOBAL DEFAULT   13 _ZSt21__to_chars_bfloat16_tPcS_fSt12chars_format@@GLIBCXX_3.4.31
  5767: 00000000000ae4a1   899 FUNC    GLOBAL DEFAULT   13 _ZSt20__to_chars_float16_tPcS_fSt12chars_format@@GLIBCXX_3.4.31
   842: 000000000016d430   106 FUNC    LOCAL  DEFAULT   13 _ZN12_GLOBAL__N_113get_ieee_reprINS_23floating_type_float16_tEEENS_6ieee_tIT_EES3_
   865: 0000000000170980  1613 FUNC    LOCAL  DEFAULT   13
+_ZSt23__floating_to_chars_hexIN12_GLOBAL__N_123floating_type_float16_tEESt15to_chars_resultPcS3_T_St8optionalIiE.constprop.0.isra.0
  7205: 00000000000ae824   950 FUNC    GLOBAL DEFAULT   13 _ZSt21__to_chars_bfloat16_tPcS_fSt12chars_format
  7985: 00000000000ae4a1   899 FUNC    GLOBAL DEFAULT   13 _ZSt20__to_chars_float16_tPcS_fSt12chars_format
so 3568 code bytes together or so.

Tested with the attached test (which doesn't prove the shortest
representation, just prints std::{,b}float16_t and std::float32_t
shortest strings side by side, then tries to verify it can be
emitted even into the exact sized range and can't be into range
one smaller than that and tries to read what is printed
back using from_chars float32_t overload (so there could be
double rounding, but apparently there is none for the shortest strings).
The only differences printed are for NaNs, where sNaNs are canonicalized
to canonical qNaNs and as to_chars doesn't print NaN mantissa, even qNaNs
other than the canonical one are read back just as the canonical NaN.

Also attaching what Patrick wrote to generate the pow10_adjustment_tab,
for std::float16_t only 1.0, 10.0, 100.0, 1000.0 and 10000.0 are powers
of 10 in the range because __FLT16_MAX__ is 65504.0, and all of the above
are exactly representable in std::float16_t, so we want to use 0 in
pow10_adjustment_tab.

2022-11-01  Jakub Jelinek  <jakub@redhat.com>

	* include/std/charconv (__to_chars_float16_t, __to_chars_bfloat16_t):
	Declare.
	(to_chars): Add _Float16 and __gnu_cxx::__bfloat16_t overloads.
	* config/abi/pre/gnu.ver (GLIBCXX_3.4.31): Export
	_ZSt20__to_chars_float16_tPcS_fSt12chars_format and
	_ZSt21__to_chars_bfloat16_tPcS_fSt12chars_format.
	* src/c++17/floating_to_chars.cc (floating_type_float16_t,
	floating_type_bfloat16_t): New types.
	(floating_type_traits<floating_type_float16_t>,
	floating_type_traits<floating_type_bfloat16_t>,
	get_ieee_repr<floating_type_float16_t>,
	get_ieee_repr<floating_type_bfloat16_t>,
	__handle_special_value<floating_type_float16_t>,
	__handle_special_value<floating_type_bfloat16_t>): New specializations.
	(floating_to_shortest_scientific): Handle floating_type_float16_t
	and floating_type_bfloat16_t like IEEE quad.
	(__floating_to_chars_shortest): For floating_type_bfloat16_t call
	__floating_to_chars_hex<float> rather than
	__floating_to_chars_hex<floating_type_bfloat16_t> to avoid
	instantiating the latter.
	(__to_chars_float16_t, __to_chars_bfloat16_t): New functions.
2022-11-01 13:40:05 +01:00
Jakub Jelinek
29ef50b6bc libstdc++-v3: Some std::*float*_t charconv and i/ostream overloads
The following patch adds the easy part of <charconv>, <istream> and
<ostream> changes for extended floats.
In particular, for the first one only overloads where the _Float* has
the same format as float/double/long double and for the latter two
everything but the _GLIBCXX_HAVE_FLOAT128_MATH case.
For charconv, I'm not really familiar with it, I'm pretty sure
we need new libstdc++.so.6 side implementation of from_chars for
{,b}float16_t and for to_chars not really sure but for unspecified precision
if it should emit minimum characters that to_chars then can unambiguously
parse, I think it is less than in the float case.  For float128_t
{to,from}_chars I think we even have it on the library side already, just
ifdefed for powerpc64le only.
For i/o stream operator<</>>, not sure what is better, if not providing
anything at all, or doing what we in the end do if user doesn't override
the virtual functions, or use {to,from}_chars under the hood, something
else?
Besides this, the patch adds some further missed
// { dg-options "-std=gnu++2b" }
spots, I've also noticed I got the formatting wrong in some testcases
by not using spaces around VERIFY conditions and elsewhere by having
space before ( for calls.
The testsuite coverage is limited, I've added test for from_chars because
it was easy to port, but not really sure what to do about to_chars, it has
for float/double huge testcases which would be excessive to repeat.
And for i/ostream not really sure what exactly is worth testing.

2022-11-01  Jakub Jelinek  <jakub@redhat.com>

	* include/std/charconv (from_chars, to_chars): Add _Float{32,64,128}
	overloads for cases where those types match {float,double,long double}.
	* include/std/istream (basic_istream::operator>>): Add
	_Float{16,32,64,128} and __gnu_cxx::__bfloat16_t overloads.
	* include/std/ostream (basic_ostream::operator<<): Add
	_Float{16,32,64,128} and __gnu_cxx::__bfloat16_t overloads.
	* testsuite/20_util/from_chars/8.cc: New test.
	* testsuite/26_numerics/headers/cmath/nextafter_c++23.cc (test):
	Formatting fixes.
	* testsuite/26_numerics/headers/cmath/functions_std_c++23.cc: Add
	dg-options "-std=gnu++2b".
	(test_functions, main): Formatting fixes.
	* testsuite/26_numerics/headers/cmath/c99_classification_macros_c++23.cc:
	Add dg-options "-std=gnu++2b".
2022-11-01 13:34:51 +01:00
Jakub Jelinek
db55f1dda2 libstdc++-v3: <complex> support for extended floating point types
The following patch adds <complex> support for extended floating point
types.
C++23 removes the float/double/long double specializations from the spec
and instead adds explicit(bool) specifier on the converting constructor.
The patch uses that for converting constructor of the base template as well
as the float/double/long double specializations's converting constructors
(e.g. so that it handles convertion construction also from complex of extended
floating point types).  Copy ctor was already defaulted as the spec now
requires.
The patch also adds partial specialization for the _Float{16,32,64,128}
and __gnu_cxx::__bfloat16_t types because the base template doesn't use
__complex__ but a pair of floating point values.
The g++.dg/cpp23/ testcase verifies explicit(bool) works correctly.

2022-10-31  Jakub Jelinek  <jakub@redhat.com>

gcc/testsuite/
	* g++.dg/cpp23/ext-floating12.C: New test.
libstdc++-v3/
	* include/std/complex (complex::complex converting ctor): For C++23
	use explicit specifier with constant expression.  Explicitly cast
	both parts to _Tp.
	(__complex_abs, __complex_arg, __complex_cos, __complex_cosh,
	__complex_exp, __complex_log, __complex_sin, __complex_sinh,
	__complex_sqrt, __complex_tan, __complex_tanh, __complex_pow): Add
	__complex__ _Float{16,32,64,128} and __complex__ decltype(0.0bf16)
	overloads.
	(complex<float>::complex converting ctor,
	complex<double>::complex converting ctor,
	complex<long double>::complex converting ctor): For C++23 implement
	as template with explicit specifier with constant expression
	and explicit casts.
	(__complex_type): New template.
	(complex): New partial specialization for types with extended floating
	point types.
	(__complex_acos, __complex_asin, __complex_atan, __complex_acosh,
	__complex_asinh, __complex_atanh): Add __complex__ _Float{16,32,64,128}
	and __complex__ decltype(0.0bf16) overloads.
	(__complex_proj): Likewise.  Add template for complex of extended
	floating point types.
	* include/bits/cpp_type_traits.h (__is_floating): Specialize for
	_Float{16,32,64,128} and __gnu_cxx::__bfloat16_t.
	* testsuite/26_numerics/complex/ext_c++23.cc: New test.
2022-10-31 20:15:27 +01:00
Jakub Jelinek
cbf56503d5 libstdc++: Small extended float support tweaks
The following patch
1) enables the std::float128_t overloads for x86 with glibc 2.26+
2) makes std::nextafter(std::float16_t, std::float16_t) and
   std::nextafter(std::bfloat16_t, std::bfloat16_t) constexpr
3) adds (small) testsuite coverage for that

2022-10-21  Jakub Jelinek  <jakub@redhat.com>

	* config/os/gnu-linux/os_defines.h (_GLIBCXX_HAVE_FLOAT128_MATH):
	Uncomment.
	* include/c_global/cmath (nextafter(_Float16, _Float16)): Make it constexpr.
	If std::__is_constant_evaluated() call __builtin_nextafterf16.
	(nextafter(__gnu_cxx::__bfloat16_t, __gnu_cxx::__bfloat16_t)): Similarly
	but call __builtin_nextafterf16b.
	* testsuite/26_numerics/headers/cmath/nextafter_c++23.cc (test): Add
	static assertions to test constexpr nextafter.
2022-10-31 16:49:04 +01:00
Jonathan Wakely
49237fe6ef libstdc++: Fix dangling reference in filesystem::path::filename()
The new -Wdangling-reference warning noticed this.

libstdc++-v3/ChangeLog:

	* include/bits/fs_path.h (path::filename()): Fix dangling
	reference.
2022-10-29 00:55:42 +01:00
Arsen Arsenović
b80f25a336 libstdc++: Make placeholders inline when inline variables are available
This slightly lowers the dependency of generated code on libstdc++.so.

libstdc++-v3/ChangeLog:

	* include/std/functional: Make placeholders inline, if possible.
2022-10-29 00:55:42 +01:00
Arsen Arsenović
655271e47f libstdc++: Don't use gstdint.h anymore
libstdc++-v3/ChangeLog:

	* configure.ac: Stop generating gstdint.h.
	* src/c++11/compatibility-atomic-c++0x.cc: Stop using gstdint.h.
	* Makefile.in: Regenerate.
	* aclocal.m4: Regenerate.
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* doc/Makefile.in: Regenerate.
	* include/Makefile.in: Regenerate.
	* libsupc++/Makefile.in: Regenerate.
	* po/Makefile.in: Regenerate.
	* python/Makefile.in: Regenerate.
	* src/Makefile.in: Regenerate.
	* src/c++11/Makefile.in: Regenerate.
	* src/c++17/Makefile.in: Regenerate.
	* src/c++20/Makefile.in: Regenerate.
	* src/c++98/Makefile.in: Regenerate.
	* src/filesystem/Makefile.in: Regenerate.
	* src/libbacktrace/Makefile.in: Regenerate.
	* testsuite/Makefile.in: Regenerate.
2022-10-29 00:55:42 +01:00
Jonathan Wakely
988dd22ec6 libstdc++: Fix allocator propagation in regex algorithms [PR107376]
The PR points out that we assume the match_results allocator is default
constuctible, which might not be true. We also have a related issue with
unwanted propagation from an object that might have an unequal
allocator.

Ideally we use the same allocator type for _State_info::_M_match_queue
but that would be an ABI change now. We should investigate if that can
be done without breaking anything, which might be possible because the
_Executor object is short-lived and never leaks out of the regex_match,
regex_search, and regex_replace algorithms. If we change the mangled
name for _Executor then there would be no ODR violations when mixing old
and new definitions. This commit does not attempt that.

libstdc++-v3/ChangeLog:

	PR libstdc++/107376
	* include/bits/regex_executor.h (_Executor::_Executor): Use same
	allocator for _M_cur_results and _M_results.
	* include/bits/regex_executor.tcc (_Executor::_M_main_dispatch):
	Prevent possibly incorrect allocator propagating to
	_M_cur_results.
	* testsuite/28_regex/algorithms/regex_match/107376.cc: New test.
2022-10-28 15:26:35 +01:00
Marek Polacek
d2249cd9ad c++: Implement -Wdangling-reference [PR106393]
This patch implements a new experimental warning (enabled by -Wall) to
detect references bound to temporaries whose lifetime has ended.  The
primary motivation is the Note in
<https://en.cppreference.com/w/cpp/algorithm/max>:

  Capturing the result of std::max by reference produces a dangling reference
  if one of the parameters is a temporary and that parameter is returned:

  int n = 1;
  const int& r = std::max(n-1, n+1); // r is dangling

That's because both temporaries for n-1 and n+1 are destroyed at the end
of the full expression.  With this warning enabled, you'll get:

g.C:3:12: warning: possibly dangling reference to a temporary [-Wdangling-reference]
    3 | const int& r = std::max(n-1, n+1);
      |            ^
g.C:3:24: note: the temporary was destroyed at the end of the full expression 'std::max<int>((n - 1), (n + 1))'
    3 | const int& r = std::max(n-1, n+1);
      |                ~~~~~~~~^~~~~~~~~~

The warning works by checking if a reference is initialized with a function
that returns a reference, and at least one parameter of the function is
a reference that is bound to a temporary.  It assumes that such a function
actually returns one of its arguments!  (I added code to check_return_expr
to suppress the warning when we've seen the definition of the function
and we can say that it can return a variable with static storage
duration.)

It warns when the function in question is a member function, but only if
the function is invoked on a temporary object, otherwise the warning
would emit loads of warnings for valid code like obj.emplace<T>({0}, 0).
It does detect the dangling reference in:

  struct S {
    const S& self () { return *this; }
  };
  const S& s = S().self();

It warns in member initializer lists as well:

  const int& f(const int& i) { return i; }
  struct S {
    const int &r;
    S() : r(f(10)) { }
  };

I've run the testsuite/bootstrap with the warning enabled by default.
There were just a few FAILs, all of which look like genuine bugs.
A bootstrap with the warning enabled by default passed as well.

When testing a previous version of the patch, there were many FAILs in
libstdc++'s 22_locale/; all of them because the warning triggered on

  const test_type& obj = std::use_facet<test_type>(std::locale());

but this code looks valid -- std::use_facet doesn't return a reference
to its parameter.  Therefore I added a #pragma and code to suppress the
warning.

	PR c++/106393

gcc/c-family/ChangeLog:

	* c.opt (Wdangling-reference): New.

gcc/cp/ChangeLog:

	* call.cc (expr_represents_temporary_p): New, factored out of...
	(conv_binds_ref_to_temporary): ...here.  Don't return false just
	because a ck_base is missing.  Use expr_represents_temporary_p.
	(do_warn_dangling_reference): New.
	(maybe_warn_dangling_reference): New.
	(extend_ref_init_temps): Call maybe_warn_dangling_reference.
	* cp-tree.h: Adjust comment.
	* typeck.cc (check_return_expr): Suppress -Wdangling-reference
	warnings.

gcc/ChangeLog:

	* doc/invoke.texi: Document -Wdangling-reference.

libstdc++-v3/ChangeLog:

	* include/bits/locale_classes.tcc: Add #pragma to disable
	-Wdangling-reference with std::use_facet.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp23/elision4.C: Use -Wdangling-reference, add dg-warning.
	* g++.dg/cpp23/elision7.C: Likewise.
	* g++.dg/warn/Wdangling-pointer-2.C: Use -Wno-dangling-reference.
	* g++.dg/warn/Wdangling-reference1.C: New test.
	* g++.dg/warn/Wdangling-reference2.C: New test.
	* g++.dg/warn/Wdangling-reference3.C: New test.
2022-10-26 15:13:04 -04:00
Jonathan Wakely
33de0ffcf0 libstdc++: Fix std::move_only_function for incomplete parameter types
The std::move_only_function::__param_t alias template attempts to
optimize argument passing for the invoker, by passing by rvalue
reference for types that are non-trivial or large. However, the
precondition for is_trivally_copyable makes it unsuitable for using
here, and can cause ODR violations. Just use is_scalar instead, and pass
all class types (even small, trivial ones) by value.

libstdc++-v3/ChangeLog:

	* include/bits/mofunc_impl.h (move_only_function::__param_t):
	Use __is_scalar instead of is_trivially_copyable.
	* testsuite/20_util/move_only_function/call.cc: Check parameters
	involving incomplete types.
2022-10-21 10:40:40 +01:00
Patrick Palka
7bf3a4d36a libstdc++: Redefine __from_chars_alnum_to_val's table
After the C++23 constexpr <charconv> patch r13-3313-g378a0f1840e694 we
have some modules testsuite regressions:

  FAIL: g++.dg/modules/xtreme-header-4_b.C -std=c++2b (test for excess errors)
  FAIL: g++.dg/modules/xtreme-header_b.C -std=c++2b (test for excess errors)

Like with PR105297, the cause seems to be the deduced type of __table
resolving ahead of time to a local class type, which trips up modules.
And unfortunately that PR's minimal workaround of making __tables's
initializer value dependent doesn't help in this case.

So this patch works around this by avoiding using a local class for the
table type.  And I suppose we should use a static data member to define
the table once for all dialects (including C++14) instead of having to
define it twice in C++23 mode (once as a static local variable and again
as a variable template for sake of constexpr evaluation).

libstdc++-v3/ChangeLog:

	* include/std/charconv (__detail::__from_chars_alnum_to_val_table):
	Redefine as a class template containing the members type, value and
	_S_make_table.  Don't use a local class as the table type.
	(__detail::__table): Remove.
	(__detail::__from_chars_alnum_to_val): Adjust after the above.
2022-10-20 10:30:31 -04:00
Patrick Palka
fb4db9bc1c libstdc++: Implement remaining P2474R2 changes to views::take/drop
libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__detail::__is_repeat_view): Define
	and later define a partial specialization.
	(views::__detail::__take_of_repeat_view): Declare and later define.
	(views::__detail::__drop_of_repeat_view): Likewise.
	(views::_Take::operator()): Return a repeat_view if the argument
	is a repeat_view as per P2474R2.
	(views::_Drop::operator()): Likewise.
	(repeat_view): Befriend __take/drop_of_repeat_view.
	* testsuite/std/ranges/repeat/1.cc (test04): New test.
2022-10-19 15:43:13 -04:00
Patrick Palka
a10d6b5eb9 libstdc++: Fix typo in stride_view's operator- [PR107313]
PR libstdc++/107313

libstdc++-v3/ChangeLog:

	* include/std/ranges (stride_view::_Iterator::operator-): Fix typo.
	* testsuite/std/ranges/adaptors/stride/1.cc (test03): New test.
2022-10-19 13:03:25 -04:00
Jakub Jelinek
ba281da28d libstdc++-v3: Implement {,b}float16_t nextafter and some fixes [PR106652]
The following patch implements nextafter for std::{,b}float16_t,
though right now only without constexpr support, and adds a testcase for it.
The testcase unfortunately relevealed I've screwed up testing of my last
patch.  I've tested earlier version of the patch with
--target_board=unix/-std=c++23
but didn't test the final version with that RUNTESTFLAGS, so missed
an invalid call to std::sph_neumann (too many arguments) in the test.
And, I've made a typo in the guard for numeric_limits (the reason
for the guard is I wanted to avoid defining a large macro that nothing
will then use because the std::{,b}float*_t types are C++23 only) and
so numeric_limits wasn't specialized for the types at all but
testsuite/18_support/headers/limits/synopsis_cxx23.cc test didn't
detect that.
In the nextafter implementation I'm calling __builtin_nextafterf
to get various required side-effects for nextafter from 0/-0, or from max
to inf or from min to largest subnormal to avoid needing to set errno
inline, or use inline asm specific for each processor to force math
evaluation barriers.  Dunno if
  #ifdef __INT16_TYPE__
    using __float16_int_type = __INT16_TYPE__;
  #else
    using __float16_int_type = short int;
  #endif
isn't too ugly, perhaps we could just blindly use short int and hope
or even assert it has the same size as _Float16 or __gnu_cxx::__bfloat16_t?
Only aarch64, arm, csky, gcn, x86, nvptx and riscv support these types
and all of them have 16-bit short (I think the only target with some
other short size is avr with certain command line switches where both
short and int are 8-bit, but such mode isn't compatible with C and C++
requirements).

2022-10-19  Jakub Jelinek  <jakub@redhat.com>

	PR c++/106652
	* include/std/limits: Fix a typo, 202202L -> 202002L.
	(numeric_limits::<_Float16>::radix, numeric_limits::<_Float32>::radix,
	numeric_limits::<_Float64>::radix, numeric_limits::<_Float128>::radix,
	numeric_limits::<__gnu_cxx::__bfloat16_t>::radix): Use __FLT_RADIX__
	macro instead of type specific macros.
	* include/c_global/cmath (nextafter(_Float16, _Float16)): New
	overload.
	(nextafter(__gnu_cxx::__bfloat16_t, __gnu_cxx::__bfloat16_t)):
	Likewise.
	* testsuite/26_numerics/headers/cmath/functions_std_c++23.cc
	(test_functions): Uncomment nextafter test.  Fix up sph_neumann call.
	* testsuite/26_numerics/headers/cmath/nextafter_c++23.cc: New test.
2022-10-19 11:26:17 +02:00
Patrick Palka
c70b44f2c1 libstdc++: Implement ranges::stride_view from P1899R3
libstdc++-v3/ChangeLog:

	* include/std/ranges (stride_view): Define.
	(stride_view::_Iterator): Define.
	(views::__detail::__can_stride_view): Define.
	(views::_Stride, views::stride): Define.
	* testsuite/std/ranges/adaptors/stride/1.cc: New test.
2022-10-18 10:51:52 -04:00
Jakub Jelinek
a23225fb4f libstdc++: Partial library support for std::float{16,32,64,128}_t and std::bfloat16_t
The following patch is partial support for std::float{16,32,64,128}_t
and std::bfloat16_t in libstdc++.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1467r9.html
says that <ostream>, <istream>, <charconv> and <complex>
need changes toom, but that isn't implemented so far.
In <cmath> the only thing missing I'm aware of is
std::nextafter std::float16_t and std::bfloat16_t overloads (I think
we probably need to implement that out of line somewhere, or inline? - might
need inline asm barriers) and std::nexttoward overloads (those are
intentional, you said there is a LWG issue about that).
Also, this patch has the glibc 2.26+ std::float128_t support for platforms
where long double isn't IEEE quad format temporarily disabled
because it depends on
https://gcc.gnu.org/pipermail/gcc-patches/2022-October/603665.html
changes which aren't in yet.

The patch also doesn't include any testcases to cover the <type_traits>
changes, it isn't clear to me where to put that.

2022-10-18  Jakub Jelinek  <jakub@redhat.com>

	PR c++/106652
	* include/std/stdfloat: New file.
	* include/std/numbers (__glibcxx_numbers): Define and use it
	for __float128 explicit instantiations as well as
	_Float{16,32,64,128} and __gnu_cxx::__bfloat16_t.
	* include/std/atomic (atomic<_Float16>, atomic<_Float32>,
	atomic<_Float64>, atomic<_Float128>, atomic<__gnu_cxx::__bfloat16_t>):
	New explicit instantiations.
	* include/std/type_traits (__is_floating_point_helper<_Float16>,
	__is_floating_point_helper<_Float32>,
	__is_floating_point_helper<_Float64>,
	__is_floating_point_helper<_Float128>,
	__is_floating_point_helper<__gnu_cxx::__bfloat16_t>): Likewise.
	* include/std/limits (__glibcxx_concat3_, __glibcxx_concat3,
	__glibcxx_float_n): Define.
	(numeric_limits<_Float16>, numeric_limits<_Float32>,
	numeric_limits<_Float64>, numeric_limits<_Float128>,
	numeric_limits<__gnu_cxx::__bfloat16_t>): New explicit instantiations.
	* include/bits/std_abs.h (abs): New overloads for
	_Float{16,32,64,128} and __gnu_cxx::__bfloat16_t.
	* include/bits/c++config (_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128): Define
	if long double is IEEE quad.
	(__gnu_cxx::__bfloat16_t): New using.
	* include/c_global/cmath (acos, asin, atan, atan2, ceil, cos, cosh,
	exp, fabs, floor, fmod, frexp, ldexp, log, log10, modf, pow, sin,
	sinh, sqrt, tan, tanh, fpclassify, isfinite, isinf, isnan, isnormal,
	signbit, isgreater, isgreaterequal, isless, islessequal,
	islessgreater, isunordered, acosh, asinh, atanh, cbrt, copysign, erf,
	erfc, exp2, expm1, fdim, fma, fmax, fmin, hypot, ilogb, lgamma,
	llrint, llround, log1p, log2, logb, lrint, lround, nearbyint,
	nextafter, remainder, rint, round, scalbln, scalbn, tgamma, trunc,
	lerp): New overloads with _Float{16,32,64,128} or
	__gnu_cxx::__bfloat16_t types.
	* config/os/gnu-linux/os_defines.h (_GLIBCXX_HAVE_FLOAT128_MATH):
	Prepare for definition if glibc 2.26 and later implements *f128 APIs
	but comment out the actual definition for now.
	* include/ext/type_traits.h (__promote<_Float16>, __promote<_Float32>,
	__promote<_Float64>, __promote<_Float128>,
	__promote<__gnu_cxx::__bfloat16_t>): New specializations.
	* include/Makefile.am (std_headers): Add stdfloat.
	* include/Makefile.in: Regenerated.
	* include/precompiled/stdc++.h: Include stdfloat.
	* testsuite/18_support/headers/stdfloat/types_std.cc: New test.
	* testsuite/18_support/headers/limits/synopsis_cxx23.cc: New test.
	* testsuite/26_numerics/headers/cmath/c99_classification_macros_c++23.cc:
	New test.
	* testsuite/26_numerics/headers/cmath/functions_std_c++23.cc: New test.
	* testsuite/26_numerics/numbers/4.cc: New test.
	* testsuite/29_atomics/atomic_float/requirements_cxx23.cc: New test.
2022-10-18 11:37:13 +02:00
Jonathan Wakely
0f4815502d libstdc++: Fix value of __cpp_lib_constexpr_charconv
libstdc++-v3/ChangeLog:

	* include/std/charconv (__cpp_lib_constexpr_charconv): Define to
	correct value.
	* include/std/version (__cpp_lib_constexpr_charconv): Likewise.
	* testsuite/20_util/to_chars/constexpr.cc: Check correct value.
	* testsuite/20_util/to_chars/version.cc: Likewise.
2022-10-17 09:39:37 +01:00
Jonathan Wakely
378a0f1840 libstdc++: Implement constexpr std::to_chars for C++23 (P2291R3)
Some of the helper functions use static constexpr local variables, which
is not permitted in a core constant expression. Removing the 'static'
seems to have negligible performance effect for __to_chars and
__to_chars_16. For __from_chars_alnum_to_val removing the 'static'
causes a significant performance impact for base 36 conversions. Use a
consteval lambda instead.

libstdc++-v3/ChangeLog:

	* include/bits/charconv.h (__to_chars_10_impl): Add constexpr
	for C++23. Remove 'static' from array.
	* include/std/charconv (__cpp_lib_constexpr_charconv): Define.
	(__to_chars, __to_chars_16): Remove 'static' from array, add
	constexpr.
	(__to_chars_10, __to_chars_8, __to_chars_2, __to_chars_i)
	(to_chars, __raise_and_add, __from_chars_pow2_base)
	(__from_chars_alnum, from_chars): Add constexpr.
	(__from_chars_alnum_to_val): Avoid local static during constant
	evaluation. Add constexpr.
	* include/std/version (__cpp_lib_constexpr_charconv): Define.
	* testsuite/20_util/from_chars/constexpr.cc: New test.
	* testsuite/20_util/to_chars/constexpr.cc: New test.
	* testsuite/20_util/to_chars/version.cc: New test.
2022-10-15 21:20:47 +01:00
Jonathan Wakely
e24b430f1e libstdc++: Fix uses_allocator_construction args for cv pair (LWG 3677)
The _Std_pair concept uses in <bits/uses_allocator_args.h> handles const
qualified pairs, but not volatile qualified. That's because it just uses
__is_pair which is specialized for const pairs.

This removes the partial specialization __is_pair<const pair<T,U>>, so
that __is_pair is now only true for cv-unqualified pairs. Then _Std_pair
needs to explicitly use remove_cv_t for the argument to __is_pair.

The other use of __is_pair is in map::insert(Pair&&) which doesn't want
to handle volatile so should just use remove_const_t.

libstdc++-v3/ChangeLog:

	* include/bits/stl_map.h (map::insert(Pair&&)): Use
	remove_const_t on argument to __is_pair.
	* include/bits/stl_pair.h (__is_pair<const pair<T,U>>): Remove
	partial specialization.
	* include/bits/uses_allocator_args.h (_Std_pair): Use
	remove_cv_t as per LWG 3677.
	* testsuite/20_util/uses_allocator/lwg3677.cc: New test.
2022-10-15 21:17:48 +01:00
Jonathan Wakely
6e8ce5d651 libstdc++: Use markdown in Doxygen comment
This makes the comment easier to read in the source, without altering
the Doxygen output.

libstdc++-v3/ChangeLog:

	* include/std/iostream: Use markdown in Doxygen comment.
2022-10-14 10:09:52 +01:00
François Dumont
f77281b25c libstdc++: Add __gnu_debug::basic_string<>::compare overloads
Rather than adding those implementations we are adding a:
using _Base::compare;

so that any compare method not implemented at __gnu_debug::basic_string
level are injected from the base class.

Also review how __gnu_debug::basic_string is tested. Now require to define
_GLIBCXX_TEST_DEBUG_STRING when running 'make check-debug'.

libstdc++-v3/ChangeLog

	* include/debug/string: Add using _Base::compare.
	(__gnu_debug::basic_string<>::compare(const basic_string<>&)): Remove.
	(__gnu_debug::basic_string<>::compare(size_type, size_type, const basic_string<>&)):
	Remove.
	(__gnu_debug::basic_string<>::compare(size_type, size_type, const basic_string<>&,
	size_type, size_type)): Remove.
	* testsuite/util/testsuite_string.h [_GLIBCXX_TEST_DEBUG_STRING]: Include <debug/string>.
	* testsuite/21_strings/basic_string/operations/compare/char/1.cc: Include testsuite_string.h
	and use __gnu_test::string.
	* testsuite/21_strings/basic_string/operations/compare/char/13650.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/compare/char/2.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/rfind/char/1.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/rfind/char/2.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/rfind/char/3.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/compare/wchar_t/1.cc:
	Include testsuite_string.h
	and use __gnu_test::wstring.
	* testsuite/21_strings/basic_string/operations/compare/wchar_t/13650.cc: Likewise.
	* testsuite/21_strings/basic_string/operations/compare/wchar_t/2.cc: Likewise.
2022-10-12 21:50:21 +02:00
Patrick Palka
bfcd9f8453 libstdc++: Implement ranges::repeat_view from P2474R2
libstdc++-v3/ChangeLog:

	* include/std/ranges (repeat_view): Define.
	(repeat_view::_Iterator): Define.
	(views::__detail::__can_repeat_view): Define.
	(views::__detail::__can_bounded_repeat_view): Define.
	(views::_Repeat, views::repeat): Define.
	* testsuite/std/ranges/repeat/1.cc: New test.
2022-10-12 11:14:11 -04:00
Jonathan Wakely
637e3668fd libstdc++: Allow emergency EH alloc pool size to be tuned [PR68606]
Implement a long-standing request to support tuning the size of the
emergency buffer for allocating exceptions after malloc fails, or to
disable that buffer entirely.

It's now possible to disable the dynamic allocation of the buffer and
use a fixed-size static buffer, via --enable-libstdcxx-static-eh-pool.
This is a built-time choice that is baked into libstdc++ and so affects
all code linked against that build of libstdc++.

The size of the pool can be set by --with-libstdcxx-eh-pool-obj-count=N
which is measured in units of sizeof(void*) not bytes. A given exception
type such as std::system_error depends on the target, so giving a size
in bytes wouldn't be portable across 16/32/64-bit targets.

When libstdc++ is configured to use a dynamic buffer, the size of that
buffer can now be tuned at runtime by setting the GLIBCXX_TUNABLES
environment variable (c.f. PR libstdc++/88264). The number of exceptions
to reserve space for is controlled by the "glibcxx.eh_pool.obj_count"
and "glibcxx.eh_pool.obj_size" tunables. The pool will be sized to be
able to allocate obj_count exceptions of size obj_size*sizeof(void*) and
obj_count "dependent" exceptions rethrown by std::rethrow_exception.

With the ability to tune the buffer size, we can reduce the default pool
size on 32-bit and 16-bit targets. Most users never need to throw 1kB
exceptions in parallel from hundreds of threads after malloc is OOM. The
users who do need that can use the tunables to select larger sizes.

The old defaults can be chosen at runtime by setting GLIBCXX_TUNABLES
to:
64-bit: glibcxx.eh_pool.obj_count=64:glibcxx.eh_pool.obj_size=112
32-bit: glibcxx.eh_pool.obj_count=32:glibcxx.eh_pool.obj_size=104

Or approximated by configuring with:
64-bit: --with-libstdcxx-eh-pool-obj-count=252
32-bit: --with-libstdcxx-eh-pool-obj-count=94

libstdc++-v3/ChangeLog:

	PR libstdc++/68606
	* Makefile.in: Regenerate.
	* acinclude.m4 (GLIBCXX_EMERGENCY_EH_ALLOC): New macro.
	* configure: Regenerate.
	* configure.ac: Use GLIBCXX_EMERGENCY_EH_ALLOC.
	* crossconfig.m4: Check for secure_getenv.
	* doc/Makefile.in: Regenerate.
	* doc/xml/manual/configure.xml: Document new configure options.
	* doc/xml/manual/evolution.xml: Document addition of tunables.
	* doc/xml/manual/using_exceptions.xml: Document emergency
	buffer and tunables.
	* doc/html/*: Regenerate.
	* include/Makefile.in: Regenerate.
	* libsupc++/Makefile.am: Use EH_POOL_FLAGS.
	* libsupc++/Makefile.in: Regenerate.
	* libsupc++/eh_alloc.cc (EMERGENCY_OBJ_SIZE): Define in units
	of sizeof(void*) not including the ABI's exception header.
	(EMERGENCY_OBJ_COUNT): Define as target-independent calculation
	based on word size.
	(MAX_OBJ_COUNT): Define macro for upper limit on pool size.
	(pool) [_GLIBCXX_EH_POOL_STATIC]: Use fixed-size buffer.
	(pool::buffer_size_in_bytes): New static member function.
	(pool::pool): Parse GLIBCXX_TUNABLES environment variable to set
	pool size at runtime.
	(pool::in_pool): Use std::less<void*> for total order.
	(__freeres) [_GLIBCXX_EH_POOL_STATIC]: Do nothing.
	(__cxa_free_exception, __cxa_free_dependent_exception): Add
	[[unlikely]] attributes.
	* po/Makefile.in: Regenerate.
	* python/Makefile.in: Regenerate.
	* src/Makefile.in: Regenerate.
	* src/c++11/Makefile.in: Regenerate.
	* src/c++17/Makefile.in: Regenerate.
	* src/c++20/Makefile.in: Regenerate.
	* src/c++98/Makefile.in: Regenerate.
	* src/filesystem/Makefile.in: Regenerate.
	* src/libbacktrace/Makefile.in: Regenerate.
	* testsuite/Makefile.in: Regenerate.
2022-10-11 16:21:48 +01:00
Jonathan Wakely
1d2f07ed4c libstdc++: Revert addition of constraints to make_signed/make_unsigned
Constraining the primary template makes it unusable in uninstantiated
contexts.

libstdc++-v3/ChangeLog:

	* include/std/type_traits (make_signed, make_unsigned): Remove
	constraints on primary template.
	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc:
	Undo changes to expected error in C++20 mode.
	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
	Likewise.
	* testsuite/24_iterators/range_access/range_access_cpp20_neg.cc:
	Likewise.
	* testsuite/20_util/make_signed/requirements/uninstantiated.cc:
	New test.
	* testsuite/20_util/make_unsigned/requirements/uninstantiated.cc:
	New test.
2022-10-10 21:37:03 +01:00
Jonathan Wakely
2bd112dee7 libstdc++: std::make_signed_t<cv bool> should be ill-formed
Currently we only reject std::make_signed_t<bool> but not cv bool.
Similarly for std::make_unsigned_t<cv bool>.

As well as making those ill-formed, this adds a requires-clause to the
make_signed and make_unsigned primary templates. This makes
non-integral, non-enum cases fail immediately with a clear error, rather
than giving an error about __make_signed_selector<T, false, false> being
incomplete.

libstdc++-v3/ChangeLog:

	* include/std/type_traits (make_signed, make_unsigned): Add
	specializations for cv bool. Add requires-clause for C++20 to
	improve diagnostics for non-integral, non-enum cases.
	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc:
	Check cv bool.
	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
	Likewise.
	* testsuite/24_iterators/range_access/range_access_cpp20_neg.cc:
	Adjust expected errors for C++20 and later.
	* testsuite/lib/prune.exp: Prune "in requirements  [with ...]"
	lines from diagnostics.
2022-10-10 12:19:11 +01:00
Jonathan Wakely
93b3ab6c0c libstdc++: Shuffle header dependencies of <functional>
The <new> header is needed by std::function to use placement new, so
include it in <bits/std_function.h> instead of in <functional>.

libstdc++-v3/ChangeLog:

	* include/bits/std_function.h: Include <new> but do not include
	<bits/stl_function.h>.
	* include/std/functional: Do not include <new>.
2022-10-07 12:55:33 +01:00
Jonathan Wakely
b8473c9a2b libtdc++: Regenerate Makefile.in after freestanding header changes
libstdc++-v3/ChangeLog:

	* include/Makefile.in: Regenerate.
2022-10-05 14:09:11 +01:00
Jonathan Wakely
dc70f3f81b libstdc++: Guard use of new built-in with __has_builtin
Another case where I forgot that non-GCC compilers don't have this
built-in yet.

libstdc++-v3/ChangeLog:

	* include/bits/invoke.h (__invoke_r): Check
	__has_builtin(__reference_converts_from_temporary) before using
	built-in.
2022-10-05 13:39:28 +01:00
Jonathan Wakely
6ddbbbffbb libstdc++: Use new built-ins __remove_cv, __remove_reference etc.
libstdc++-v3/ChangeLog:

	* include/std/type_traits (remove_cv): Use __remove_cv built-in.
	(remove_reference): Use __remove_reference built-in.
	(remove_cvref): Use __remove_cvref built-in. Remove inheritance
	for fallback implementation.
2022-10-04 17:42:59 +01:00
Jonathan Wakely
68ed966793 libstdc++: Refactor seed sequence constraints in <random>
Every use of _If_seed_seq in <random> and <ext/random> uses it with
enable_if. We can just move the enable_if into the helper alias instead
of repeating it everywhere.

libstdc++-v3/ChangeLog:

	* include/bits/random.h (__is_seed_seq): Replace with ...
	(_If_seed_seq_for): ... this.
	* include/ext/random: Adjust to use _If_seed_seq_for.
2022-10-04 17:42:59 +01:00
Patrick Palka
3b8bcc3f8b libstdc++: Avoid heavyweight std::visit in ranges::join_with_view
libstdc++-v3/ChangeLog:

	* include/std/ranges (join_with_view::_Iterator::operator*):
	Replace use of std::visit with manual visitation.
	(join_with_view::_Iterator::operator++): Likewise.
	(join_with_view::_Iterator::operator--): Likewise.
	(join_with_view::_Iterator::iter_move): Likewise.
	(join_with_view::_Iterator::iter_swap): Likewise.
2022-10-04 10:54:36 -04:00
Jonathan Wakely
b74ab7a160 libstdc++: Enable std::hash<coroutine_handle<P>> [PR107139]
Everything that <coroutine> depends on is available for freestanding
now.

libstdc++-v3/ChangeLog:

	PR libstdc++/107139
	* include/std/coroutine: Remove all _GLIBCXXHOSTED preprocessor
	conditionals.
2022-10-04 15:03:28 +01:00
Jonathan Wakely
7cdab65f3d libstdc++: Make <cstdint> work freestanding [PR107134]
When gcc/config.gcc defines use_gcc_stdin=wrap, GCC's <stdint.h> tries
to use libc's <stdint.h> unless -ffreestanding is used.

When libstdc++ is configured --disable-hosted-libstdcxx we want
<cstdint> to work even without -ffreestanding being given. This is a
kluge to make it include GCC's <stdint-gcc.h> directly even without
-ffreestanding.

libstdc++-v3/ChangeLog:

	PR libstdc++/107134
	* include/c_global/cstdint [!_GLIBCXX_HOSTED]: Include
	<stdint-gcc.h> directly.
2022-10-04 15:03:28 +01:00
Jonathan Wakely
7d8189882f libstdc++: Define <bits/functexcept.h> functions for freestanding [PR107135]
We don't compile src/c++11/functexcept.cc for freestanding, so just
define the functions used by freestanding entities as inline calls to
std::terminate.

libstdc++-v3/ChangeLog:

	PR libstdc++/107135
	* include/bits/functexcept.h [!_GLIBCXX_HOSTED]
	(__throw_invalid_argument, __throw_out_of_range)
	(__throw_out_of_range_fmt, __throw_runtime_error)
	(__throw_overflow_error): Define inline.
	* include/std/bitset (_M_copy_from_ptr) [!_GLIBCXX_HOSTED]:
	Replace __builtin_abort with __throw_invalid_argument.
2022-10-04 15:03:28 +01:00
Patrick Palka
147f6ed39f libstdc++: Implement ranges::join_with_view from P2441R2
libstdc++-v3/ChangeLog:

	* include/std/ranges: Include <variant> for C++23.
	(__detail::__compatible_joinable_ranges): Define.
	(__detail::__bidirectional_common): Define.
	(join_with_view): Define.
	(join_with_view::_Iterator): Define.
	(join_with_view::_Sentinel): Define.
	(views::__detail::__can_join_with_view): Define.
	(views::_JoinWith, views::join_with): Define.
	* testsuite/std/ranges/adaptors/join_with/1.cc: New test.
2022-10-04 09:45:15 -04:00
Arsen Arsenović
49c3e9dfc5 libstdc++: Use ///< for inline documentation
I noticed that some variables were misdocumented when using trailing
comment for documentation.  I ran a search with a relatively simple
regex[1] to look for any ///s following some code that did not have a <,
and came up with these instances only.

[1]: \s*([^  ]+\s*)+///[^<].*$

libstdc++-v3/ChangeLog:

	* include/std/iostream: Use ///< for inline documentation.
	* include/std/limits: Likewise.
	* include/experimental/internet: Likewise.
2022-10-04 09:43:57 +01:00
Jonathan Wakely
7f38b5c4c1 libstdc++: Enable std::bitset<N>::bitset(const char*) for freestanding
The std::bitset constructor from a character string is specified in
terms of std::basic_string and std::char_traits, but doesn't need to be.
This makes it available for freestanding.

libstdc++-v3/ChangeLog:

	* include/std/bitset (bitset(const C*, size_type n, C, C))
	[!_GLIBCXX_HOSTED]: Remove std::basic_string dependency.
	(_M_copy_from_ptr) [!_GLIBCXX_HOSTED]: Abort instead of
	throwing.
2022-10-03 15:43:49 +01:00
Arsen Arsenović
54fd7d8170 libstdc++: Re-enable std::hash<std::bitset> in freestanding [PR103626]
PR libstdc++/103626 - _GLIBCXX_HOSTED should respect -ffreestanding

libstdc++-v3/ChangeLog:

	PR libstdc++/103626
	* include/std/bitset [!_GLIBCXX_HOSTED]: Re-enable std::hash.
	* testsuite/20_util/bitset/cons/constexpr_c++23.cc: Require ET
	hosted.
	* testsuite/20_util/bitset/ext/constexpr.cc: Likewise.
2022-10-03 15:43:49 +01:00
Arsen Arsenović
6885e7e4ee libstdc++: Rework how freestanding install works [PR106953]
In light of there being far more freestanding headers now, ad-hoc
maintenance of a subset of the install implementation has become
unsustainable. Instead, we gate off a part of the normal install routine
so that it works without HOSTED enabled, as well as subdivide lists of
headers into freestanding and hosted components, according to the HOSTED
flag.

libstdc++-v3/ChangeLog:

	PR libstdc++/106953
	* include/Makefile.am [!_GLIBCXX_HOSTED]: Remove
	install-freestanding-headers, unifying it with the usual
	install-headers
	* include/Makefile.in: Regenerate.
2022-10-03 15:43:48 +01:00
Arsen Arsenović
18f176d0b2 libstdc++: Mark headers that must be hosted as such [PR103626]
PR libstdc++/103626 - _GLIBCXX_HOSTED should respect -ffreestanding

Co-authored-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	PR libstdc++/103626
	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/bits/requires_hosted.h: New header.
	* include/experimental/algorithm: Include
	<bits/requires_hosted.h>.
	* include/experimental/any: Likewise.
	* include/experimental/array: Likewise.
	* include/experimental/buffer: Likewise.
	* include/experimental/chrono: Likewise.
	* include/experimental/deque: Likewise.
	* include/experimental/executor: Likewise.
	* include/experimental/filesystem: Likewise.
	* include/experimental/forward_list: Likewise.
	* include/experimental/functional: Likewise.
	* include/experimental/internet: Likewise.
	* include/experimental/io_context: Likewise.
	* include/experimental/iterator: Likewise.
	* include/experimental/list: Likewise.
	* include/experimental/map: Likewise.
	* include/experimental/memory: Likewise.
	* include/experimental/memory_resource: Likewise.
	* include/experimental/net: Likewise.
	* include/experimental/netfwd: Likewise.
	* include/experimental/numeric: Likewise.
	* include/experimental/optional: Likewise.
	* include/experimental/propagate_const: Likewise.
	* include/experimental/random: Likewise.
	* include/experimental/ratio: Likewise.
	* include/experimental/regex: Likewise.
	* include/experimental/scope: Likewise.
	* include/experimental/set: Likewise.
	* include/experimental/simd: Likewise.
	* include/experimental/socket: Likewise.
	* include/experimental/source_location: Likewise.
	* include/experimental/string: Likewise.
	* include/experimental/string_view: Likewise.
	* include/experimental/system_error: Likewise.
	* include/experimental/timer: Likewise.
	* include/experimental/tuple: Likewise.
	* include/experimental/unordered_map: Likewise.
	* include/experimental/unordered_set: Likewise.
	* include/experimental/utility: Likewise.
	* include/experimental/vector: Likewise.
	* include/std/barrier: Likewise.
	* include/std/chrono: Likewise.
	* include/std/condition_variable: Likewise.
	* include/std/deque: Likewise.
	* include/std/execution: Likewise.
	* include/std/filesystem: Likewise.
	* include/std/forward_list: Likewise.
	* include/std/fstream: Likewise.
	* include/std/future: Likewise.
	* include/std/iomanip: Likewise.
	* include/std/ios: Likewise.
	* include/std/iosfwd: Likewise.
	* include/std/iostream: Likewise.
	* include/std/istream: Likewise.
	* include/std/latch: Likewise.
	* include/std/list: Likewise.
	* include/std/locale: Likewise.
	* include/std/map: Likewise.
	* include/std/memory_resource: Likewise.
	* include/std/mutex: Likewise.
	* include/std/ostream: Likewise.
	* include/std/queue: Likewise.
	* include/std/random: Likewise.
	* include/std/regex: Likewise.
	* include/std/semaphore: Likewise.
	* include/std/set: Likewise.
	* include/std/shared_mutex: Likewise.
	* include/std/spanstream: Likewise.
	* include/std/sstream: Likewise.
	* include/std/stack: Likewise.
	* include/std/stacktrace: Likewise.
	* include/std/stop_token: Likewise.
	* include/std/streambuf: Likewise.
	* include/std/string: Likewise.
	* include/std/syncstream: Likewise.
	* include/std/system_error: Likewise.
	* include/std/thread: Likewise.
	* include/std/unordered_map: Likewise.
	* include/std/unordered_set: Likewise.
	* include/std/valarray: Likewise.
	* include/std/vector: Likewise.
	* include/tr1/array: Likewise.
	* include/tr1/ccomplex: Likewise.
	* include/tr1/cctype: Likewise.
	* include/tr1/cfenv: Likewise.
	* include/tr1/cfloat: Likewise.
	* include/tr1/cinttypes: Likewise.
	* include/tr1/climits: Likewise.
	* include/tr1/cmath: Likewise.
	* include/tr1/complex: Likewise.
	* include/tr1/complex.h: Likewise.
	* include/tr1/cstdarg: Likewise.
	* include/tr1/cstdbool: Likewise.
	* include/tr1/cstdint: Likewise.
	* include/tr1/cstdio: Likewise.
	* include/tr1/cstdlib: Likewise.
	* include/tr1/ctgmath: Likewise.
	* include/tr1/ctime: Likewise.
	* include/tr1/ctype.h: Likewise.
	* include/tr1/cwchar: Likewise.
	* include/tr1/cwctype: Likewise.
	* include/tr1/fenv.h: Likewise.
	* include/tr1/float.h: Likewise.
	* include/tr1/functional: Likewise.
	* include/tr1/inttypes.h: Likewise.
	* include/tr1/limits.h: Likewise.
	* include/tr1/math.h: Likewise.
	* include/tr1/memory: Likewise.
	* include/tr1/random: Likewise.
	* include/tr1/regex: Likewise.
	* include/tr1/stdarg.h: Likewise.
	* include/tr1/stdbool.h: Likewise.
	* include/tr1/stdint.h: Likewise.
	* include/tr1/stdio.h: Likewise.
	* include/tr1/stdlib.h: Likewise.
	* include/tr1/tgmath.h: Likewise.
	* include/tr1/tuple: Likewise.
	* include/tr1/type_traits: Likewise.
	* include/tr1/unordered_map: Likewise.
	* include/tr1/unordered_set: Likewise.
	* include/tr1/utility: Likewise.
	* include/tr1/wchar.h: Likewise.
	* include/tr1/wctype.h: Likewise.
	* include/c_global/cmath: Likewise.
	* include/ext/algorithm: Include <bits/requires_hosted.h>.
	* include/ext/bitmap_allocator.h: Likewise.
	* include/ext/cmath: Likewise.
	* include/ext/codecvt_specializations.h: Likewise.
	* include/ext/debug_allocator.h: Likewise.
	* include/ext/enc_filebuf.h: Likewise.
	* include/ext/extptr_allocator.h: Likewise.
	* include/ext/functional: Likewise.
	* include/ext/malloc_allocator.h: Likewise.
	* include/ext/memory: Likewise.
	* include/ext/mt_allocator.h: Likewise.
	* include/ext/new_allocator.h: Likewise.
	* include/ext/numeric: Likewise.
	* include/ext/pod_char_traits.h: Likewise.
	* include/ext/pool_allocator.h: Likewise.
	* include/ext/random: Likewise.
	* include/ext/random.tcc: Likewise.
	* include/ext/rb_tree: Likewise.
	* include/ext/rc_string_base.h: Likewise.
	* include/ext/rope: Likewise.
	* include/ext/ropeimpl.h: Likewise.
	* include/ext/slist: Likewise.
	* include/ext/sso_string_base.h: Likewise.
	* include/ext/stdio_filebuf.h: Likewise.
	* include/ext/stdio_sync_filebuf.h: Likewise.
	* include/ext/string_conversions.h: Likewise.
	* include/ext/throw_allocator.h: Likewise.
	* include/ext/vstring.h: Likewise.
	* include/ext/vstring.tcc: Likewise.
	* include/ext/vstring_fwd.h: Likewise.
	* include/ext/vstring_util.h: Likewise.
	* include/std/charconv: Likewise.
	(__cpp_lib_to_chars): Do not define for freestanding.
	* include/std/version: Adjust which macros get defined in
	freestanding.
	* include/ext/pointer.h [!_GLIBCXX_HOSTED]: Omit iostream
	functionality from freestanding.
	* include/std/algorithm [!_GLIBCXX_HOSTED]: Omit PSTL algos.
	* include/std/memory [!_GLIBCXX_HOSTED]: Omit
	<bits/stl_tempbuf.h> in freestanding
	* include/bits/algorithmfwd.h [!_GLIBCXX_HOSTED]: Omit leftover
	random_shuffle and stable_partition definition.
	* include/bits/stl_algo.h [!_GLIBCXX_HOSTED]: Omit
	random_shuffle and stable_partition from freestanding.
	* include/bits/ranges_algo.h [!_GLIBCXX_HOSTED]: Omit
	stable_partition from freestanding.
	* include/bits/concept_check.h: Remove needless HOSTED check.
	* include/std/iterator: Include <bits/ranges_base.h>.
	* include/std/numeric (__cpp_lib_parallel_algorithms): Do not
	define for freestanding.
	* include/std/functional (__cpp_lib_boyer_moore_searcher):
	Likewise.
	* testsuite/lib/prune.exp: Match error for hosted-only libstdc++
	tests.
2022-10-03 15:43:48 +01:00
Jonathan Wakely
06b3c0fad1 libstdc++: Adjust precompiled headers for freestanding
Co-authored-by: Arsen Arsenović <arsen@aarsen.me>

libstdc++-v3/ChangeLog:

	* include/precompiled/extc++.h [!_GLIBCXX_HOSTED]: Do not
	include headers that aren't valid for freestanding.
	* include/precompiled/stdc++.h [!_GLIBCXX_HOSTED]: Likewise.
2022-10-03 15:43:48 +01:00
Jonathan Wakely
f1b51f68f8 libstdc++: Make _GLIBCXX_HOSTED respect -ffreestanding [PR103626]
This allows the library to switch to freestanding mode when compiling
with the -ffreestanding flag. This means you don't need a separate
libstdc++ build configured with --disable-hosted-libstdcxx in order to
compile for a freestanding environment.

The testsuite support files cannot be compiled for freestanding, so add
-fno-freestanding to override any -ffreestanding in the test flags.

libstdc++-v3/ChangeLog:

	PR libstdc++/103626
	* acinclude.m4 (GLIBCXX_ENABLE_HOSTED): Define _GLIBCXX_HOSTED
	to __STDC_HOSTED__ for non-freestanding installations.
	* configure: Regenerate.
	* include/Makefile.am (${host_builddir}/c++config.h): Adjust
	grep pattern.
	* include/Makefile.in: Regenerate.
	* testsuite/lib/libstdc++.exp (v3-build_support): Use
	-fno-freestanding.
	* testsuite/libstdc++-abi/abi.exp: Likewise.
2022-10-03 15:43:48 +01:00
Jonathan Wakely
1c12a3cfdf libstdc++: Optimize operator>> for std::bitset
We can improve performance by using a char buffer instead of
basic_string. The loop bound already means we can't overflow the buffer,
and we don't need to keep writing a null character after every character
written to the buffer.

We could just use basic_string::resize(N) to zero-init the whole string,
then overwrite those chars. But that zero-init of all N chars would be
wasted in the case where we are writing to a bitset<N> with large N, but
only end up extracting one or two chars from the stream.

With this change we just use buffer of uninitialized chars.  For a
small-ish bitset (currently <= 256) we can improve performance further
by using alloca instead of the heap.

libstdc++-v3/ChangeLog:

	* include/std/bitset (operator>>): Use a simple buffer instead
	of std::basic_string.
2022-09-30 21:52:02 +01:00
Jonathan Wakely
4eb46f453c libstdc++: Remove non-standard public members in std::bitset
This makes _M_copy_from_ptr, _M_copy_from_string and _M_copy_to_string
private, and declares operator<< and operator>> as friends.

Also remove the historical _M_copy_from_string and _M_copy_to_string
overloads. Those were used before DR 396 was implemented but are
not needed now. There are no tests or docs describing them, so I don't
think we intend to support them as extensions.

libstdc++-v3/ChangeLog:

	* include/std/bitset (_M_copy_from_ptr, _M_copy_from_string)
	(_M_copy_to_string): Change access to private.
	(_M_copy_from_string(const basic_string&, size_t, size_t)):
	Remove.
	(_M_copy_to_string(const basic_string&)): Remove.
2022-09-30 21:52:02 +01:00
Arsen Arsenović
57707f38f2 libstdc++: Add missing <bits/stl_algobase.h> include to <bitset>
libstdc++-v3/ChangeLog:

	* include/std/bitset: Include <bits/stl_algobase.h>.
2022-09-30 14:43:19 +01:00
Patrick Palka
9ca1471540 c++: implement __remove_cv, __remove_reference and __remove_cvref
This implements builtins for std::remove_cv, std::remove_reference and
std::remove_cvref using TRAIT_TYPE from the previous patch.

gcc/c-family/ChangeLog:

	* c-common.cc (c_common_reswords): Add __remove_cv,
	__remove_reference and __remove_cvref.
	* c-common.h (enum rid): Add RID_REMOVE_CV, RID_REMOVE_REFERENCE
	and RID_REMOVE_CVREF.

gcc/cp/ChangeLog:

	* constraint.cc (diagnose_trait_expr): Handle CPTK_REMOVE_CV,
	CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
	* cp-objcp-common.cc (names_builtin_p): Likewise.
	* cp-tree.h (enum cp_trait_kind): Add CPTK_REMOVE_CV,
	CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
	* cxx-pretty-print.cc (pp_cxx_trait): Handle CPTK_REMOVE_CV,
	CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
	* parser.cc (cp_keyword_starts_decl_specifier_p): Return true
	for RID_REMOVE_CV, RID_REMOVE_REFERENCE and RID_REMOVE_CVREF.
	(cp_parser_trait): Handle RID_REMOVE_CV, RID_REMOVE_REFERENCE
	and RID_REMOVE_CVREF.
	(cp_parser_simple_type_specifier): Likewise.
	* semantics.cc (finish_trait_type): Likewise.

libstdc++-v3/ChangeLog:

	* include/bits/unique_ptr.h (unique_ptr<_Tp[], _Dp>): Remove
	__remove_cv and use __remove_cv_t instead.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __remove_cv,
	__remove_reference and __remove_cvref.
	* g++.dg/ext/remove_cv.C: New test.
	* g++.dg/ext/remove_reference.C: New test.
	* g++.dg/ext/remove_cvref.C: New test.
2022-09-29 09:18:40 -04:00
Jonathan Wakely
df7f273650 libstdc++: Guard use of new built-in with __has_builtin
I forgot that non-GCC compilers don't have this built-in yet.

For Clang we could do something like the check below (as described in
P2255), but for now I'm just fixing the regression.

 #if __has_builtin((__reference_binds_to_temporary)
  bool _Dangle = __reference_binds_to_temporary(_Tp, _Res_t)
                 && __and_<is_reference<_Tp>,
                           __not_<is_reference<_Res_t>>,
                           is_convertible<__remove_cvref_t<_Res_t>*,
                                          __remove_cvref_t<_Tp>*>>::value
 #endif

libstdc++-v3/ChangeLog:

	* include/std/type_traits (__is_invocable_impl): Check
	__has_builtin(__reference_converts_from_temporary) before using
	built-in.
2022-09-29 12:47:37 +01:00