gcc/libstdc++-v3/include/experimental
Jonathan Wakely 82db1a42e9 libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978]
This fixes a bug with mixed signed and unsigned types, where converting
a negative value to the unsigned result type alters the value. The
solution is to obtain the absolute values of the arguments immediately
and to perform the actual GCD or LCM algorithm on two arguments of the
same type.

In order to operate on the most negative number without overflow when
taking its absolute, use an unsigned type for the result of the abs
operation. For example, -INT_MIN will overflow, but -(unsigned)INT_MIN
is (unsigned)INT_MAX+1U which is the correct value.

libstdc++-v3/ChangeLog:

	PR libstdc++/92978
	* include/std/numeric (__abs_integral): Replace with ...
	(__detail::__absu): New function template that returns an
	unsigned type, guaranteeing it can represent the most
	negative signed value.
	(__detail::__gcd, __detail::__lcm): Require arguments to
	be unsigned and therefore already non-negative.
	(gcd, lcm): Convert arguments to absolute value as unsigned
	type before calling __detail::__gcd or __detail::__lcm.
	* include/experimental/numeric (gcd, lcm): Likewise.
	* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust expected
	errors.
	* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
	* testsuite/26_numerics/gcd/92978.cc: New test.
	* testsuite/26_numerics/lcm/92978.cc: New test.
	* testsuite/experimental/numeric/92978.cc: New test.
2020-08-28 23:03:28 +01:00
..
bits libstdc++: Refactor filesystem::path string conversions 2020-05-23 09:40:16 +01:00
algorithm Update copyright years. 2020-01-01 12:51:42 +01:00
any Update copyright years. 2020-01-01 12:51:42 +01:00
array Update copyright years. 2020-01-01 12:51:42 +01:00
buffer libstdc++: Use 'using' for types in Networking TS headers 2020-05-21 00:59:55 +01:00
chrono Update copyright years. 2020-01-01 12:51:42 +01:00
deque Update copyright years. 2020-01-01 12:51:42 +01:00
executor libstdc++: Make Networking TS work without gthreads [PR 89760] 2020-08-11 16:16:22 +01:00
filesystem Update copyright years. 2020-01-01 12:51:42 +01:00
forward_list Update copyright years. 2020-01-01 12:51:42 +01:00
functional Update copyright years. 2020-01-01 12:51:42 +01:00
internet libstdc++: Fix net::basic_socket::close(error_code&) 2020-05-21 00:59:55 +01:00
io_context libstdc++: Make Networking TS work without gthreads [PR 89760] 2020-08-11 16:16:22 +01:00
iterator Update copyright years. 2020-01-01 12:51:42 +01:00
list Update copyright years. 2020-01-01 12:51:42 +01:00
map Update copyright years. 2020-01-01 12:51:42 +01:00
memory Update copyright years. 2020-01-01 12:51:42 +01:00
memory_resource x86-vxworks malloc aligns to 8 bytes like solaris 2020-05-13 04:49:00 -03:00
net Update copyright years. 2020-01-01 12:51:42 +01:00
netfwd Update copyright years. 2020-01-01 12:51:42 +01:00
numeric libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978] 2020-08-28 23:03:28 +01:00
optional Update copyright years. 2020-01-01 12:51:42 +01:00
propagate_const Update copyright years. 2020-01-01 12:51:42 +01:00
random Update copyright years. 2020-01-01 12:51:42 +01:00
ratio Update copyright years. 2020-01-01 12:51:42 +01:00
regex Update copyright years. 2020-01-01 12:51:42 +01:00
set Update copyright years. 2020-01-01 12:51:42 +01:00
socket libstdc++: Use macro for nodiscard attribute 2020-05-21 01:03:27 +01:00
source_location Update copyright years. 2020-01-01 12:51:42 +01:00
string Update copyright years. 2020-01-01 12:51:42 +01:00
string_view libstdc++: Enable assertions in constexpr string_view members [PR 71960] 2020-08-26 14:48:49 +01:00
system_error Update copyright years. 2020-01-01 12:51:42 +01:00
timer libstdc++: Use 'using' for types in Networking TS headers 2020-05-21 00:59:55 +01:00
tuple Update copyright years. 2020-01-01 12:51:42 +01:00
type_traits libstdc++: Fix testsuite failures and warnings due to is_pod deprecation 2020-01-09 21:31:50 +00:00
unordered_map Update copyright years. 2020-01-01 12:51:42 +01:00
unordered_set Update copyright years. 2020-01-01 12:51:42 +01:00
utility Update copyright years. 2020-01-01 12:51:42 +01:00
vector Update copyright years. 2020-01-01 12:51:42 +01:00