From 2c15439dd3443bb01ee1f9a96fe932cfc80668ea Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 17 May 2016 14:52:19 +0000 Subject: [PATCH] Implement P0030R1: Introduce a 3-Argument Overload to std::hypot git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@269772 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/cmath | 28 +++++++++++++++++++++++++ test/std/numerics/c.math/cmath.pass.cpp | 23 ++++++++++++++++++++ www/cxx1z_status.html | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/cmath b/include/cmath index ebbde1816..b3e959405 100644 --- a/include/cmath +++ b/include/cmath @@ -209,6 +209,10 @@ floating_point hypot (arithmetic x, arithmetic y); float hypotf(float x, float y); long double hypotl(long double x, long double y); +double hypot(double x, double y, double z); // C++17 +float hypot(float x, float y, float z); // C++17 +long double hypot(long double x, long double y, long double z); // C++17 + int ilogb (arithmetic x); int ilogbf(float x); int ilogbl(long double x); @@ -548,6 +552,30 @@ using ::lgamma; using ::lgammaf; #endif // __sun__ +#if _LIBCPP_STD_VER > 14 +inline _LIBCPP_INLINE_VISIBILITY float hypot( float x, float y, float z ) { return sqrt(x*x + y*y + z*z); } +inline _LIBCPP_INLINE_VISIBILITY double hypot( double x, double y, double z ) { return sqrt(x*x + y*y + z*z); } +inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); } + +template +inline _LIBCPP_INLINE_VISIBILITY +typename std::__lazy_enable_if +< + std::is_arithmetic<_A1>::value && + std::is_arithmetic<_A2>::value && + std::is_arithmetic<_A3>::value, + std::__promote<_A1, _A2, _A3> +>::type +hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT +{ + typedef typename std::__promote<_A1, _A2, _A3>::type __result_type; + static_assert((!(std::is_same<_A1, __result_type>::value && + std::is_same<_A2, __result_type>::value && + std::is_same<_A3, __result_type>::value)), ""); + return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z); +} +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_CMATH diff --git a/test/std/numerics/c.math/cmath.pass.cpp b/test/std/numerics/c.math/cmath.pass.cpp index 0b0d86288..5b78925f0 100644 --- a/test/std/numerics/c.math/cmath.pass.cpp +++ b/test/std/numerics/c.math/cmath.pass.cpp @@ -79,6 +79,7 @@ Ambiguous fma(Ambiguous, Ambiguous, Ambiguous){ return Ambiguous(); } Ambiguous fmax(Ambiguous, Ambiguous){ return Ambiguous(); } Ambiguous fmin(Ambiguous, Ambiguous){ return Ambiguous(); } Ambiguous hypot(Ambiguous, Ambiguous){ return Ambiguous(); } +Ambiguous hypot(Ambiguous, Ambiguous, Ambiguous){ return Ambiguous(); } Ambiguous ilogb(Ambiguous){ return Ambiguous(); } Ambiguous lgamma(Ambiguous){ return Ambiguous(); } Ambiguous llrint(Ambiguous){ return Ambiguous(); } @@ -1010,6 +1011,28 @@ void test_hypot() static_assert((std::is_same::value), ""); static_assert((std::is_same::value), ""); assert(std::hypot(3,4) == 5); + +#if TEST_STD_VER > 14 + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + static_assert((std::is_same::value), ""); + + assert(std::hypot(2,3,6) == 7); + assert(std::hypot(1,4,8) == 9); +#endif } void test_ilogb() diff --git a/www/cxx1z_status.html b/www/cxx1z_status.html index 23824e4bf..3725c5c63 100644 --- a/www/cxx1z_status.html +++ b/www/cxx1z_status.html @@ -89,7 +89,7 @@ P0253R1LWGFixing a design mistake in the searchers interfaceJacksonvilleComplete3.9 P0025R0LWGAn algorithm to "clamp" a value between a pair of boundary valuesJacksonvilleComplete3.9 P0154R1LWGconstexpr std::hardware_{constructive,destructive}_interference_sizeJacksonville - P0030R1LWGProposal to Introduce a 3-Argument Overload to std::hypotJacksonville + P0030R1LWGProposal to Introduce a 3-Argument Overload to std::hypotJacksonvilleComplete3.9 P0031R0LWGA Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range AccessJacksonville P0272R1LWGGive std::string a non-const .data() member functionJacksonvilleComplete3.9 P0077R2LWGis_callable, the missing INVOKE related traitJacksonvilleComplete3.9