changeset 31242:96ad887ae4f8 stable

dec2bin.m and dec2hex.m: graft bugfixes from dd6b37f67db2 to stable (bug #63089) Fix behavior of dec2bin and dec2hex for negative integer-type inputs
author Arun Giridhar <arungiridhar@gmail.com>
date Wed, 28 Sep 2022 17:00:43 -0400
parents adfbd487d0f6
children 7018819318d1 24247d13a44b
files scripts/strings/dec2bin.m scripts/strings/dec2hex.m
diffstat 2 files changed, 77 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/dec2bin.m	Mon Sep 19 13:13:31 2022 +0200
+++ b/scripts/strings/dec2bin.m	Wed Sep 28 17:00:43 2022 -0400
@@ -24,12 +24,11 @@
 ########################################################################
 
 ## -*- texinfo -*-
-## @deftypefn  {} {} dec2bin (@var{d})
-## @deftypefnx {} {} dec2bin (@var{d}, @var{len})
+## @deftypefn  {} {@var{bstr} =} dec2bin (@var{d})
+## @deftypefnx {} {@var{bstr} =} dec2bin (@var{d}, @var{len})
 ## Return a string of ones and zeros representing the conversion of the integer
 ## @var{d} to a binary number.
 ##
-## If @var{d} is negative, return the two's complement binary value of @var{d}.
 ## If @var{d} is a matrix or cell array, return a string matrix with one row
 ## for each element in @var{d}, padded with leading zeros to the width of the
 ## largest value.
@@ -37,6 +36,13 @@
 ## The optional second argument, @var{len}, specifies the minimum number of
 ## digits in the result.
 ##
+## For negative elements of @var{d}, return the binary value of the two's
+## complement.  The result is padded with leading ones to 8, 16, 32, or 64
+## bits as appropriate for the magnitude of the input.  Positive input
+## elements are padded with leading zeros to the same width.  If the second
+## argument @var{len} exceeds that calculated width, the result is further
+## padded with leading zeros, for compatibility with @sc{matlab}.
+##
 ## Examples:
 ##
 ## @example
@@ -49,9 +55,6 @@
 ## @end group
 ## @end example
 ##
-## Programming Notes: The largest negative value that can be converted into
-## two's complement is @code{- (flintmax () / 2)}.
-##
 ## Known @sc{matlab} Incompatibility: @sc{matlab}'s @code{dec2bin} allows
 ## non-integer values for @var{d}, truncating the value using the equivalent
 ## of @code{fix (@var{d})} for positive values, but, as of R2020b and in
@@ -63,7 +66,7 @@
 ## @seealso{bin2dec, dec2base, dec2hex}
 ## @end deftypefn
 
-function b = dec2bin (d, len)
+function bstr = dec2bin (d, len)
 
   if (nargin == 0)
     print_usage ();
@@ -75,26 +78,39 @@
   ## Create column vector for algorithm (output is always col. vector anyways)
   d = d(:);
 
-  lt_zero_idx = (d < 0);
-  if (any (lt_zero_idx))
-    ## FIXME: Need an algorithm that works with larger values such as int64.
-    if (any (d(lt_zero_idx) < -2^52))
-      error ("dec2bin: negative inputs cannot be less than -flintmax () / 2");
-    elseif (any (d(lt_zero_idx) < intmin ("int32")))
-      d(lt_zero_idx) += flintmax ();
-    elseif (any (d < intmin ("int16")))
-      d(lt_zero_idx) += double (intmax ("uint32")) + 1;
-    elseif (any (d < intmin ("int8")))
-      d(lt_zero_idx) += double (intmax ("uint16"))+ 1;
+  neg = (d < 0);  # keep track of which elements are negative
+  if (any (neg))  # must be a signed type
+    ## Cast to a suitable signed integer type, then to unsigned.
+    ## Ensure that the left-most bit of the unsigned number is 1,
+    ## to signify negative input.
+    tmp = int64 (d);
+    if (all (tmp >= -128 & tmp <= 127))
+      d = int8 (d);
+      d(neg) = (d(neg) + intmax (d)) + 1;
+      d = uint8 (d);
+      d(neg) += uint8 (128);
+    elseif (all (tmp >= -32768 & tmp <= 32767))
+      d = int16 (d);
+      d(neg) = (d(neg) + intmax (d)) + 1;
+      d = uint16 (d);
+      d(neg) += uint16 (32768);
+    elseif (all (tmp >= -2147483648 & tmp <= 2147483647))
+      d = int32 (d);
+      d(neg) = (d(neg) + intmax (d)) + 1;
+      d = uint32 (d);
+      d(neg) += uint32 (2147483648);
     else
-      d(lt_zero_idx) += double (intmax ("uint8")) + 1;
+      d = int64 (d);
+      d(neg) = (d(neg) + intmax (d)) + 1;
+      d = uint64 (d);
+      d(neg) += uint64 (9223372036854775808);
     endif
   endif
 
   if (nargin == 1)
-    b = dec2base (d, 2);
+    bstr = dec2base (d, 2);
   else
-    b = dec2base (d, 2, len);
+    bstr = dec2base (d, 2, len);
   endif
 
 endfunction
@@ -111,25 +127,20 @@
 %!assert (dec2bin (-3), "11111101")
 %!assert (dec2bin (-3, 3), "11111101")
 %!assert (dec2bin (-3, 9), "011111101")
-%!assert (dec2bin (-2^7 -1), "1111111101111111")
-%!assert (dec2bin (-2^15 -1), "11111111111111110111111111111111")
-## FIXME: Matlab generates a string that is 64 characters long
-%!assert (dec2bin (-2^31 -1),
-%!        "11111111111111111111101111111111111111111111111111111")
+%!assert (dec2bin (-2^7 - 1), "1111111101111111")
+%!assert (dec2bin (-2^15 - 1), "11111111111111110111111111111111")
+%!assert (dec2bin (-2^31 - 1),
+%!        "1111111111111111111111111111111101111111111111111111111111111111")
 %!assert (dec2bin (-2^52),
-%!        "10000000000000000000000000000000000000000000000000000")
-## FIXME: Uncomment when support for int64 is added
-%!#assert (dec2bin (-2^63),
+%!        "1111111111110000000000000000000000000000000000000000000000000000")
+%!assert (dec2bin (-2^63),
 %!        "1000000000000000000000000000000000000000000000000000000000000000")
-%!#test
-%! assert (dec2bin (int64 (-2^63)),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2bin (int64 (-2^63) -1),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2bin (int64 (-2^63) +1),
-%!        "1000000000000000000000000000000000000000000000000000000000000001");
+%!assert (dec2bin (int64 (-2) ^ 63),
+%!        "1000000000000000000000000000000000000000000000000000000000000000")
+%!assert (dec2bin (int64 (-2) ^ 63 - 1),
+%!        "1000000000000000000000000000000000000000000000000000000000000000")
+%!assert (dec2bin (int64 (-2) ^ 63 + 1),
+%!        "1000000000000000000000000000000000000000000000000000000000000001")
 %!assert (dec2bin ([-1, -2; -3, -4]),
 %!        ["11111111"; "11111101"; "11111110"; "11111100"])
 %!assert (dec2bin ([1, 2; 3, -4]),
@@ -139,4 +150,4 @@
 
 ## Test input validation
 %!error <Invalid call> dec2bin ()
-%!error <negative inputs cannot be less than> dec2bin (- flintmax ())
+
--- a/scripts/strings/dec2hex.m	Mon Sep 19 13:13:31 2022 +0200
+++ b/scripts/strings/dec2hex.m	Wed Sep 28 17:00:43 2022 -0400
@@ -24,8 +24,8 @@
 ########################################################################
 
 ## -*- texinfo -*-
-## @deftypefn  {} {} dec2hex (@var{d})
-## @deftypefnx {} {} dec2hex (@var{d}, @var{len})
+## @deftypefn  {} {@var{hstr} =} dec2hex (@var{d})
+## @deftypefnx {} {@var{hstr} =} dec2hex (@var{d}, @var{len})
 ## Return a string representing the conversion of the integer @var{d} to a
 ## hexadecimal (base16) number.
 ##
@@ -53,40 +53,31 @@
 ## @seealso{hex2dec, dec2base, dec2bin}
 ## @end deftypefn
 
-function h = dec2hex (d, len)
+function hstr = dec2hex (d, len)
 
   if (nargin == 0)
     print_usage ();
   endif
 
-  if (iscell (d))
-    d = cell2mat (d);
-  endif
-  ## Create column vector for algorithm (output is always col. vector anyways)
-  d = d(:);
-
-  lt_zero_idx = (d < 0);
-  if (any (lt_zero_idx))
-    ## FIXME: Need an algorithm that works with larger values such as int64.
-    if (any (d(lt_zero_idx) < -2^52))
-      error ("dec2hex: negative inputs cannot be less than -flintmax () / 2");
-    elseif (any (d(lt_zero_idx) < intmin ("int32")))
-      d(lt_zero_idx) += flintmax ();
-    elseif (any (d < intmin ("int16")))
-      d(lt_zero_idx) += double (intmax ("uint32")) + 1;
-    elseif (any (d < intmin ("int8")))
-      d(lt_zero_idx) += double (intmax ("uint16"))+ 1;
-    else
-      d(lt_zero_idx) += double (intmax ("uint8")) + 1;
-    endif
+  ## To avoid repeating a lot of code, including input validation, we call dec2bin.
+  if (nargin == 2)
+    d = dec2bin (d, len*4);
+  else
+    d = dec2bin (d);
   endif
 
-  if (nargin == 1)
-    h = dec2base (d, 16);
-  else
-    h = dec2base (d, 16, len);
+  ## Left-pad with zeros to make the number of columns divisible by 4
+  n = mod (columns (d), 4);
+  if (n > 0)
+    d = [repmat("0", rows(d), 4-n), d];
   endif
 
+  d -= "0"; # convert to numeric
+  d = d(:, 1:4:end) * 8 + d(:, 2:4:end) * 4 + d(:, 3:4:end) * 2 + d(:, 4:4:end);
+  ## Elements of d are now in the range 0 to 15
+
+  hstr = "0123456789ABCDEF"(d+1); # convert to char and return
+
 endfunction
 
 
@@ -100,28 +91,18 @@
 %!assert (dec2hex (-3), "FD")
 %!assert (dec2hex (-3, 1), "FD")
 %!assert (dec2hex (-3, 3), "0FD")
-%!assert (dec2hex (-2^7 -1), "FF7F")
-%!assert (dec2hex (-2^15 -1), "FFFF7FFF")
-## FIXME: Matlab returns longer string that begins with 'F'
-%!assert (dec2hex (-2^31 -1), "1FFFFF7FFFFFFF")
-## FIXME: Matlab returns longer string that begins with 'FFF'
-%!assert (dec2hex (-2^52), "10000000000000")
-## FIXME: Uncomment when support for int64 is added
-%!#assert (dec2hex (-2^63),
-%!        "1000000000000000000000000000000000000000000000000000000000000000")
-%!#test
-%! assert (dec2hex (int64 (-2^63)),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2hex (int64 (-2^63) -1),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2hex (int64 (-2^63) +1),
-%!        "1000000000000000000000000000000000000000000000000000000000000001");
+%!assert (dec2hex (-2^7 - 1), "FF7F")
+%!assert (dec2hex (-2^15 - 1), "FFFF7FFF")
+%!assert (dec2hex (-2^31 - 1), "FFFFFFFF7FFFFFFF")
+%!assert (dec2hex (-2^52), "FFF0000000000000")
+%!assert (dec2hex (-2^63), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63 - 1), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63 + 1), "8000000000000001")
 %!assert (dec2hex ([-1, -2; -3, -4]), ["FF"; "FD"; "FE"; "FC"])
 %!assert (dec2hex ([1, 2; 3, -4]), ["01"; "03"; "02"; "FC"])
 %!assert (dec2hex ({1, 2; 3, -4}), ["01"; "03"; "02"; "FC"])
 
 ## Test input validation
 %!error <Invalid call> dec2hex ()
-%!error <negative inputs cannot be less than> dec2hex (- flintmax ())
+