be careful about overflow in con_float
authorceriel <none@none>
Thu, 28 Apr 1988 15:03:03 +0000 (15:03 +0000)
committerceriel <none@none>
Thu, 28 Apr 1988 15:03:03 +0000 (15:03 +0000)
mach/i86/cg/mach.c
mach/i86/ncg/mach.c
mach/m68k2/cg/mach.c

index 71e5a4f..009f84d 100644 (file)
@@ -63,9 +63,9 @@ con_float() {
        double f;
        double atof();
        int i;
-#ifndef OWNFLOAT
-       double frexp();
        int j;
+       double frexp();
+#ifndef OWNFLOAT
        int sign = 0;
        int fraction[4] ;
 #else OWNFLOAT
@@ -79,9 +79,31 @@ con_float() {
        }
        fprintf(codefile,"!float %s sz %d\n", str, argval);
        f = atof(str);
+       if (f == 0) {
+               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
+               fprintf(codefile, ".data2 0, 0\n");
+               return;
+       }
 #ifdef OWNFLOAT
        if (argval == 4) {
+               /* careful: avoid overflow */
+               double ldexp();
+               f = frexp(f, &i);
                fl = f;
+               fl = frexp(fl,&j);
+               if (i+j > 127) {
+                       /* overflow situation */
+                       fprintf(codefile, ".data1 0%o, 0377, 0377, 0377 ! overflow\n",
+                               f < 0 ? 0377 : 0177);
+                       return;
+               }
+               if (i+j < -127) {
+                       /* underflow situation */
+                       fprintf(codefile, ".data1 0%o, 0200, 0, 0 ! underflow\n",
+                               f < 0 ? 0200 : 0);
+                       return;
+               }
+               fl = ldexp(fl, i+j);
                p = (char *) &fl;
        }
        else {
@@ -92,11 +114,6 @@ con_float() {
                fprintf(codefile,",0%o", *p++ & 0377);
        }
 #else OWNFLOAT
-       if (f == 0) {
-               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
-               fprintf(codefile, ".data2 0, 0\n");
-               return;
-       }
        f = frexp(f, &i);
        if (f < 0) {
                f = -f;
index 71e5a4f..009f84d 100644 (file)
@@ -63,9 +63,9 @@ con_float() {
        double f;
        double atof();
        int i;
-#ifndef OWNFLOAT
-       double frexp();
        int j;
+       double frexp();
+#ifndef OWNFLOAT
        int sign = 0;
        int fraction[4] ;
 #else OWNFLOAT
@@ -79,9 +79,31 @@ con_float() {
        }
        fprintf(codefile,"!float %s sz %d\n", str, argval);
        f = atof(str);
+       if (f == 0) {
+               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
+               fprintf(codefile, ".data2 0, 0\n");
+               return;
+       }
 #ifdef OWNFLOAT
        if (argval == 4) {
+               /* careful: avoid overflow */
+               double ldexp();
+               f = frexp(f, &i);
                fl = f;
+               fl = frexp(fl,&j);
+               if (i+j > 127) {
+                       /* overflow situation */
+                       fprintf(codefile, ".data1 0%o, 0377, 0377, 0377 ! overflow\n",
+                               f < 0 ? 0377 : 0177);
+                       return;
+               }
+               if (i+j < -127) {
+                       /* underflow situation */
+                       fprintf(codefile, ".data1 0%o, 0200, 0, 0 ! underflow\n",
+                               f < 0 ? 0200 : 0);
+                       return;
+               }
+               fl = ldexp(fl, i+j);
                p = (char *) &fl;
        }
        else {
@@ -92,11 +114,6 @@ con_float() {
                fprintf(codefile,",0%o", *p++ & 0377);
        }
 #else OWNFLOAT
-       if (f == 0) {
-               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
-               fprintf(codefile, ".data2 0, 0\n");
-               return;
-       }
        f = frexp(f, &i);
        if (f < 0) {
                f = -f;
index 9757e2a..78d252f 100644 (file)
@@ -61,9 +61,9 @@ con_float() {
        double f;
        double atof();
        int i;
-#ifndef OWNFLOAT
-       double frexp();
        int j;
+       double frexp();
+#ifndef OWNFLOAT
        int sign = 0;
        int fraction[4] ;
 #else OWNFLOAT
@@ -77,9 +77,31 @@ con_float() {
        }
        fprintf(codefile,"!float %s sz %d\n", str, argval);
        f = atof(str);
+       if (f == 0) {
+               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
+               fprintf(codefile, ".data2 0, 0\n");
+               return;
+       }
 #ifdef OWNFLOAT
        if (argval == 4) {
+               /* careful: avoid overflow */
+               double ldexp();
+               f = frexp(f, &i);
                fl = f;
+               fl = frexp(fl,&j);
+               if (i+j > 127) {
+                       /* overflow situation */
+                       fprintf(codefile, ".data1 0%o, 0377, 0377, 0377 ! overflow\n",
+                               f < 0 ? 0377 : 0177);
+                       return;
+               }
+               if (i+j < -127) {
+                       /* underflow situation */
+                       fprintf(codefile, ".data1 0%o, 0200, 0, 0 ! underflow\n",
+                               f < 0 ? 0200 : 0);
+                       return;
+               }
+               fl = ldexp(fl, i+j);
                p = (char *) &fl;
        }
        else {
@@ -90,11 +112,6 @@ con_float() {
                fprintf(codefile,",0%o", *p++ & 0377);
        }
 #else OWNFLOAT
-       if (f == 0) {
-               if (argval == 8) fprintf(codefile, ".data2 0, 0\n");
-               fprintf(codefile, ".data2 0, 0\n");
-               return;
-       }
        f = frexp(f, &i);
        if (f < 0) {
                f = -f;