119b4035fSMarshall Clow //===----------------------------------------------------------------------===//
219b4035fSMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
619b4035fSMarshall Clow //
719b4035fSMarshall Clow //===----------------------------------------------------------------------===//
819b4035fSMarshall Clow //
9*31cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
10a2e54b63SMarshall Clow 
1119b4035fSMarshall Clow // <numeric>
1219b4035fSMarshall Clow 
1319b4035fSMarshall Clow // template<class _M, class _N>
1419b4035fSMarshall Clow // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
1519b4035fSMarshall Clow 
1619b4035fSMarshall Clow #include <numeric>
1719b4035fSMarshall Clow #include <cassert>
188c401179SBilly Robert O'Neal III #include <climits>
198c401179SBilly Robert O'Neal III #include <cstdint>
2019b4035fSMarshall Clow #include <cstdlib>    // for rand()
218c401179SBilly Robert O'Neal III #include <type_traits>
2219b4035fSMarshall Clow 
237fc6a556SMarshall Clow #include "test_macros.h"
247fc6a556SMarshall Clow 
2519b4035fSMarshall Clow constexpr struct {
2619b4035fSMarshall Clow   int x;
2719b4035fSMarshall Clow   int y;
2819b4035fSMarshall Clow   int expect;
2919b4035fSMarshall Clow } Cases[] = {
3019b4035fSMarshall Clow     {0, 0, 0},
3119b4035fSMarshall Clow     {1, 0, 1},
3219b4035fSMarshall Clow     {0, 1, 1},
3319b4035fSMarshall Clow     {1, 1, 1},
3419b4035fSMarshall Clow     {2, 3, 1},
3519b4035fSMarshall Clow     {2, 4, 2},
3619b4035fSMarshall Clow     {36, 17, 1},
3719b4035fSMarshall Clow     {36, 18, 18}
3819b4035fSMarshall Clow };
3919b4035fSMarshall Clow 
4019b4035fSMarshall Clow 
4119b4035fSMarshall Clow template <typename Input1, typename Input2, typename Output>
428c401179SBilly Robert O'Neal III constexpr bool test0(int in1, int in2, int out)
4319b4035fSMarshall Clow {
448c401179SBilly Robert O'Neal III     auto value1 = static_cast<Input1>(in1);
458c401179SBilly Robert O'Neal III     auto value2 = static_cast<Input2>(in2);
468c401179SBilly Robert O'Neal III     static_assert(std::is_same_v<Output, decltype(std::gcd(value1, value2))>, "");
478c401179SBilly Robert O'Neal III     static_assert(std::is_same_v<Output, decltype(std::gcd(value2, value1))>, "");
488c401179SBilly Robert O'Neal III     assert(static_cast<Output>(out) == std::gcd(value1, value2));
498c401179SBilly Robert O'Neal III     return true;
5019b4035fSMarshall Clow }
5119b4035fSMarshall Clow 
5219b4035fSMarshall Clow 
5319b4035fSMarshall Clow template <typename Input1, typename Input2 = Input1>
54fd838227SEric Fiselier constexpr bool do_test(int = 0)
5519b4035fSMarshall Clow {
568c401179SBilly Robert O'Neal III     using S1 = std::make_signed_t<Input1>;
578c401179SBilly Robert O'Neal III     using S2 = std::make_signed_t<Input2>;
588c401179SBilly Robert O'Neal III     using U1 = std::make_unsigned_t<Input1>;
598c401179SBilly Robert O'Neal III     using U2 = std::make_unsigned_t<Input2>;
6019b4035fSMarshall Clow     bool accumulate = true;
6119b4035fSMarshall Clow     for (auto TC : Cases) {
6219b4035fSMarshall Clow         { // Test with two signed types
6319b4035fSMarshall Clow             using Output = std::common_type_t<S1, S2>;
6419b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
6519b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
6619b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
6719b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
6819b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
6919b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
7019b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
7119b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
7219b4035fSMarshall Clow         }
7319b4035fSMarshall Clow         { // test with two unsigned types
7419b4035fSMarshall Clow             using Output = std::common_type_t<U1, U2>;
7519b4035fSMarshall Clow             accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
7619b4035fSMarshall Clow             accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
7719b4035fSMarshall Clow         }
7819b4035fSMarshall Clow         { // Test with mixed signs
7919b4035fSMarshall Clow             using Output = std::common_type_t<S1, U2>;
8019b4035fSMarshall Clow             accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
8119b4035fSMarshall Clow             accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
8219b4035fSMarshall Clow             accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
8319b4035fSMarshall Clow             accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
8419b4035fSMarshall Clow         }
8519b4035fSMarshall Clow         { // Test with mixed signs
8619b4035fSMarshall Clow             using Output = std::common_type_t<S2, U1>;
8719b4035fSMarshall Clow             accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
8819b4035fSMarshall Clow             accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
8919b4035fSMarshall Clow             accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
9019b4035fSMarshall Clow             accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
9119b4035fSMarshall Clow         }
9219b4035fSMarshall Clow     }
9319b4035fSMarshall Clow     return accumulate;
9419b4035fSMarshall Clow }
9519b4035fSMarshall Clow 
962df59c50SJF Bastien int main(int, char**)
9719b4035fSMarshall Clow {
9819b4035fSMarshall Clow     auto non_cce = std::rand(); // a value that can't possibly be constexpr
9919b4035fSMarshall Clow 
10019b4035fSMarshall Clow     static_assert(do_test<signed char>(), "");
10119b4035fSMarshall Clow     static_assert(do_test<short>(), "");
10219b4035fSMarshall Clow     static_assert(do_test<int>(), "");
10319b4035fSMarshall Clow     static_assert(do_test<long>(), "");
10419b4035fSMarshall Clow     static_assert(do_test<long long>(), "");
10519b4035fSMarshall Clow 
10619b4035fSMarshall Clow     assert(do_test<signed char>(non_cce));
10719b4035fSMarshall Clow     assert(do_test<short>(non_cce));
10819b4035fSMarshall Clow     assert(do_test<int>(non_cce));
10919b4035fSMarshall Clow     assert(do_test<long>(non_cce));
11019b4035fSMarshall Clow     assert(do_test<long long>(non_cce));
11119b4035fSMarshall Clow 
1128c401179SBilly Robert O'Neal III     static_assert(do_test<std::int8_t>(), "");
1138c401179SBilly Robert O'Neal III     static_assert(do_test<std::int16_t>(), "");
1148c401179SBilly Robert O'Neal III     static_assert(do_test<std::int32_t>(), "");
1158c401179SBilly Robert O'Neal III     static_assert(do_test<std::int64_t>(), "");
11619b4035fSMarshall Clow 
1178c401179SBilly Robert O'Neal III     assert(do_test<std::int8_t>(non_cce));
1188c401179SBilly Robert O'Neal III     assert(do_test<std::int16_t>(non_cce));
1198c401179SBilly Robert O'Neal III     assert(do_test<std::int32_t>(non_cce));
1208c401179SBilly Robert O'Neal III     assert(do_test<std::int64_t>(non_cce));
12119b4035fSMarshall Clow 
12219b4035fSMarshall Clow     static_assert(do_test<signed char, int>(), "");
12319b4035fSMarshall Clow     static_assert(do_test<int, signed char>(), "");
12419b4035fSMarshall Clow     static_assert(do_test<short, int>(), "");
12519b4035fSMarshall Clow     static_assert(do_test<int, short>(), "");
12619b4035fSMarshall Clow     static_assert(do_test<int, long>(), "");
12719b4035fSMarshall Clow     static_assert(do_test<long, int>(), "");
12819b4035fSMarshall Clow     static_assert(do_test<int, long long>(), "");
12919b4035fSMarshall Clow     static_assert(do_test<long long, int>(), "");
13019b4035fSMarshall Clow 
13119b4035fSMarshall Clow     assert((do_test<signed char, int>(non_cce)));
13219b4035fSMarshall Clow     assert((do_test<int, signed char>(non_cce)));
13319b4035fSMarshall Clow     assert((do_test<short, int>(non_cce)));
13419b4035fSMarshall Clow     assert((do_test<int, short>(non_cce)));
13519b4035fSMarshall Clow     assert((do_test<int, long>(non_cce)));
13619b4035fSMarshall Clow     assert((do_test<long, int>(non_cce)));
13719b4035fSMarshall Clow     assert((do_test<int, long long>(non_cce)));
13819b4035fSMarshall Clow     assert((do_test<long long, int>(non_cce)));
1397a1c0effSMarshall Clow 
1407a1c0effSMarshall Clow //  LWG#2837
1417a1c0effSMarshall Clow     {
1428c401179SBilly Robert O'Neal III     auto res = std::gcd(static_cast<std::int64_t>(1234), INT32_MIN);
1438c401179SBilly Robert O'Neal III     static_assert(std::is_same_v<decltype(res), std::int64_t>, "");
1447a1c0effSMarshall Clow     assert(res == 2);
1457a1c0effSMarshall Clow     }
1462df59c50SJF Bastien 
1472df59c50SJF Bastien   return 0;
14819b4035fSMarshall Clow }
149