Speclib  0.1.2
The library for writing better CUDA libraries
sp::Complex< T > Class Template Reference

A complex number of type T. More...

#include <Complex.hpp>

Public Types

using RealType = T
 
using UnderlyingRealType = decltype(squared_norm(T{}))
 Recursively removes any instances of sp::Complex around a type and returns the underlying real type. More...
 

Public Member Functions

constexpr Complex ()=default
 Default constructor. More...
 
constexpr Complex (const T &re, const T &im=T())
 Construct from real/imaginary parts. More...
 
template<typename Q >
constexpr Complex (const Complex< Q > &s)
 Construct from a different type of Complex. More...
 
template<typename Q >
constexpr Complex (const std::complex< Q > &s)
 Construct from an std::complex (and permit type-conversion). More...
 
constexpr Complex (const Complex< T > &s)=default
 Copy constructor (must be explicitly specified because an explicit volatile operator = exists). More...
 
constexpr T real () const
 Get the real part. More...
 
constexpr T & real ()
 Access the real part. More...
 
constexpr void real (T value)
 Update the real part. More...
 
constexpr T imag () const
 Get the imaginary part. More...
 
constexpr T & imag ()
 Access the imaginary part. More...
 
constexpr void imag (T value)
 Update the imaginary part. More...
 
constexpr auto norm () const
 Squared magnitude (consistent with std::norm()) More...
 
constexpr auto norm_l1 () const
 
constexpr auto abs () const
 Magnitude (consistent with std::abs()) More...
 
constexpr auto conjugate () const
 Complex conjugate. More...
 
template<typename Q >
constexpr bool operator== (const Complex< Q > &other) const
 Comparison to other complex numbers. More...
 
template<typename Q >
constexpr bool operator== (const Q &other) const
 Comparison to scalar, which is interpreted as a Complex(x, 0). More...
 
template<typename Q >
constexpr bool operator!= (const Q &other) const
 General purpose inequality operator. More...
 
volatile Complex< T > & operator= (const volatile Complex< T > &other) volatile
 Volatile assignment operators. More...
 
volatile Complex< T > & operator= (const Complex< T > &other) volatile
 
Complex< T > & operator= (const volatile Complex< T > &other)
 
Complex< T > & operator= (const Complex< T > &other)=default
 Assignment. More...
 
constexpr Complex< T > operator+ () const
 
constexpr Complex< T > operator- () const
 

Public Attributes

re
 
im
 

Detailed Description

template<typename T>
class sp::Complex< T >

A complex number of type T.

The API is very similar to std::complex, but it allows T to be any type which has the right arithmetic operators. Notably, this means you can have half-float complex numbers.

Other notable properties:

  • Far more fluent API than cuComplex. You can actually use arithmetic operators!
  • Fully constexpr, so it can be used for metaprogramming.
  • Uses the Cayley-Dickson construction, so can be composed to form quaternions etc.

Example

This example is a very uninteresting device function that computes (a * x) + y for three complex numbers, a, x, and y.

Vanilla CUDA

__device__ cuComplex complexAxpy(cuComplex a, cuComplex x, cuComplex y) {
return cuCaddf(cuCmulf(a, x), y);
}

Using Speclib

constexpr auto complexAxpy(sp::Complex<float> a, sp::Complex<float> x, sp::Complex<float> y) {
return (a * x) + y;
}
A complex number of type T.
Definition: Complex.hpp:89

The speclib version also:

  • Is constexpr, so will work at compile time, as well as on both CPU and GPU.
  • Compiles to the same machine code.
  • Easily adapts to work for double precision, half-precision, integers, etc, by adding a template parameter

Member Typedef Documentation

◆ UnderlyingRealType

template<typename T >
using sp::Complex< T >::UnderlyingRealType = decltype(squared_norm(T{}))

Recursively removes any instances of sp::Complex around a type and returns the underlying real type.

Constructor & Destructor Documentation

◆ Complex() [1/5]

template<typename T >
constexpr sp::Complex< T >::Complex ( )
constexprdefault

Default constructor.

◆ Complex() [2/5]

template<typename T >
constexpr sp::Complex< T >::Complex ( const T &  re,
const T &  im = T() 
)
constexpr

Construct from real/imaginary parts.

◆ Complex() [3/5]

template<typename T >
template<typename Q >
constexpr sp::Complex< T >::Complex ( const Complex< Q > &  s)
constexpr

Construct from a different type of Complex.

◆ Complex() [4/5]

template<typename T >
template<typename Q >
constexpr sp::Complex< T >::Complex ( const std::complex< Q > &  s)
constexpr

Construct from an std::complex (and permit type-conversion).

◆ Complex() [5/5]

template<typename T >
constexpr sp::Complex< T >::Complex ( const Complex< T > &  s)
constexprdefault

Copy constructor (must be explicitly specified because an explicit volatile operator = exists).

Member Function Documentation

◆ abs()

template<typename T >
constexpr auto sp::Complex< T >::abs ( ) const
constexpr

Magnitude (consistent with std::abs())

◆ conjugate()

template<typename T >
constexpr auto sp::Complex< T >::conjugate
constexpr

Complex conjugate.

◆ imag() [1/3]

template<typename T >
constexpr T & sp::Complex< T >::imag ( )
constexpr

Access the imaginary part.

◆ imag() [2/3]

template<typename T >
constexpr T sp::Complex< T >::imag ( ) const
constexpr

Get the imaginary part.

◆ imag() [3/3]

template<typename T >
constexpr void sp::Complex< T >::imag ( value)
constexpr

Update the imaginary part.

◆ norm()

template<typename T >
constexpr auto sp::Complex< T >::norm ( ) const
constexpr

Squared magnitude (consistent with std::norm())

◆ operator!=()

template<typename T >
template<typename Q >
constexpr bool sp::Complex< T >::operator!= ( const Q &  other) const
constexpr

General purpose inequality operator.

◆ operator=() [1/2]

template<typename T >
Complex< T > & sp::Complex< T >::operator= ( const Complex< T > &  other)
default

Assignment.

◆ operator=() [2/2]

template<typename T >
volatile Complex< T > & sp::Complex< T >::operator= ( const volatile Complex< T > &  other) volatile

Volatile assignment operators.

◆ operator==() [1/2]

template<typename T >
template<typename Q >
constexpr bool sp::Complex< T >::operator== ( const Complex< Q > &  other) const
constexpr

Comparison to other complex numbers.

◆ operator==() [2/2]

template<typename T >
template<typename Q >
constexpr bool sp::Complex< T >::operator== ( const Q &  other) const
constexpr

Comparison to scalar, which is interpreted as a Complex(x, 0).

◆ real() [1/3]

template<typename T >
constexpr T & sp::Complex< T >::real ( )
constexpr

Access the real part.

◆ real() [2/3]

template<typename T >
constexpr T sp::Complex< T >::real ( ) const
constexpr

Get the real part.

◆ real() [3/3]

template<typename T >
constexpr void sp::Complex< T >::real ( value)
constexpr

Update the real part.