be careful about overflow in con_float

This commit is contained in:
ceriel 1988-04-28 15:03:03 +00:00
parent 6fa999d162
commit 9388fb69f8
3 changed files with 72 additions and 21 deletions

View 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;

View 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;

View 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;