changeset 8808:d724487d2c4b

hex2num.cc: use union to avoid cast and GCC warning
author John W. Eaton <jwe@octave.org>
date Wed, 18 Feb 2009 14:51:47 -0500
parents 401d54a83690
children 732cb0236488
files src/ChangeLog src/DLD-FUNCTIONS/hex2num.cc
diffstat 2 files changed, 27 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Feb 18 14:32:53 2009 -0500
+++ b/src/ChangeLog	Wed Feb 18 14:51:47 2009 -0500
@@ -1,5 +1,8 @@
 2009-02-18  John W. Eaton  <jwe@octave.org>
 
+	* DLD-FUNCTIONS/hex2num.cc (Fhex2num, Fnum2hex):
+	Use union to avoid reinterpret_cast and GCC warning.
+
 	* mex.cc (call_mex): Declare local nargout variable volatile to
 	avoid "maybe clobbered by vfork" warning from GCC.
 
--- a/src/DLD-FUNCTIONS/hex2num.cc	Wed Feb 18 14:32:53 2009 -0500
+++ b/src/DLD-FUNCTIONS/hex2num.cc	Wed Feb 18 14:51:47 2009 -0500
@@ -68,24 +68,29 @@
 
 	  for (octave_idx_type i = 0; i < nr; i++)
 	    {
-	      uint64_t num = 0;
+	      union
+	      {
+		uint64_t ival;
+		double dval;
+	      } num;
+
 	      for (octave_idx_type j = 0; j < nc; j++)
 		{
 		  unsigned char ch = cmat.elem (i, j);
 
 		  if (isxdigit (ch))
 		    {
-		      num <<= 4;
+		      num.ival <<= 4;
 		      if (ch >= 'a')
-			num += static_cast<uint64_t> (ch - 'a' + 10);
+			num.ival += static_cast<uint64_t> (ch - 'a' + 10);
 		      else if (ch >= 'A')
-			num += static_cast<uint64_t> (ch - 'A' + 10);
+			num.ival += static_cast<uint64_t> (ch - 'A' + 10);
 		      else
-			num += static_cast<uint64_t> (ch - '0');
+			num.ival += static_cast<uint64_t> (ch - '0');
 		    }
 		  else
 		    {
-		      error ("hex2num: invalid character found in string");
+		      error ("hex2num: illegal character found in string");
 		      break;
 		    }
 		}
@@ -95,10 +100,9 @@
 	      else
 		{
 		  if (nc < 16)
-		    num <<= (16 - nc) * 4;
+		    num.ival <<= (16 - nc) * 4;
 
-		  m (i) = *reinterpret_cast<double *>(&num);
-
+		  m(i) = num.dval;
 		}
 	    }
 
@@ -111,12 +115,8 @@
 }
 
 /*
-
 %!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]')
-
- */
-
-
+*/
 
 DEFUN_DLD (num2hex, args, ,
   "-*- texinfo -*-\n\
@@ -153,11 +153,18 @@
 
 	  for (octave_idx_type i = 0; i < nr; i++)
 	    {
-	      const uint64_t num = *reinterpret_cast<const uint64_t *> (pv++);
+	      union
+	      {
+		uint64_t ival;
+		double dval;
+	      } num;
+
+	      num.dval = *pv++;
+
 	      for (octave_idx_type j = 0; j < 16; j++)
 		{
 		  unsigned char ch = 
-		    static_cast<char> (num >> ((15 - j) * 4) & 0xF);
+		    static_cast<char> (num.ival >> ((15 - j) * 4) & 0xF);
 		  if (ch >= 10)
 		    ch += 'a' - 10;
 		  else
@@ -175,9 +182,5 @@
 }
 
 /*
-
 %!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000'])
-
- */
-
-
+*/