changeset 8293:ad5bb02d267a

workaround missing std::abs(int64_t) in MSVC
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 30 Oct 2008 20:12:28 +0100
parents 242756f065f0
children b2a6309b2d87
files liboctave/ChangeLog liboctave/oct-inttypes.cc liboctave/oct-inttypes.h
diffstat 3 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Thu Oct 30 15:08:50 2008 +0100
+++ b/liboctave/ChangeLog	Thu Oct 30 20:12:28 2008 +0100
@@ -1,3 +1,10 @@
+2008-10-30  Jaroslav Hajek <highegg@gmail.com>
+
+	* oct-inttypes.h (octave_int_abs): New function.
+	(octave_int_arith_base<T, true>::div): Use octave_int_abs instead of
+	std::abs.
+	* oct-inttypes.cc (octave_int_arith_base<int64_t, true>): Dtto.
+
 2008-10-28  Jaroslav Hajek <highegg@gmail.com>
 
 	* Array-C.cc Array-d.cc Array-f.cc Array-fC.cc Array-i.cc Array-s.cc:
--- a/liboctave/oct-inttypes.cc	Thu Oct 30 15:08:50 2008 +0100
+++ b/liboctave/oct-inttypes.cc	Thu Oct 30 20:12:28 2008 +0100
@@ -196,7 +196,7 @@
   // (as above) and impose the sign.
   // FIXME: Can we do something faster if we HAVE_FAST_INT_OPS?
 
-  uint64_t usx = std::abs (x), usy = std::abs (y);
+  uint64_t usx = octave_int_abs (x), usy = octave_int_abs (y);
   bool positive = (x < 0) == (y < 0);
 
   // Get upper words
@@ -454,7 +454,7 @@
       dblesplit (y, sign, my, e);
       uint32_t w[4];
       sign = (sign != (x.value () < 0));
-      umul128 (std::abs (x.value ()), my, w);
+      umul128 (octave_int_abs (x.value ()), my, w);
       octave_int64 res = octave_int64::zero;
       for (short i = 0; i < 4; i++)
         {
--- a/liboctave/oct-inttypes.h	Thu Oct 30 15:08:50 2008 +0100
+++ b/liboctave/oct-inttypes.h	Thu Oct 30 20:12:28 2008 +0100
@@ -39,6 +39,12 @@
 inline long double xround (long double x) { return roundl (x); }
 #endif
 
+// FIXME: we define this by our own because some compilers, such as MSVC,
+// do not provide std::abs (int64_t) and std::abs (uint64_t). In the future,
+// it should go away in favor of std::abs.
+template <class T>
+inline T octave_int_abs (T x) { return x >= 0 ? x : -x; }
+
 // Query for an integer type of certain sizeof, and signedness.
 template<int qsize, bool qsigned>
 struct query_integer_type
@@ -694,7 +700,7 @@
           else
             {
               z = x / y;
-              T w = -std::abs (x % y); // Can't overflow, but std::abs (x) can!
+              T w = -octave_int_abs (x % y); // Can't overflow, but std::abs (x) can!
               if (w <= y - w) 
                 z -= 1 - (signbit (x) << 1);
             }
@@ -702,7 +708,8 @@
       else
         {
           z = x / y;
-          T w = std::abs (x % y); // Can't overflow, but std::abs (x) can!
+          // FIXME: this is a workaround due to MSVC's absence of std::abs (int64_t).
+          T w = octave_int_abs (x % y); // Can't overflow, but std::abs (x) can!
           if (w >= y - w) 
             z += 1 - (signbit (x) << 1);
         }