[arrayfire] 97/408: Adding more operator overloading for af::cfloat and af::cdouble

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:11:27 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit 56a000d7f7ab811a7218a900f552ac095f2b943a
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Thu Jul 2 19:30:13 2015 -0400

    Adding more operator overloading for af::cfloat and af::cdouble
---
 include/af/complex.h    | 31 +++++++++++++------------
 src/api/cpp/complex.cpp | 61 ++++++++++++++++++++++++++-----------------------
 2 files changed, 49 insertions(+), 43 deletions(-)

diff --git a/include/af/complex.h b/include/af/complex.h
index 066958b..52210e7 100644
--- a/include/af/complex.h
+++ b/include/af/complex.h
@@ -50,21 +50,22 @@ AFAPI double real(af_cdouble val);
 AFAPI float imag(af_cfloat val);
 AFAPI double imag(af_cdouble val);
 
-AFAPI af::cfloat operator+(const af::cfloat &lhs, const af::cfloat &rhs);
-AFAPI af::cfloat operator+(const af::cfloat &lhs, const double &rhs);
-AFAPI af::cdouble operator+(const af::cdouble &lhs, const af::cdouble &rhs);
-AFAPI af::cdouble operator+(const af::cdouble &lhs, const double &rhs);
-
-AFAPI af::cfloat operator-(const af::cfloat &lhs, const af::cfloat &rhs);
-AFAPI af::cdouble operator-(const af::cdouble &lhs, const af::cdouble &rhs);
-
-AFAPI cfloat operator*(const cfloat &lhs, const cfloat &rhs);
-AFAPI cdouble operator*(const cdouble &lhs, const cdouble &rhs);
-
-AFAPI cfloat operator/(const cfloat &lhs, const cfloat &rhs);
-AFAPI af::cfloat operator/(const af::cfloat &lhs, const float &rhs);
-AFAPI cdouble operator/(const cdouble &lhs, const cdouble &rhs);
-AFAPI af::cdouble operator/(const af::cdouble &lhs, const double &rhs);
+#define DEFINE_OP(OP)                                                   \
+    AFAPI af::cfloat  operator OP(const af::cfloat  &lhs, const af::cfloat  &rhs); \
+    AFAPI af::cdouble operator OP(const af::cdouble &lhs, const af::cdouble &rhs); \
+    AFAPI af::cfloat  operator OP(const af::cfloat  &lhs, const     double  &rhs); \
+    AFAPI af::cdouble operator OP(const af::cdouble &lhs, const     double  &rhs); \
+    AFAPI af::cfloat  operator OP(const double      &rhs, const af::cfloat  &lhs); \
+    AFAPI af::cdouble operator OP(const double      &rhs, const af::cdouble &lhs); \
+    AFAPI af::cdouble operator OP(const af::cfloat  &lhs, const af::cdouble &rhs); \
+    AFAPI af::cdouble operator OP(const af::cdouble &lhs, const af::cfloat  &rhs); \
+
+DEFINE_OP(+)
+DEFINE_OP(-)
+DEFINE_OP(*)
+DEFINE_OP(/)
+
+#undef DEFINE_OP
 
 AFAPI bool operator==(const cfloat &lhs, const cfloat &rhs);
 AFAPI bool operator==(const cdouble &lhs, const cdouble &rhs);
diff --git a/src/api/cpp/complex.cpp b/src/api/cpp/complex.cpp
index ddfd038..7ec2c98 100644
--- a/src/api/cpp/complex.cpp
+++ b/src/api/cpp/complex.cpp
@@ -14,6 +14,7 @@
 
 namespace af
 {
+using std::complex;
 
 float real(af_cfloat val) { return val.real; }
 double real(af_cdouble val) { return val.real; }
@@ -21,7 +22,6 @@ double real(af_cdouble val) { return val.real; }
 float imag(af_cfloat val) { return val.imag; }
 double imag(af_cdouble val) { return val.imag; }
 
-
 cfloat operator+(const cfloat &lhs, const cfloat &rhs)
 {
     cfloat out(lhs.real + rhs.real, lhs.imag + rhs.imag);
@@ -34,19 +34,6 @@ cdouble operator+(const cdouble &lhs, const cdouble &rhs)
     return out;
 }
 
-cfloat operator-(const cfloat &lhs, const cfloat &rhs)
-{
-    cfloat out(lhs.real - rhs.real, lhs.imag - rhs.imag);
-    return out;
-}
-
-cdouble operator-(const cdouble &lhs, const cdouble &rhs)
-{
-    cdouble out(lhs.real - rhs.real, lhs.imag - rhs.imag);
-    return out;
-}
-
-    using std::complex;
 cfloat operator*(const cfloat &lhs, const cfloat &rhs)
 {
     complex<float> clhs(lhs.real, lhs.imag);
@@ -63,6 +50,18 @@ cdouble operator*(const cdouble &lhs, const cdouble &rhs)
     return cdouble(out.real(), out.imag());
 }
 
+cfloat operator-(const cfloat &lhs, const cfloat &rhs)
+{
+    cfloat out(lhs.real - rhs.real, lhs.imag - rhs.imag);
+    return out;
+}
+
+cdouble operator-(const cdouble &lhs, const cdouble &rhs)
+{
+    cdouble out(lhs.real - rhs.real, lhs.imag - rhs.imag);
+    return out;
+}
+
 cfloat operator/(const cfloat &lhs, const cfloat &rhs)
 {
     complex<float> clhs(lhs.real, lhs.imag);
@@ -79,20 +78,26 @@ cdouble operator/(const cdouble &lhs, const cdouble &rhs)
     return cdouble(out.real(), out.imag());
 }
 
-cfloat operator/(const cfloat &lhs, const float &rhs)
-{
-    complex<float> clhs(lhs.real, lhs.imag);
-    complex<float> out = clhs / rhs;
-    return cfloat(out.real(), out.imag());
-
-}
-
-cdouble operator/(const cdouble &lhs, const double &rhs)
-{
-    complex<double> clhs(lhs.real, lhs.imag);
-    complex<double> out = clhs / rhs;
-    return cdouble(out.real(), out.imag());
-}
+#define IMPL_OP(OP)                                             \
+    cfloat  operator OP(const cfloat  &lhs, const double  &rhs) \
+    { return lhs OP cfloat (rhs); }                             \
+    cdouble operator OP(const cdouble &lhs, const double  &rhs) \
+    { return lhs OP cdouble(rhs); }                             \
+    cfloat  operator OP(const double  &rhs, const cfloat  &lhs) \
+    { return cfloat (lhs) OP rhs; }                             \
+    cdouble operator OP(const double  &rhs, const cdouble &lhs) \
+    { return cdouble(lhs) OP rhs; }                             \
+    cdouble operator OP(const cfloat  &lhs, const cdouble &rhs) \
+    { return cdouble(real(lhs), imag(lhs)) OP rhs; }            \
+    cdouble operator OP(const cdouble &lhs, const cfloat  &rhs) \
+    { return lhs OP cdouble(real(rhs), imag(rhs)); }            \
+
+IMPL_OP(+)
+IMPL_OP(-)
+IMPL_OP(*)
+IMPL_OP(/)
+
+#undef IMPL_OP
 
 bool operator!=(const cfloat &lhs, const cfloat &rhs)
 {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list