# HG changeset patch # User John W. Eaton # Date 1234986707 18000 # Node ID d724487d2c4b01e2aebe77c287a5b7825726d2f9 # Parent 401d54a836905dc39c34c28ce8ab164f0ceea02e hex2num.cc: use union to avoid cast and GCC warning diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,8 @@ 2009-02-18 John W. Eaton + * 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. diff --git a/src/DLD-FUNCTIONS/hex2num.cc b/src/DLD-FUNCTIONS/hex2num.cc --- a/src/DLD-FUNCTIONS/hex2num.cc +++ b/src/DLD-FUNCTIONS/hex2num.cc @@ -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 (ch - 'a' + 10); + num.ival += static_cast (ch - 'a' + 10); else if (ch >= 'A') - num += static_cast (ch - 'A' + 10); + num.ival += static_cast (ch - 'A' + 10); else - num += static_cast (ch - '0'); + num.ival += static_cast (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(&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 (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 (num >> ((15 - j) * 4) & 0xF); + static_cast (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']) - - */ - - +*/