Work around bug in portable representation of flonums: the flonum 0.0
authorChris Hanson <org/chris-hanson/cph>
Thu, 21 Sep 1989 22:53:12 +0000 (22:53 +0000)
committerChris Hanson <org/chris-hanson/cph>
Thu, 21 Sep 1989 22:53:12 +0000 (22:53 +0000)
is written as "06 + 0".  But this program always reads two numbers
after the sign, and furthermore treats the first one as the exponent,
and the -second- as the size in bits.  This caused it to gobble up the
type code of the next word by accident.

v7/src/microcode/psbtobin.c
v8/src/microcode/psbtobin.c

index 366f1d7c503889c891c2b1de26a02c5609268165..66a29622de7166c79b566cdd1b15861b582ec986 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-C-*-
 
-$Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v7/src/microcode/Attic/psbtobin.c,v 9.36 1989/09/20 23:04:46 cph Exp $
+$Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v7/src/microcode/Attic/psbtobin.c,v 9.37 1989/09/21 22:53:12 cph Exp $
 
 Copyright (c) 1987, 1989 Massachusetts Institute of Technology
 
@@ -410,24 +410,67 @@ compute_max()
   return (Result);
 }
 \f
+long
+read_signed_decimal (stream)
+     fast FILE * stream;
+{
+  fast int c = (getc (stream));
+  fast long result = (-1);
+  int negative_p = 0;
+  while (c == ' ')
+    c = (getc (stream));
+  if (c == '-')
+    {
+      negative_p = 1;
+      c = (getc (stream));
+    }
+  else if (c == '+')
+    c = (getc (stream));
+  if ((c >= '0') && (c <= '9'))
+    {
+      result = (c - '0');
+      c = (getc (stream));
+      while ((c >= '0') && (c <= '9'))
+       {
+         result = ((result * 10) + (c - '0'));
+         c = (getc (stream));
+       }
+    }
+  if (c != EOF)
+    ungetc (c, stream);
+  if (result == (-1))
+    {
+      fprintf (stderr, "%s: Unable to read expected decimal integer\n",
+              program_name);
+      inconsistency ();
+    }
+  return (negative_p ? (-result) : result);
+}
+\f
 double
-read_a_flonum()
+read_a_flonum ()
 {
   Boolean negative;
-  long size_in_bits, exponent;
+  long exponent;
+  long size_in_bits;
   fast double Result;
 
-  getc(portable_file);                         /* Space */
-  negative = ((getc(portable_file)) == '-');
-  VMS_BUG(exponent = 0);
-  VMS_BUG(size_in_bits = 0);
-  fscanf(portable_file, "%ld %ld", &exponent, &size_in_bits);
+  getc (portable_file);                                /* Space */
+  negative = ((getc (portable_file)) == '-');
+  /* Hair here because portable file format incorrect for flonum 0. */
+  exponent = (read_signed_decimal (portable_file));
+  if (exponent == 0)
+    {
+      int c = (getc (portable_file));
+      if (c == '\n')
+       return (0);
+      ungetc (c, portable_file);
+    }
+  size_in_bits = (read_signed_decimal (portable_file));
   if (size_in_bits == 0)
-  {
-    Result = 0.0;
-  }
-  else if ((exponent > MAX_FLONUM_EXPONENT) ||
-          (exponent < -MAX_FLONUM_EXPONENT))
+    return (0);
+  if ((exponent > MAX_FLONUM_EXPONENT) ||
+      (exponent < -MAX_FLONUM_EXPONENT))
   {
     /* Skip over mantissa */
 
index ebb47e440f8cee243c866866071412df69e08a6c..7dd4b9981aeb4dc63939479194955cab9753250c 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-C-*-
 
-$Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v8/src/microcode/psbtobin.c,v 9.36 1989/09/20 23:04:46 cph Exp $
+$Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v8/src/microcode/psbtobin.c,v 9.37 1989/09/21 22:53:12 cph Exp $
 
 Copyright (c) 1987, 1989 Massachusetts Institute of Technology
 
@@ -410,24 +410,67 @@ compute_max()
   return (Result);
 }
 \f
+long
+read_signed_decimal (stream)
+     fast FILE * stream;
+{
+  fast int c = (getc (stream));
+  fast long result = (-1);
+  int negative_p = 0;
+  while (c == ' ')
+    c = (getc (stream));
+  if (c == '-')
+    {
+      negative_p = 1;
+      c = (getc (stream));
+    }
+  else if (c == '+')
+    c = (getc (stream));
+  if ((c >= '0') && (c <= '9'))
+    {
+      result = (c - '0');
+      c = (getc (stream));
+      while ((c >= '0') && (c <= '9'))
+       {
+         result = ((result * 10) + (c - '0'));
+         c = (getc (stream));
+       }
+    }
+  if (c != EOF)
+    ungetc (c, stream);
+  if (result == (-1))
+    {
+      fprintf (stderr, "%s: Unable to read expected decimal integer\n",
+              program_name);
+      inconsistency ();
+    }
+  return (negative_p ? (-result) : result);
+}
+\f
 double
-read_a_flonum()
+read_a_flonum ()
 {
   Boolean negative;
-  long size_in_bits, exponent;
+  long exponent;
+  long size_in_bits;
   fast double Result;
 
-  getc(portable_file);                         /* Space */
-  negative = ((getc(portable_file)) == '-');
-  VMS_BUG(exponent = 0);
-  VMS_BUG(size_in_bits = 0);
-  fscanf(portable_file, "%ld %ld", &exponent, &size_in_bits);
+  getc (portable_file);                                /* Space */
+  negative = ((getc (portable_file)) == '-');
+  /* Hair here because portable file format incorrect for flonum 0. */
+  exponent = (read_signed_decimal (portable_file));
+  if (exponent == 0)
+    {
+      int c = (getc (portable_file));
+      if (c == '\n')
+       return (0);
+      ungetc (c, portable_file);
+    }
+  size_in_bits = (read_signed_decimal (portable_file));
   if (size_in_bits == 0)
-  {
-    Result = 0.0;
-  }
-  else if ((exponent > MAX_FLONUM_EXPONENT) ||
-          (exponent < -MAX_FLONUM_EXPONENT))
+    return (0);
+  if ((exponent > MAX_FLONUM_EXPONENT) ||
+      (exponent < -MAX_FLONUM_EXPONENT))
   {
     /* Skip over mantissa */