octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

MSVC compiler support [patch 36]: abs of long


From: John W. Eaton
Subject: MSVC compiler support [patch 36]: abs of long
Date: Fri, 27 Oct 2006 22:33:32 -0400

On 17-Oct-2006, Michael Goffioul wrote:

| Help the compiler to find the correct function for "abs"
| Index: src/pr-output.cc
| ===================================================================
| RCS file: /cvs/octave/src/pr-output.cc,v
| retrieving revision 1.149
| diff -p -c -r1.149 pr-output.cc
| *** src/pr-output.cc  2 Oct 2006 17:23:19 -0000       1.149
| --- src/pr-output.cc  17 Oct 2006 11:07:42 -0000
| *************** octave_print_internal (std::ostream& os,
| *** 2329,2341 ****
|         for (octave_idx_type i = 0; i < dims.numel (); i++)
|           {
|             int new_digits = static_cast<int> 
| !             (floor (log10 (double (abs (nda(i).value ()))) + 1.0));
|   
|             if (new_digits > digits)
|               digits = new_digits;
|   
|             if (! isneg)
| !           isneg = (abs (nda(i).value ()) != nda(i).value ());
|           }
|   
|         fw = digits + isneg;
| --- 2329,2341 ----
|         for (octave_idx_type i = 0; i < dims.numel (); i++)
|           {
|             int new_digits = static_cast<int> 
| !             (floor (log10 (double (abs ((long)nda(i).value ()))) + 1.0));
|   
|             if (new_digits > digits)
|               digits = new_digits;
|   
|             if (! isneg)
| !           isneg = (abs ((long)nda(i).value ()) != nda(i).value ());
|           }
|   
|         fw = digits + isneg;

I don't think this is the right fix because the value that would be
cast to long can be a 64-bit type and on some systems that have 64-bit
values, long will only be 32-bits wide.

Does the following work?

Thanks,

jwe

src/ChangeLog:

2006-10-27  John W. Eaton  <address@hidden>

        * pr-output.cc (SPECIALIZE_UABS): New macro.
        Use it to generate specializations of abs for unsigned int types.
        Instantiate abs for signed int types.

Index: src/pr-output.cc
===================================================================
RCS file: /cvs/octave/src/pr-output.cc,v
retrieving revision 1.150
diff -u -u -r1.150 pr-output.cc
--- src/pr-output.cc    27 Oct 2006 02:16:18 -0000      1.150
+++ src/pr-output.cc    28 Oct 2006 02:32:49 -0000
@@ -2205,19 +2205,41 @@
     }
 }
 
+// FIXME -- all this mess with abs is an attempt to avoid seeing
+//
+//   warning: comparison of unsigned expression < 0 is always false
+//
+// from GCC.  Isn't there a better way
+
 template <class T>
 /* static */ inline T
 abs (T x)
 {
-  return x;
+  return x < 0 ? -x : x;
 }
 
-#define INSTANTIATE_ABS(T) template /* static */ inline T abs (T x)
+#define INSTANTIATE_ABS(T) \
+  template /* static */ inline T abs (T)
 
-INSTANTIATE_ABS (unsigned int);
-INSTANTIATE_ABS (unsigned short);
-INSTANTIATE_ABS (unsigned long);
-INSTANTIATE_ABS (unsigned long long);
+INSTANTIATE_ABS(signed char);
+INSTANTIATE_ABS(short);
+INSTANTIATE_ABS(int);
+INSTANTIATE_ABS(long);
+INSTANTIATE_ABS(long long);
+
+#define SPECIALIZE_UABS(T) \
+  template <> \
+  /* static */ inline unsigned T \
+  abs (unsigned T x) \
+  { \
+    return x; \
+  }
+
+SPECIALIZE_UABS(char)
+SPECIALIZE_UABS(short)
+SPECIALIZE_UABS(int)
+SPECIALIZE_UABS(long)
+SPECIALIZE_UABS(long long)
 
 template <class T>
 void

reply via email to

[Prev in Thread] Current Thread [Next in Thread]