changeset 9553:0c72d9284087

further bool ops tweaks
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 21 Aug 2009 12:12:25 +0200
parents ded32956d660
children bda3513e7ba9
files liboctave/CMatrix.cc liboctave/CNDArray.cc liboctave/ChangeLog liboctave/dMatrix.cc liboctave/dNDArray.cc liboctave/fCMatrix.cc liboctave/fCNDArray.cc liboctave/fMatrix.cc liboctave/fNDArray.cc liboctave/mx-inlines.cc liboctave/mx-op-defs.h
diffstat 11 files changed, 62 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/CMatrix.cc
+++ b/liboctave/CMatrix.cc
@@ -3058,8 +3058,7 @@
 boolMatrix
 ComplexMatrix::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolMatrix, ComplexMatrix> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolMatrix, ComplexMatrix> (*this, mx_inline_not);
 }
 
 // other operations
--- a/liboctave/CNDArray.cc
+++ b/liboctave/CNDArray.cc
@@ -497,8 +497,7 @@
 boolNDArray
 ComplexNDArray::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolNDArray, ComplexNDArray> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolNDArray, ComplexNDArray> (*this, mx_inline_not);
 }
 
 // FIXME -- this is not quite the right thing.
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,20 @@
+2009-08-20  Jaroslav Hajek  <highegg@gmail.com>
+
+	* mx-inlines.cc (logical_value): New overloaded template.
+	(DEFMXBOOL_OP): Use it in definitions.
+	(mx_inline_not): Also use logical_value.
+	* mx-op-defs.h (ND_LOGICAL_NAN_CHECK, SC_LOGICAL_NAN_CHECK): Remove
+	(also from all macros).
+
+	* CMatrix.cc (operator !): Simply call mx_inline_not.
+	* CNDArray.cc: Ditto.
+	* dMatrix.cc: Ditto.
+	* dNDArray.cc: Ditto.
+	* fCMatrix.cc: Ditto.
+	* fCNDArray.cc: Ditto.
+	* fMatrix.cc: Ditto.
+	* fNDArray.cc: Ditto.
+	
 2009-08-20  Jaroslav Hajek  <highegg@gmail.com>
 	
 	* dMatrix.cc (Matrix::operator!): Simplify & check for NaNs.
--- a/liboctave/dMatrix.cc
+++ b/liboctave/dMatrix.cc
@@ -2567,8 +2567,7 @@
 boolMatrix
 Matrix::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolMatrix, Matrix> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolMatrix, Matrix> (*this, mx_inline_not);
 }
 
 // column vector by row vector -> matrix operations
--- a/liboctave/dNDArray.cc
+++ b/liboctave/dNDArray.cc
@@ -538,8 +538,7 @@
 boolNDArray
 NDArray::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolNDArray, NDArray> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolNDArray, NDArray> (*this, mx_inline_not);
 }
 
 bool
--- a/liboctave/fCMatrix.cc
+++ b/liboctave/fCMatrix.cc
@@ -3051,8 +3051,7 @@
 boolMatrix
 FloatComplexMatrix::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolMatrix, FloatComplexMatrix> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolMatrix, FloatComplexMatrix> (*this, mx_inline_not);
 }
 
 // other operations
--- a/liboctave/fCNDArray.cc
+++ b/liboctave/fCNDArray.cc
@@ -492,8 +492,7 @@
 boolNDArray
 FloatComplexNDArray::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolNDArray, FloatComplexNDArray> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolNDArray, FloatComplexNDArray> (*this, mx_inline_not);
 }
 
 // FIXME -- this is not quite the right thing.
--- a/liboctave/fMatrix.cc
+++ b/liboctave/fMatrix.cc
@@ -2566,8 +2566,7 @@
 boolMatrix
 FloatMatrix::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolMatrix, FloatMatrix> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolMatrix, FloatMatrix> (*this, mx_inline_not);
 }
 
 // column vector by row vector -> matrix operations
--- a/liboctave/fNDArray.cc
+++ b/liboctave/fNDArray.cc
@@ -496,8 +496,7 @@
 boolNDArray
 FloatNDArray::operator ! (void) const
 {
-  ND_LOGICAL_NAN_CHECK (*this);
-  return do_mx_unary_op<boolNDArray, FloatNDArray> (*this, mx_inline_iszero);
+  return do_mx_unary_op<boolNDArray, FloatNDArray> (*this, mx_inline_not);
 }
 
 bool
--- a/liboctave/mx-inlines.cc
+++ b/liboctave/mx-inlines.cc
@@ -48,7 +48,6 @@
 { for (size_t i = 0; i < n; i++) r[i] = OP x[i]; }
 
 DEFMXUNOP (mx_inline_uminus, -)
-DEFMXUNOP (mx_inline_not, !)
 
 #define DEFMXUNBOOLOP(F, OP) \
 template <class X> \
@@ -119,36 +118,57 @@
 inline void F (size_t n, bool *r, X x, const std::complex<Y> *y) \
 { for (size_t i = 0; i < n; i++) r[i] = x OP real (y[i]); }
 
-#define DEFMXBOOLOP(F, EQ1, OP, EQ2) \
+// Convert to logical value, for logical op purposes.
+template <class T> inline bool logical_value (T x) { return x; }
+template <class T> inline bool logical_value (const octave_int<T>& x) 
+{ return x.value (); }
+
+// NaNs in real data should generate an error. Doing it on-the-fly is faster.
+
+#define DEFLOGCHKNAN(ARG, ZERO) \
+inline bool logical_value (ARG x) \
+{ if (xisnan (x)) gripe_nan_to_logical_conversion (); return x != ZERO; }
+
+DEFLOGCHKNAN (double, 0.0)
+DEFLOGCHKNAN (const Complex&, 0.0)
+DEFLOGCHKNAN (float, 0.0f)
+DEFLOGCHKNAN (const FloatComplex&, 0.0f)
+
+template <class X>
+void mx_inline_not (size_t n, bool *r, const X* x)
+{
+  for (size_t i = 0; i < n; i++)
+    r[i] = ! logical_value (x[i]);
+}
+
+#define DEFMXBOOLOP(F, NOT1, OP, NOT2) \
 template <class X, class Y> \
 inline void F (size_t n, bool *r, const X *x, const Y *y) \
 { \
-  const X xzero = X(); \
-  const Y yzero = Y(); \
   for (size_t i = 0; i < n; i++) \
-    r[i] = (x[i] EQ1 xzero) OP (y[i] EQ2 yzero); \
+    r[i] = (NOT1 logical_value (x[i])) OP (NOT2 logical_value (y[i])); \
 } \
 template <class X, class Y> \
 inline void F (size_t n, bool *r, const X *x, Y y) \
 { \
-  const X xzero = X(); \
-  const bool yy = y EQ2 Y(); \
-  for (size_t i = 0; i < n; i++) r[i] = (x[i] EQ1 xzero) OP yy; \
+  const bool yy = (NOT2 logical_value (y)); \
+  for (size_t i = 0; i < n; i++) \
+    r[i] = (NOT1 logical_value (x[i])) OP yy; \
 } \
 template <class X, class Y> \
 inline void F (size_t n, bool *r, X x, const Y *y) \
 { \
-  const bool xx = x EQ1 X(); \
-  const Y yzero = Y(); \
-  for (size_t i = 0; i < n; i++) r[i] = xx OP (y[i] EQ2 yzero); \
+  const bool xx = (NOT1 logical_value (x)); \
+  for (size_t i = 0; i < n; i++) \
+    r[i] = xx OP (NOT2 logical_value (y[i])); \
 }
 
-DEFMXBOOLOP (mx_inline_and, !=, &, !=)
-DEFMXBOOLOP (mx_inline_or, !=, |, !=)
-DEFMXBOOLOP (mx_inline_not_and, ==, &, !=)
-DEFMXBOOLOP (mx_inline_not_or, ==, |, !=)
-DEFMXBOOLOP (mx_inline_and_not, !=, &, ==)
-DEFMXBOOLOP (mx_inline_or_not, !=, |, ==)
+DEFMXBOOLOP (mx_inline_and, , &, )
+DEFMXBOOLOP (mx_inline_or, , |, )
+DEFMXBOOLOP (mx_inline_not_and, !, &, )
+DEFMXBOOLOP (mx_inline_not_or, !, |, )
+DEFMXBOOLOP (mx_inline_and_not, , &, !)
+DEFMXBOOLOP (mx_inline_or_not, , |, !)
 
 template <class T> 
 inline bool 
--- a/liboctave/mx-op-defs.h
+++ b/liboctave/mx-op-defs.h
@@ -28,14 +28,6 @@
 #include "mx-op-decl.h"
 #include "mx-inlines.cc"
 
-#define ND_LOGICAL_NAN_CHECK(X) \
-if (mx_inline_any_nan ((X).numel (), (X).data ())) \
-    gripe_nan_to_logical_conversion ()
-
-#define SC_LOGICAL_NAN_CHECK(X) \
-  if (xisnan(X)) \
-      gripe_nan_to_logical_conversion ()
-
 // vector by scalar operations.
 
 #define VS_BIN_OP(R, F, OP, V, S) \
@@ -158,8 +150,6 @@
   boolMatrix \
   F (const M& m, const S& s) \
   { \
-    ND_LOGICAL_NAN_CHECK (m); \
-    SC_LOGICAL_NAN_CHECK (s); \
     return do_ms_binary_op<boolMatrix, M, S> (m, s, OP); \
   }
 
@@ -215,8 +205,6 @@
   boolMatrix \
   F (const S& s, const M& m) \
   { \
-    SC_LOGICAL_NAN_CHECK (s); \
-    ND_LOGICAL_NAN_CHECK (m); \
     return do_sm_binary_op<boolMatrix, S, M> (s, m, OP); \
   }
 
@@ -277,8 +265,6 @@
   boolMatrix \
   F (const M1& m1, const M2& m2) \
   { \
-    ND_LOGICAL_NAN_CHECK(m1); \
-    ND_LOGICAL_NAN_CHECK(m2); \
     return do_mm_binary_op<boolMatrix, M1, M2> (m1, m2, OP, #F); \
   }
 
@@ -381,8 +367,6 @@
   boolNDArray \
   F (const ND& m, const S& s) \
   { \
-    ND_LOGICAL_NAN_CHECK (m); \
-    SC_LOGICAL_NAN_CHECK (s); \
     return do_ms_binary_op<boolNDArray, ND, S> (m, s, OP); \
   }
 
@@ -487,8 +471,6 @@
   boolNDArray \
   F (const S& s, const ND& m) \
   { \
-    SC_LOGICAL_NAN_CHECK (s); \
-    ND_LOGICAL_NAN_CHECK (m); \
     return do_sm_binary_op<boolNDArray, S, ND> (s, m, OP); \
   }
 
@@ -549,8 +531,6 @@
   boolNDArray \
   F (const ND1& m1, const ND2& m2) \
   { \
-    ND_LOGICAL_NAN_CHECK(m1); \
-    ND_LOGICAL_NAN_CHECK(m2); \
     return do_mm_binary_op<boolNDArray, ND1, ND2> (m1, m2, OP, #F); \
   }