Skip to content

[SYCL] Adds complex Literals support to sycl::experimental::complex #9819

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

Merged
merged 6 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions sycl/doc/extensions/proposed/sycl_ext_oneapi_complex.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,33 @@ mathematical operation.
|Compute the hyperbolic tangent of complex number x.
|===


=== Suffixes for complex number literals

This proposal describes literal suffixes for constructing complex number literals.
The suffixes `i` and `if` create complex numbers of the types `complex<double>` and `complex<float>` respectively, with their imaginary part denoted by the given literal number and the real part being zero.

```C++
namespace sycl {
namespace ext {
namespace oneapi {

namespace literals {
namespace complex_literals {
constexpr complex<double> operator""i(long double x);
constexpr complex<double> operator""i(unsigned long long x);

constexpr complex<float> operator""if(long double x);
constexpr complex<float> operator""if(unsigned long long x);
} // namespace complex_literals
} // namespace literals

} // namespace oneapi
} // namespace ext
} // namespace sycl
```


== Implementation notes

The complex mathematical operations can all be defined using SYCL built-ins.
Expand Down
21 changes: 21 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,27 @@ tan(const complex<_Tp> &__x) {
return complex<_Tp>(__z.imag(), -__z.real());
}

// Literal suffix for complex number literals [complex.literals]
inline namespace literals {
inline namespace complex_literals {
constexpr complex<double> operator""i(long double __im) {
return {0.0, static_cast<double>(__im)};
}

constexpr complex<double> operator""i(unsigned long long __im) {
return {0.0, static_cast<double>(__im)};
}

constexpr complex<float> operator""if(long double __im) {
return {0.0f, static_cast<float>(__im)};
}

constexpr complex<float> operator""if(unsigned long long __im) {
return {0.0f, static_cast<float>(__im)};
}
} // namespace complex_literals
} // namespace literals

} // namespace experimental
} // namespace oneapi
} // namespace ext
Expand Down
12 changes: 12 additions & 0 deletions sycl/test/extensions/test_complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ void check_sycl_constructor_from_std() {
}
}

// Check types for sycl complex constructed from literals
void check_sycl_complex_literals() {
static_assert(
std::is_same_v<decltype(0.3if),
sycl::ext::oneapi::experimental::complex<float>>);
static_assert(
std::is_same_v<decltype(0.3i),
sycl::ext::oneapi::experimental::complex<double>>);
}

int main() {
check_math_function_types();
check_math_operator_types();
Expand All @@ -195,5 +205,7 @@ int main() {
check_std_to_sycl_conversion();
check_sycl_constructor_from_std();

check_sycl_complex_literals();

return 0;
}