From 9388fb69f8a0e04ca3e0f9f994b031ef7d20d36e Mon Sep 17 00:00:00 2001 From: ceriel Date: Thu, 28 Apr 1988 15:03:03 +0000 Subject: [PATCH] be careful about overflow in con_float --- mach/i86/cg/mach.c | 31 ++++++++++++++++++++++++------- mach/i86/ncg/mach.c | 31 ++++++++++++++++++++++++------- mach/m68k2/cg/mach.c | 31 ++++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/mach/i86/cg/mach.c b/mach/i86/cg/mach.c index 71e5a4fa4..009f84d7c 100644 --- a/mach/i86/cg/mach.c +++ b/mach/i86/cg/mach.c @@ -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; diff --git a/mach/i86/ncg/mach.c b/mach/i86/ncg/mach.c index 71e5a4fa4..009f84d7c 100644 --- a/mach/i86/ncg/mach.c +++ b/mach/i86/ncg/mach.c @@ -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; diff --git a/mach/m68k2/cg/mach.c b/mach/m68k2/cg/mach.c index 9757e2a0e..78d252fc3 100644 --- a/mach/m68k2/cg/mach.c +++ b/mach/m68k2/cg/mach.c @@ -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; -- 2.34.1