gcc/libstdc++-v3/testsuite/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
..
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
chrono Update copyright years. 2020-01-01 12:51:42 +01:00
deque Update copyright years. 2020-01-01 12:51:42 +01:00
filesystem libstdc++: Remove accidental -std=gnu++17 from test 2020-07-31 18:02:10 +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
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 libstdc++: Make Networking TS work without gthreads [PR 89760] 2020-08-11 16:16:22 +01:00
numeric libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978] 2020-08-28 23:03:28 +01:00
optional PR libstdc++/95915 2020-06-29 00:36:38 +03:00
polymorphic_allocator 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
set Update copyright years. 2020-01-01 12:51:42 +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 Update copyright years. 2020-01-01 12:51:42 +01:00
system_error Update copyright years. 2020-01-01 12:51:42 +01:00
tuple Update copyright years. 2020-01-01 12:51:42 +01:00
type_erased_allocator Update copyright years. 2020-01-01 12:51:42 +01:00
type_traits PR libstdc++/95915 2020-06-29 00:36:38 +03: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
vector Update copyright years. 2020-01-01 12:51:42 +01:00
feat-char8_t.cc libstdc++: P1423R3 char8_t remediation (3/4) 2019-11-29 17:43:46 +00:00
feat-cxx14.cc Use dg-do preprocess instead of compile for tests 2016-08-23 11:13:31 +01:00
feat-lib-fund.cc Use dg-do preprocess instead of compile for tests 2016-08-23 11:13:31 +01:00
names.cc Update copyright years. 2020-01-01 12:51:42 +01:00