Skip to content

Commit

Permalink
<complex> Enable users to disable normalization for division too. (N…
Browse files Browse the repository at this point in the history
…VIDIA#474)

* `<complex>` Enable users to disable normalization for division too.

Also add some documentation for those.
  • Loading branch information
miscco committed Jun 6, 2023
1 parent 3fa3678 commit ec85394
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/standard_api/numerics_library/complex.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ This warning can be suppressed silenced with `#pragma`s, but only globally, not
User-defined floating-point literals must be specified in terms of
`long double`, so they lead to warns that are unable to be suppressed.

## Customizations

Our implementation by default recovers infinite values during multiplication and division. This adds a significant runtime overhead, so we allow disabling that canonicalization if it is not desired.

Definition of `LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS` disables canonicalization for both multiplication *and* division.

Definition of `LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION` or `LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION` disables canonicalization for multiplication or division individually.

11 changes: 11 additions & 0 deletions include/cuda/std/detail/libcxx/include/complex
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ template<class T> complex<T> tanh (const complex<T>&);
# define _LIBCUDACXX_COMPLEX_ALIGNAS(V)
# endif

#ifdef LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS
# ifndef LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_MULTIPLICATION
# define LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_MULTIPLICATION
# endif // LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_MULTIPLICATION
# ifndef LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION
# define LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION
# endif // LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION
#endif // LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template<class _Tp> class _LIBCUDACXX_TEMPLATE_VIS _LIBCUDACXX_COMPLEX_ALIGNAS(2*sizeof(_Tp)) complex;
Expand Down Expand Up @@ -968,6 +977,7 @@ operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
_Tp __denom = __c * __c + __d * __d;
_Tp __x = _CUDA_VSTD::__constexpr_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
_Tp __y = _CUDA_VSTD::__constexpr_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
#ifndef LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION
if (_CUDA_VSTD::__constexpr_isnan(__x) && _CUDA_VSTD::__constexpr_isnan(__y))
{
if ((__denom == _Tp(0)) && (!_CUDA_VSTD::__constexpr_isnan(__a) || !_CUDA_VSTD::__constexpr_isnan(__b)))
Expand All @@ -988,6 +998,7 @@ operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
__y = _Tp(0) * (__b * __c - __a * __d);
}
}
#endif // LIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_DIVISION
return complex<_Tp>(__x, __y);
}

Expand Down

0 comments on commit ec85394

Please sign in to comment.