From 1c1e91d9a35e51778b49a73b39012768770cd482 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 26 Jul 2016 14:29:45 +0000 Subject: [PATCH] Implement LCM and GCD for C++17. Same code as for Library Fundamentals TS. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@276751 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/algorithm | 4 +- include/numeric | 62 ++++++++ .../gcd.not_integral1.fail.cpp | 24 ++++ .../gcd.not_integral2.fail.cpp | 24 ++++ .../numeric.ops/numeric.ops.gcd/gcd.pass.cpp | 132 ++++++++++++++++++ .../lcm.not_integral1.fail.cpp | 24 ++++ .../lcm.not_integral2.fail.cpp | 24 ++++ .../numeric.ops/numeric.ops.lcm/lcm.pass.cpp | 131 +++++++++++++++++ www/cxx1z_status.html | 6 +- 9 files changed, 426 insertions(+), 5 deletions(-) create mode 100644 test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral1.fail.cpp create mode 100644 test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral2.fail.cpp create mode 100644 test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp create mode 100644 test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp create mode 100644 test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp create mode 100644 test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp diff --git a/include/algorithm b/include/algorithm index 25e95ac44..cf03d66d5 100644 --- a/include/algorithm +++ b/include/algorithm @@ -2431,7 +2431,7 @@ __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIt template inline _LIBCPP_INLINE_VISIBILITY _Integral -__gcd(_Integral __x, _Integral __y) +__algo_gcd(_Integral __x, _Integral __y) { do { @@ -2456,7 +2456,7 @@ __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _Ran _VSTD::swap_ranges(__first, __middle, __middle); return __middle; } - const difference_type __g = _VSTD::__gcd(__m1, __m2); + const difference_type __g = _VSTD::__algo_gcd(__m1, __m2); for (_RandomAccessIterator __p = __first + __g; __p != __first;) { value_type __t(_VSTD::move(*--__p)); diff --git a/include/numeric b/include/numeric index 21c3781ab..1643d6400 100644 --- a/include/numeric +++ b/include/numeric @@ -53,6 +53,12 @@ template template void iota(ForwardIterator first, ForwardIterator last, T value); +template + constexpr common_type_t gcd(M m, N n); // C++17 + +template + constexpr common_type_t lcm(M m, N n); // C++17 + } // std */ @@ -192,6 +198,62 @@ iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) *__first = __value_; } + +#if _LIBCPP_STD_VER > 14 +template ::value> struct __abs; + +template +struct __abs<_Tp, true> { + _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY + _Tp operator()(_Tp __t) const noexcept { return __t >= 0 ? __t : -__t; } +}; + +template +struct __abs<_Tp, false> { + _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY + _Tp operator()(_Tp __t) const noexcept { return __t; } +}; + + +template +_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY +_Tp __gcd(_Tp __m, _Tp __n) +{ + static_assert((!is_signed<_Tp>::value), "" ); + return __n == 0 ? __m : __gcd<_Tp>(__n, __m % __n); +} + + +template +_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY +common_type_t<_Tp,_Up> +gcd(_Tp __m, _Up __n) +{ + static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); + using _Rp = common_type_t<_Tp,_Up>; + using _Wp = make_unsigned_t<_Rp>; + return static_cast<_Rp>(__gcd(static_cast<_Wp>(__abs<_Tp>()(__m)), + static_cast<_Wp>(__abs<_Up>()(__n)))); +} + +template +_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY +common_type_t<_Tp,_Up> +lcm(_Tp __m, _Up __n) +{ + static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); + if (__m == 0 || __n == 0) + return 0; + + using _Rp = common_type_t<_Tp,_Up>; + _Rp __val1 = __abs<_Tp>()(__m) / gcd(__m,__n); + _Up __val2 = __abs<_Up>()(__n); + _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); + return __val1 * __val2; +} + +#endif /* _LIBCPP_STD_VER > 14 */ + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_NUMERIC diff --git a/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral1.fail.cpp b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral1.fail.cpp new file mode 100644 index 000000000..585e40bac --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral1.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::gcd(2.0, 4); +} diff --git a/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral2.fail.cpp b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral2.fail.cpp new file mode 100644 index 000000000..7a2709aa9 --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.not_integral2.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::gcd(4, 6.0); +} diff --git a/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp new file mode 100644 index 000000000..84625cd65 --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp @@ -0,0 +1,132 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) + +#include +#include +#include // for rand() +#include + +constexpr struct { + int x; + int y; + int expect; +} Cases[] = { + {0, 0, 0}, + {1, 0, 1}, + {0, 1, 1}, + {1, 1, 1}, + {2, 3, 1}, + {2, 4, 2}, + {36, 17, 1}, + {36, 18, 18} +}; + + +template +constexpr bool test0(Input1 in1, Input2 in2, Output out) +{ + static_assert((std::is_same::value), "" ); + static_assert((std::is_same::value), "" ); + return out == std::gcd(in1, in2) ? true : (std::abort(), false); +} + + +template +constexpr bool do_test(int dummy = 0) +{ + using S1 = typename std::make_signed::type; + using S2 = typename std::make_signed::type; + using U1 = typename std::make_unsigned::type; + using U2 = typename std::make_unsigned::type; + bool accumulate = true; + for (auto TC : Cases) { + { // Test with two signed types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + } + { // test with two unsigned types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + } + return accumulate; +} + +int main() +{ + auto non_cce = std::rand(); // a value that can't possibly be constexpr + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test< int8_t>(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test< int8_t>(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); +} diff --git a/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp new file mode 100644 index 000000000..1e69b25b1 --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::lcm(2.0, 4); +} diff --git a/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp new file mode 100644 index 000000000..d58d903a2 --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::lcm(4, 6.0); +} diff --git a/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp new file mode 100644 index 000000000..2375a3497 --- /dev/null +++ b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp @@ -0,0 +1,131 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// + +// template +// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) + +#include +#include +#include +#include + +constexpr struct { + int x; + int y; + int expect; +} Cases[] = { + {0, 0, 0}, + {1, 0, 0}, + {0, 1, 0}, + {1, 1, 1}, + {2, 3, 6}, + {2, 4, 4}, + {3, 17, 51}, + {36, 18, 36} +}; + +template +constexpr bool test0(Input1 in1, Input2 in2, Output out) +{ + static_assert((std::is_same::value), "" ); + static_assert((std::is_same::value), "" ); + return out == std::lcm(in1, in2) ? true : (std::abort(), false); +} + + +template +constexpr bool do_test(int dummy = 0) +{ + using S1 = typename std::make_signed::type; + using S2 = typename std::make_signed::type; + using U1 = typename std::make_unsigned::type; + using U2 = typename std::make_unsigned::type; + bool accumulate = true; + for (auto TC : Cases) { + { // Test with two signed types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + } + { // test with two unsigned types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + } + return accumulate; +} + +int main() +{ + auto non_cce = std::rand(); // a value that can't possibly be constexpr + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test< int8_t>(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test< int8_t>(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); +} diff --git a/www/cxx1z_status.html b/www/cxx1z_status.html index fcd52cf90..108a68b11 100644 --- a/www/cxx1z_status.html +++ b/www/cxx1z_status.html @@ -108,14 +108,14 @@ p0181r1LWGOrdered by DefaultOulu p0209r2LWGmake_from_tuple: apply for constructionOuluComplete3.9 p0219r1LWGRelative Paths for FilesystemOulu - p0254r2LWGIntegrating std::string_view and std::stringOulu + p0254r2LWGIntegrating std::string_view and std::stringOuluComplete4.0 p0258r2LWGhas_unique_object_representationsOulu - p0295r0LWGAdopt Selected Library Fundamentals V2 Components for C++17Oulu + p0295r0LWGAdopt Selected Library Fundamentals V2 Components for C++17OuluComplete4.0 p0302r1LWGRemoving Allocator Support in std::functionOulu p0307r2LWGMaking Optional Greater Equal AgainOulu p0336r1LWGBetter Names for Parallel Execution Policies in C++17Oulu p0337r0LWGDelete operator= for polymorphic_allocatorOuluComplete3.9 - p0346r1LWGA <random> Nomenclature TweakOulu + p0346r1LWGA <random> Nomenclature TweakOuluComplete3.9 p0358r1LWGFixes for not_fnOuluComplete3.9 p0371r1LWGTemporarily discourage memory_order_consumeOulu p0392r0LWGAdapting string_view by filesystem pathsOuluComplete4.0