Make lcm/gcd work better in edge cases. Fixes a UBSAN failure.

llvm-svn: 294779
diff --git a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
index a52b50b..2c1a3b8 100644
--- a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+++ b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
@@ -129,4 +129,11 @@
     assert((do_test<long, int>(non_cce)));
     assert((do_test<int, long long>(non_cce)));
     assert((do_test<long long, int>(non_cce)));
+
+//  LWG#2792
+    {
+        auto res = std::gcd((int64_t)1234, (int32_t)-2147483648);
+        static_assert( std::is_same<decltype(res), std::common_type<int64_t, int32_t>::type>::value, "");
+        assert(res == 2);
+    }
 }
diff --git a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
index e3c109f..4dd139a 100644
--- a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
+++ b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
@@ -128,4 +128,12 @@
     assert((do_test<long, int>(non_cce)));
     assert((do_test<int, long long>(non_cce)));
     assert((do_test<long long, int>(non_cce)));
+
+//  LWG#2792
+    {
+    auto res1 = std::lcm((int64_t)1234, (int32_t)-2147483648);
+    (void) std::lcm<int, unsigned long>(INT_MIN, 2);	// this used to trigger UBSAN
+    static_assert( std::is_same<decltype(res1), std::common_type<int64_t, int32_t>::type>::value, "");
+	assert(res1 == 1324997410816LL);
+    }
 }
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
index 0774bff..961b515 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 //
 // UNSUPPORTED: c++98, c++03, c++11, c++14
-// XFAIL: ubsan
 
 // <numeric>
 
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
index a74ac76..90d4839 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 //
 // UNSUPPORTED: c++98, c++03, c++11, c++14
-// XFAIL: ubsan
 // <numeric>
 
 // template<class _M, class _N>
@@ -132,8 +131,9 @@
 
 //  LWG#2837
     {
-        auto res = std::lcm((int64_t)1234, (int32_t)-2147483648);
-        static_assert( std::is_same<decltype(res), std::common_type<int64_t, int32_t>::type>::value, "");
-        assert(res == -1324997410816LL);
+    auto res1 = std::lcm((int64_t)1234, (int32_t)-2147483648);
+    (void) std::lcm<int, unsigned long>(INT_MIN, 2);	// this used to trigger UBSAN
+    static_assert( std::is_same<decltype(res1), std::common_type<int64_t, int32_t>::type>::value, "");
+	assert(res1 == 1324997410816LL);
     }
 }