-
Notifications
You must be signed in to change notification settings - Fork 11.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc++] Constrain additional overloads of pow
for complex
harder
#110235
Open
frederick-vs-ja
wants to merge
1
commit into
llvm:main
Choose a base branch
from
frederick-vs-ja:complex-pow-constraints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
libcxx/test/libcxx/numerics/complex.number/cmplx.over.pow.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <complex> | ||
|
||
// template<class T, class U> complex<__promote<T, U>::type> pow(const complex<T>&, const U&); | ||
// template<class T, class U> complex<__promote<T, U>::type> pow(const complex<T>&, const complex<U>&); | ||
// template<class T, class U> complex<__promote<T, U>::type> pow(const T&, const complex<U>&); | ||
|
||
// Test that these additional overloads are free from catching std::complex<non-floating-point>, | ||
// which is expected by several 3rd party libraries, see https://github.com/llvm/llvm-project/issues/109858. | ||
|
||
#include <cassert> | ||
#include <cmath> | ||
#include <complex> | ||
#include <type_traits> | ||
|
||
#include "test_macros.h" | ||
|
||
namespace usr { | ||
struct usr_tag {}; | ||
|
||
template <class T, class U> | ||
TEST_CONSTEXPR | ||
typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || | ||
(std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), | ||
int>::type | ||
pow(const T&, const std::complex<U>&) { | ||
return std::is_same<T, usr_tag>::value ? 0 : 1; | ||
} | ||
|
||
template <class T, class U> | ||
TEST_CONSTEXPR | ||
typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || | ||
(std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), | ||
int>::type | ||
pow(const std::complex<T>&, const U&) { | ||
return std::is_same<U, usr_tag>::value ? 2 : 3; | ||
} | ||
|
||
template <class T, class U> | ||
TEST_CONSTEXPR | ||
typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || | ||
(std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), | ||
int>::type | ||
pow(const std::complex<T>&, const std::complex<U>&) { | ||
return std::is_same<T, usr_tag>::value ? 4 : 5; | ||
} | ||
} // namespace usr | ||
|
||
int main(int, char**) { | ||
using std::pow; | ||
using usr::pow; | ||
|
||
TEST_CONSTEXPR usr::usr_tag tag; | ||
TEST_CONSTEXPR_CXX14 const std::complex<usr::usr_tag> ctag; | ||
|
||
assert(pow(tag, std::complex<float>(1.0f)) == 0); | ||
assert(pow(std::complex<float>(1.0f), tag) == 2); | ||
assert(pow(tag, std::complex<double>(1.0)) == 0); | ||
assert(pow(std::complex<double>(1.0), tag) == 2); | ||
assert(pow(tag, std::complex<long double>(1.0l)) == 0); | ||
assert(pow(std::complex<long double>(1.0l), tag) == 2); | ||
|
||
assert(pow(1.0f, ctag) == 1); | ||
assert(pow(ctag, 1.0f) == 3); | ||
assert(pow(1.0, ctag) == 1); | ||
assert(pow(ctag, 1.0) == 3); | ||
assert(pow(1.0l, ctag) == 1); | ||
assert(pow(ctag, 1.0l) == 3); | ||
|
||
assert(pow(ctag, std::complex<float>(1.0f)) == 4); | ||
assert(pow(std::complex<float>(1.0f), ctag) == 5); | ||
assert(pow(ctag, std::complex<double>(1.0)) == 4); | ||
assert(pow(std::complex<double>(1.0), ctag) == 5); | ||
assert(pow(ctag, std::complex<long double>(1.0l)) == 4); | ||
assert(pow(std::complex<long double>(1.0l), ctag) == 5); | ||
|
||
#if TEST_STD_VER >= 11 | ||
static_assert(pow(tag, std::complex<float>(1.0f)) == 0, ""); | ||
static_assert(pow(std::complex<float>(1.0f), tag) == 2, ""); | ||
static_assert(pow(tag, std::complex<double>(1.0)) == 0, ""); | ||
static_assert(pow(std::complex<double>(1.0), tag) == 2, ""); | ||
static_assert(pow(tag, std::complex<long double>(1.0l)) == 0, ""); | ||
static_assert(pow(std::complex<long double>(1.0l), tag) == 2, ""); | ||
|
||
static_assert(pow(1.0f, ctag) == 1, ""); | ||
static_assert(pow(ctag, 1.0f) == 3, ""); | ||
static_assert(pow(1.0, ctag) == 1, ""); | ||
static_assert(pow(ctag, 1.0) == 3, ""); | ||
static_assert(pow(1.0l, ctag) == 1, ""); | ||
static_assert(pow(ctag, 1.0l) == 3, ""); | ||
|
||
static_assert(pow(ctag, std::complex<float>(1.0f)) == 4, ""); | ||
static_assert(pow(std::complex<float>(1.0f), ctag) == 5, ""); | ||
static_assert(pow(ctag, std::complex<double>(1.0)) == 4, ""); | ||
static_assert(pow(std::complex<double>(1.0), ctag) == 5, ""); | ||
static_assert(pow(ctag, std::complex<long double>(1.0l)) == 4, ""); | ||
static_assert(pow(std::complex<long double>(1.0l), ctag) == 5, ""); | ||
#endif | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this isn't an issue for e.g.
std::asinh(std::complex<T>)
defined below? I'm wary of trying to work around something that is explicitly unspecified in the Standard.