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
algorithm
any
array
buffer
chrono
deque
executor libstdc++: Make Networking TS work without gthreads [PR 89760] 2020-08-11 16:16:22 +01:00
filesystem
forward_list
functional
internet
io_context libstdc++: Make Networking TS work without gthreads [PR 89760] 2020-08-11 16:16:22 +01:00
iterator
list
map
memory
memory_resource
net
netfwd
numeric libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978] 2020-08-28 23:03:28 +01:00
optional
propagate_const
random
ratio
regex
set
socket
source_location
string
string_view libstdc++: Enable assertions in constexpr string_view members [PR 71960] 2020-08-26 14:48:49 +01:00
system_error
timer
tuple
type_traits
unordered_map
unordered_set
utility
vector