OpenCL-Guide icon indicating copy to clipboard operation
OpenCL-Guide copied to clipboard

Issue with the example of cpp_for_opencl.md

Open Eldadkro opened this issue 2 years ago • 0 comments

In chapters/cpp_for_opencl.md there is an example of how to implement a kernel using C++ of complex number arithmetic.

template<typename T>
class complex_t {
T m_re; // Real component.
T m_im; // Imaginary component.

public:
complex_t(T re, T im): m_re{re}, m_im{im} {};
complex_t operator*(const complex_t &other) const
{
  return {m_re * other.m_re - m_im * other.m_im,
           m_re * other.m_im + m_im * other.m_re};
}
int get_re() const { return m_re; }
int get_im() const { return m_im; }
};

It seems like:

  1. the get functions should return T type.
  2. The multiplication operator should return a new instance of the complex_t class rather than a brace-enclosed list.

Here is what I believe is what was intended:

template<typename T>
class complex_t {
T m_re; // Real component.
T m_im; // Imaginary component.

public:
complex_t(T re, T im): m_re{re}, m_im{im} {};

complex_t operator*(const complex_t &other) const
{
  return complex_t<T>(m_re * other.m_re - m_im * other.m_im,
           m_re * other.m_im + m_im * other.m_re);
}

T get_re() const { return m_re; }
T get_im() const { return m_im; }
};

Eldadkro avatar Sep 11 '23 12:09 Eldadkro