119b4035fSMarshall Clow //===----------------------------------------------------------------------===// 219b4035fSMarshall Clow // 319b4035fSMarshall Clow // The LLVM Compiler Infrastructure 419b4035fSMarshall Clow // 519b4035fSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open 619b4035fSMarshall Clow // Source Licenses. See LICENSE.TXT for details. 719b4035fSMarshall Clow // 819b4035fSMarshall Clow //===----------------------------------------------------------------------===// 919b4035fSMarshall Clow // 1019b4035fSMarshall Clow // UNSUPPORTED: c++98, c++03, c++11, c++14 11*a2e54b63SMarshall Clow // XFAIL: ubsan 12*a2e54b63SMarshall Clow 1319b4035fSMarshall Clow // <numeric> 1419b4035fSMarshall Clow 1519b4035fSMarshall Clow // template<class _M, class _N> 1619b4035fSMarshall Clow // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) 1719b4035fSMarshall Clow 1819b4035fSMarshall Clow #include <numeric> 1919b4035fSMarshall Clow #include <cassert> 2019b4035fSMarshall Clow #include <cstdlib> // for rand() 2119b4035fSMarshall Clow #include <iostream> 2219b4035fSMarshall Clow 2319b4035fSMarshall Clow constexpr struct { 2419b4035fSMarshall Clow int x; 2519b4035fSMarshall Clow int y; 2619b4035fSMarshall Clow int expect; 2719b4035fSMarshall Clow } Cases[] = { 2819b4035fSMarshall Clow {0, 0, 0}, 2919b4035fSMarshall Clow {1, 0, 1}, 3019b4035fSMarshall Clow {0, 1, 1}, 3119b4035fSMarshall Clow {1, 1, 1}, 3219b4035fSMarshall Clow {2, 3, 1}, 3319b4035fSMarshall Clow {2, 4, 2}, 3419b4035fSMarshall Clow {36, 17, 1}, 3519b4035fSMarshall Clow {36, 18, 18} 3619b4035fSMarshall Clow }; 3719b4035fSMarshall Clow 3819b4035fSMarshall Clow 3919b4035fSMarshall Clow template <typename Input1, typename Input2, typename Output> 4019b4035fSMarshall Clow constexpr bool test0(Input1 in1, Input2 in2, Output out) 4119b4035fSMarshall Clow { 4219b4035fSMarshall Clow static_assert((std::is_same<Output, decltype(std::gcd(in1, in2))>::value), "" ); 4319b4035fSMarshall Clow static_assert((std::is_same<Output, decltype(std::gcd(in2, in1))>::value), "" ); 4419b4035fSMarshall Clow return out == std::gcd(in1, in2) ? true : (std::abort(), false); 4519b4035fSMarshall Clow } 4619b4035fSMarshall Clow 4719b4035fSMarshall Clow 4819b4035fSMarshall Clow template <typename Input1, typename Input2 = Input1> 49fd838227SEric Fiselier constexpr bool do_test(int = 0) 5019b4035fSMarshall Clow { 5119b4035fSMarshall Clow using S1 = typename std::make_signed<Input1>::type; 5219b4035fSMarshall Clow using S2 = typename std::make_signed<Input2>::type; 5319b4035fSMarshall Clow using U1 = typename std::make_unsigned<Input1>::type; 5419b4035fSMarshall Clow using U2 = typename std::make_unsigned<Input2>::type; 5519b4035fSMarshall Clow bool accumulate = true; 5619b4035fSMarshall Clow for (auto TC : Cases) { 5719b4035fSMarshall Clow { // Test with two signed types 5819b4035fSMarshall Clow using Output = std::common_type_t<S1, S2>; 5919b4035fSMarshall Clow accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect); 6019b4035fSMarshall Clow accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect); 6119b4035fSMarshall Clow accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect); 6219b4035fSMarshall Clow accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect); 6319b4035fSMarshall Clow accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect); 6419b4035fSMarshall Clow accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect); 6519b4035fSMarshall Clow accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect); 6619b4035fSMarshall Clow accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect); 6719b4035fSMarshall Clow } 6819b4035fSMarshall Clow { // test with two unsigned types 6919b4035fSMarshall Clow using Output = std::common_type_t<U1, U2>; 7019b4035fSMarshall Clow accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect); 7119b4035fSMarshall Clow accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect); 7219b4035fSMarshall Clow } 7319b4035fSMarshall Clow { // Test with mixed signs 7419b4035fSMarshall Clow using Output = std::common_type_t<S1, U2>; 7519b4035fSMarshall Clow accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect); 7619b4035fSMarshall Clow accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect); 7719b4035fSMarshall Clow accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect); 7819b4035fSMarshall Clow accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect); 7919b4035fSMarshall Clow } 8019b4035fSMarshall Clow { // Test with mixed signs 8119b4035fSMarshall Clow using Output = std::common_type_t<S2, U1>; 8219b4035fSMarshall Clow accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect); 8319b4035fSMarshall Clow accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect); 8419b4035fSMarshall Clow accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect); 8519b4035fSMarshall Clow accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect); 8619b4035fSMarshall Clow } 8719b4035fSMarshall Clow } 8819b4035fSMarshall Clow return accumulate; 8919b4035fSMarshall Clow } 9019b4035fSMarshall Clow 9119b4035fSMarshall Clow int main() 9219b4035fSMarshall Clow { 9319b4035fSMarshall Clow auto non_cce = std::rand(); // a value that can't possibly be constexpr 9419b4035fSMarshall Clow 9519b4035fSMarshall Clow static_assert(do_test<signed char>(), ""); 9619b4035fSMarshall Clow static_assert(do_test<short>(), ""); 9719b4035fSMarshall Clow static_assert(do_test<int>(), ""); 9819b4035fSMarshall Clow static_assert(do_test<long>(), ""); 9919b4035fSMarshall Clow static_assert(do_test<long long>(), ""); 10019b4035fSMarshall Clow 10119b4035fSMarshall Clow assert(do_test<signed char>(non_cce)); 10219b4035fSMarshall Clow assert(do_test<short>(non_cce)); 10319b4035fSMarshall Clow assert(do_test<int>(non_cce)); 10419b4035fSMarshall Clow assert(do_test<long>(non_cce)); 10519b4035fSMarshall Clow assert(do_test<long long>(non_cce)); 10619b4035fSMarshall Clow 10719b4035fSMarshall Clow static_assert(do_test< int8_t>(), ""); 10819b4035fSMarshall Clow static_assert(do_test<int16_t>(), ""); 10919b4035fSMarshall Clow static_assert(do_test<int32_t>(), ""); 11019b4035fSMarshall Clow static_assert(do_test<int64_t>(), ""); 11119b4035fSMarshall Clow 11219b4035fSMarshall Clow assert(do_test< int8_t>(non_cce)); 11319b4035fSMarshall Clow assert(do_test<int16_t>(non_cce)); 11419b4035fSMarshall Clow assert(do_test<int32_t>(non_cce)); 11519b4035fSMarshall Clow assert(do_test<int64_t>(non_cce)); 11619b4035fSMarshall Clow 11719b4035fSMarshall Clow static_assert(do_test<signed char, int>(), ""); 11819b4035fSMarshall Clow static_assert(do_test<int, signed char>(), ""); 11919b4035fSMarshall Clow static_assert(do_test<short, int>(), ""); 12019b4035fSMarshall Clow static_assert(do_test<int, short>(), ""); 12119b4035fSMarshall Clow static_assert(do_test<int, long>(), ""); 12219b4035fSMarshall Clow static_assert(do_test<long, int>(), ""); 12319b4035fSMarshall Clow static_assert(do_test<int, long long>(), ""); 12419b4035fSMarshall Clow static_assert(do_test<long long, int>(), ""); 12519b4035fSMarshall Clow 12619b4035fSMarshall Clow assert((do_test<signed char, int>(non_cce))); 12719b4035fSMarshall Clow assert((do_test<int, signed char>(non_cce))); 12819b4035fSMarshall Clow assert((do_test<short, int>(non_cce))); 12919b4035fSMarshall Clow assert((do_test<int, short>(non_cce))); 13019b4035fSMarshall Clow assert((do_test<int, long>(non_cce))); 13119b4035fSMarshall Clow assert((do_test<long, int>(non_cce))); 13219b4035fSMarshall Clow assert((do_test<int, long long>(non_cce))); 13319b4035fSMarshall Clow assert((do_test<long long, int>(non_cce))); 1347a1c0effSMarshall Clow 1357a1c0effSMarshall Clow // LWG#2837 1367a1c0effSMarshall Clow { 1377a1c0effSMarshall Clow auto res = std::gcd((int64_t)1234, (int32_t)-2147483648); 1387a1c0effSMarshall Clow static_assert( std::is_same<decltype(res), std::common_type<int64_t, int32_t>::type>::value, ""); 1397a1c0effSMarshall Clow assert(res == 2); 1407a1c0effSMarshall Clow } 14119b4035fSMarshall Clow } 142