Skip to content
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
2 changes: 1 addition & 1 deletion include/af/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <af/dim4.hpp>
#include <af/seq.h>
#include <af/traits.hpp>

#include <af/complex.h>

#ifdef __cplusplus
namespace af
Expand Down
77 changes: 77 additions & 0 deletions include/af/complex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#ifndef NO_CPP_11
#include <complex>
typedef std::complex<float> std_cfloat;
typedef std::complex<double> std_cdouble;
#endif
#include <af/defines.h>

typedef af_float2 af_cfloat;
typedef af_double2 af_cdouble;

#ifdef __cplusplus
namespace af
{
class AFAPI cfloat
{
private:
af_cfloat cval;

public:
cfloat();
cfloat(const float &real, const float &imag=0);
AFAPI float real() const;
AFAPI float imag() const;
AFAPI cfloat conj() const;
AFAPI cfloat operator+(const float &val) const;
AFAPI cfloat operator+(const cfloat &val) const;
AFAPI cfloat operator-(const float &val) const;
AFAPI cfloat operator-(const cfloat &val) const;
AFAPI friend cfloat operator+(const float &lhs, const cfloat &rhs);
AFAPI friend cfloat operator-(const float &lhs, const cfloat &rhs);

#ifndef NO_CPP_11
AFAPI operator std_cfloat() const;
AFAPI cfloat operator=(const std_cfloat &rhs) const;
#endif
};

AFAPI cfloat operator+(const float &lhs, const cfloat &rhs);
AFAPI cfloat operator-(const float &lhs, const cfloat &rhs);

class AFAPI cdouble
{
private:
af_cdouble cval;

public:
cdouble();
cdouble(const double &real, const double &imag=0);
AFAPI double real() const;
AFAPI double imag() const;
AFAPI cdouble conj() const;
AFAPI cdouble operator+(const double &val) const;
AFAPI cdouble operator+(const cdouble &val) const;
AFAPI cdouble operator-(const double &val) const;
AFAPI cdouble operator-(const cdouble &val) const;
AFAPI friend cdouble operator+(const double &lhs, const cdouble &rhs);
AFAPI friend cdouble operator-(const double &lhs, const cdouble &rhs);

#ifndef NO_CPP_11
AFAPI operator std_cdouble() const;
AFAPI cdouble operator=(const std_cdouble &rhs) const;
#endif
};

AFAPI cdouble operator+(const double &lhs, const cdouble &rhs);
AFAPI cdouble operator-(const double &lhs, const cdouble &rhs);
}
#endif
1 change: 1 addition & 0 deletions include/af/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once
#include <af/defines.h>
#include <af/complex.h>

#ifdef __cplusplus
namespace af
Expand Down
15 changes: 2 additions & 13 deletions include/af/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,15 @@

#include <cstddef>

#ifdef __cplusplus
#include <complex>

typedef std::complex<float> af_cfloat;
typedef std::complex<double> af_cdouble;

#else
typedef struct {
float x;
float y;
} af_cfloat;
} af_float2;

typedef struct {
double x;
double y;
} af_cdouble;

#endif
} af_double2;

typedef enum {
AF_SUCCESS=0,
Expand Down Expand Up @@ -116,8 +107,6 @@ typedef enum {
#ifdef __cplusplus
namespace af
{
typedef af_cfloat cfloat;
typedef af_cdouble cdouble;
typedef af_dtype dtype;
typedef af_source source;
typedef af_interp_type interpType;
Expand Down
1 change: 1 addition & 0 deletions include/af/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <complex>
#include <af/defines.h>
#include <af/complex.h>

namespace af {

Expand Down
174 changes: 174 additions & 0 deletions src/api/cpp/complex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <af/complex.h>

namespace af
{
cfloat::cfloat()
{
cval = {0, 0};
}

cfloat::cfloat(const float &real, const float &imag)
{
cval = {real, imag};
}

float cfloat::real() const
{
return cval.x;
}

float cfloat::imag() const
{
return cval.y;
}

cfloat cfloat::conj() const
{
return cfloat(cval.x, -cval.y);
}

cfloat cfloat::operator+(const cfloat &val) const
{
float real = val.real() + this->real();
float imag = val.imag() + this->imag();
return cfloat(real, imag);
}

cfloat cfloat::operator+(const float &val) const
{
float real = val + this->real();
float imag = this->imag();
return cfloat(real, imag);
}

cfloat cfloat::operator-(const cfloat &val) const
{
float real = this->real() - val.real();
float imag = this->imag() - val.imag();
return cfloat(real, imag);
}

cfloat cfloat::operator-(const float &val) const
{
float real = this->real() - val;
float imag = this->imag();
return cfloat(real, imag);
}


cfloat operator+(const float &lhs, const cfloat &rhs)
{
float real = lhs + rhs.real();
float imag = rhs.imag();
return cfloat(real, imag);
}

cfloat operator-(const float &lhs, const cfloat &rhs)
{
float real = lhs - rhs.real();
float imag = -rhs.imag();
return cfloat(real, imag);
}

#ifndef NO_CPP_11
cfloat::operator std_cfloat() const
{
return std_cfloat(real(), imag());
}

cfloat cfloat::operator=(const std_cfloat &in) const
{
return cfloat(in.real(), in.imag());
}
#endif

cdouble::cdouble()
{
cval = {0, 0};
}

cdouble::cdouble(const double &real, const double &imag)
{
cval = {real, imag};
}

double cdouble::real() const
{
return cval.x;
}

double cdouble::imag() const
{
return cval.y;
}

cdouble cdouble::conj() const
{
return cdouble(cval.x, -cval.y);
}

cdouble cdouble::operator+(const cdouble &val) const
{
double real = val.real() + this->real();
double imag = val.imag() + this->imag();
return cdouble(real, imag);
}

cdouble cdouble::operator+(const double &val) const
{
double real = val + this->real();
double imag = this->imag();
return cdouble(real, imag);
}

cdouble cdouble::operator-(const cdouble &val) const
{
double real = this->real() - val.real();
double imag = this->imag() - val.imag();
return cdouble(real, imag);
}

cdouble cdouble::operator-(const double &val) const
{
double real = this->real() - val;
double imag = this->imag();
return cdouble(real, imag);
}


cdouble operator+(const double &lhs, const cdouble &rhs)
{
double real = lhs + rhs.real();
double imag = rhs.imag();
return cdouble(real, imag);
}

cdouble operator-(const double &lhs, const cdouble &rhs)
{
double real = lhs - rhs.real();
double imag = -rhs.imag();
return cdouble(real, imag);
}

#ifndef NO_CPP_11
cdouble::operator std_cdouble() const
{
return std_cdouble(real(), imag());
}

cdouble cdouble::operator=(const std_cdouble &in) const
{
return cdouble(in.real(), in.imag());
}
#endif

}
4 changes: 2 additions & 2 deletions src/api/cpp/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ namespace af
array constant(cdouble val, const dim4 &dims)
{
af_array res;
AF_THROW(af_constant_complex(&res, real(val), imag(val),
AF_THROW(af_constant_complex(&res, val.real(), val.imag(),
dims.ndims(), dims.get(), c64));
return array(res);
}

array constant(cfloat val, const dim4 &dims)
{
af_array res;
AF_THROW(af_constant_complex(&res, real(val), imag(val),
AF_THROW(af_constant_complex(&res, val.real(), val.imag(),
dims.ndims(), dims.get(), c32));
return array(res);
}
Expand Down
7 changes: 3 additions & 4 deletions src/api/cpp/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ namespace af
{ \
double rval, ival; \
AF_THROW(af_##fn##_all(&rval, &ival, in.get())); \
T out = {(Tr)rval, (Tr)ival}; \
return out; \
return T((Tr)rval, (Tr)ival); \
} \

#define INSTANTIATE(fn) \
Expand All @@ -82,8 +81,8 @@ namespace af
INSTANTIATE_REAL(fn, unsigned) \
INSTANTIATE_REAL(fn, char) \
INSTANTIATE_REAL(fn, unsigned char) \
INSTANTIATE_CPLX(fn, af_cfloat, float) \
INSTANTIATE_CPLX(fn, af_cdouble, double) \
INSTANTIATE_CPLX(fn, cfloat, float) \
INSTANTIATE_CPLX(fn, cdouble, double) \

INSTANTIATE(sum)
INSTANTIATE(min)
Expand Down
14 changes: 14 additions & 0 deletions src/backend/cpu/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@
#pragma once

#include <af/traits.hpp>
namespace af {

template<>
struct dtype_traits<std::complex<float> > {
enum { af_type = c32 };
};

template<>
struct dtype_traits<std::complex<double> > {
enum { af_type = c64 };
};

}

using af::dtype_traits;